Android Studio 管理gradle配置以及第三方App_key - MrWu94/AndroidNote GitHub Wiki
第三方API的KEY的管理
Android开发,基本都会用到第三方SDK,例如百度地图、支付宝支付等。 通常第三方的KEY都是保存在AndroidManifest中:
<!-- 百度地图的KEY-->
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="百度地图注册的KEY"/>
方法
AndroidManifest中的KEY,我们用一个变量代替,在build.gradle中动态的替换,还以百度地图SDK的KEY为例:
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="${baidu_key}"/>
在build.gradle中:
debug {
manifestPlaceholders = [baidu_key: 百度地图的KEY]
}
release {
manifestPlaceholders = [baidu_key: 百度地图的KEY]
}
Gradle中版本管理
gradle中,包含包名、VersionCode、versionName、compileSdkVersion以及第三方依赖库等大量信息,管理起来,稍有不慎就会误删或者改错,导致编译失败,能不能像管理第三方KEY一样,将所有需要修改的参数统一管理起来,只修改参数,不干扰其他因素这样在版本升级中,无需打开gradle在繁杂的代码中修改各种版本号,例如:
新建一个buildconfigure.gradle
// model config file ,make app build.gradle clean
ext {
android = [
AndroidBuildToolsVersion : "23.0.2",
AndridMinSdkVersion : 15,
AndroidTargetSdkVersion : 23,
AndroidCompileSdkVersion : 23,
applicationId : "me.silencedut.nbaplus",
versionCode:10,
versionName:"1.1.0"
]
//LibrariesVersion
googlesupportVersion="23.1.1"
goodleDependencies =[
appcompat_v7: "com.android.support:appcompat-v7:${googlesupportVersion}",
design : "com.android.support:design:${googlesupportVersion}",
support_v4 :"com.android.support:support-v4:${googlesupportVersion}",
recyclerview_v7: "com.android.support:recyclerview-v7:${googlesupportVersion}",
cardview_v7 :"com.android.support:cardview-v7:${googlesupportVersion}"
]
otherDependdencies=[
butterknife :"com.jakewharton:butterknife:7.0.1+",
glide :"com.github.bumptech.glide:glide:3.6.1",
eventbus:"de.greenrobot:eventbus:2.4.0+",
materialish_progress:"com.pnikosis:materialish-progress:1.5+",
circleimageview :"de.hdodenhof:circleimageview:1.3.0+",
licensesdialog :"de.psdev.licensesdialog:licensesdialog:1.8.0+",
fir_sdk:"im.fir:sdk:latest.integration@aar",
retrofit_2_0:"com.squareup.retrofit:retrofit:2.0.0-beta2",
retrofit_converter_gson:"com.squareup.retrofit:converter-gson:2.0.0-beta2",
leakcanary_debug:"com.squareup.leakcanary:leakcanary-android:1.3.1",
leakcanary_release:"com.squareup.leakcanary:leakcanary-android-no-op:1.3.1",
adapter_rxjava:"com.squareup.retrofit:adapter-rxjava:2.0.0-beta2",
rxandroid:"io.reactivex:rxandroid:1.2.1",
rxjava:"io.reactivex:rxjava:1.1.6",
greendao:"de.greenrobot:greendao:2.0.0",
pickview:"com.brucetoo.pickview:library:1.1.1",
finestwebview:"com.thefinestartist:finestwebview:1.1.2"
]
}
在该项目目录build.gradle中
添加buildconfigure.gradle插件
apply from: "buildconfig.gradle"
android{
def globalConfiguration = rootProject.ext.android
compileSdkVersion globalConfiguration.AndroidCompileSdkVersion
buildToolsVersion globalConfiguration.AndroidBuildToolsVersion
defaultConfig {
applicationId globalConfiguration.applicationId
minSdkVersion globalConfiguration.AndridMinSdkVersion
targetSdkVersion globalConfiguration.AndroidTargetSdkVersion
versionCode globalConfiguration.versionCode
versionName globalConfiguration.versionName
}
//build变种生成不同名字的apk
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(
output.outputFile.parent,
"AndroidNode-${variant.versionName}.apk".toLowerCase())
}
}
}
在该项目依赖中添加如下代码
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
def googleDependencies = rootProject.ext.goodleDependencies
def otherDependencies = rootProject.ext.otherDependdencies
compile googleDependencies.support_v4
compile googleDependencies.appcompat_v7
compile googleDependencies.design
compile googleDependencies.recyclerview_v7
compile googleDependencies.cardview_v7
compile otherDependencies.butterknife
compile otherDependencies.glide
compile otherDependencies.eventbus
compile otherDependencies.materialish_progress
compile otherDependencies.circleimageview
compile otherDependencies.licensesdialog
compile otherDependencies.fir_sdk
compile otherDependencies.retrofit_2_0
compile otherDependencies.retrofit_converter_gson
compile otherDependencies.rxandroid
compile otherDependencies.rxjava
compile otherDependencies.adapter_rxjava
compile otherDependencies.greendao
compile otherDependencies.pickview
compile otherDependencies.finestwebview
debugCompile otherDependencies.leakcanary_debug
releaseCompile otherDependencies.leakcanary_release
}
避免引入其他库
你的代码本身就是一个库了,因此我强烈不建议你的代码还引入别人的库。友盟推送的代码就是一个典型的反例,一个推送库引入了okhttp、okio等其他库,臃肿不堪,完全没有让我使用的欲望。 一个第三方库引入其他第三方库有很多坏处,使用者可能会遇到版本冲突的问题(比如:友盟反馈和友盟推送同时使用),方法数还会极速增多。 你可能会说appcompat这个库基本所有第三方库都会引入的,有没有什么好的办法可以避免呢?好在我们有provided关键字。provided可以将你需要的库引入,但是并不会将其打包到aar里面以CommonAdapter为例:
dependencies {
provided 'com.android.support:recyclerview-v7:23.2.1'
provided 'com.android.databinding:baseLibrary:1.0'
provided "org.projectlombok:lombok:1.12.6"
}
都用了私有依赖的方式来做的。 首先,我能确定使用这个库的人,肯定使用了recyclerView,所以我通过私有依赖的方式将recyclerView的代码剔除,那么recyclerView的最终版本由使用者来定。
## 根据需求考虑是否提供no-op
如果你开发的库可能只需要在debug时才用到,但库提供的类或方法需要写入现有的代码中,那么就可以采用no-op的方案。所谓no-op就是希望某些代码仅仅存在于debug环境中,在release版本中,可能就是保留了了一些代码接口,但是并不提供实现。 以leakcanary为例,它的文档中就给出了no-op的依赖。