看见别人在用implementation rootProject.ext.dependencies["xxxx"]不知道是什么意思,上网查了一下,原来是为了解决或者说预防gradle依赖冲突的问题。
在项目开发中我们会经常引入多个Module,然而每个Module中又包含了V4、V7,为了升级新版本依赖包只用更改一次,我们决定采用Gradle依赖的统一管理,避免重复繁琐的劳动。
记录get到的新知识,用法如下:
1.在Project目录下新建config.gradle文件,文件名可自定义
具体内容如下:
ext { android = [
compileSdkVersion : ,
buildToolsVersion : "27.0.0",
minSdkVersion : ,
targetSdkVersion : ,
versionCode : ,
versionName : "1.2.2",
renderscriptTargetApi :
] version = [
supportLibraryVersion : "26.1.1",
okhttpVersion : "3.9.0",
retrofitVersion : "2.3.0",
glideVersion : "4.0.0",
butterknifeVersion : "8.8.1",
fragmentationVersion : "1.1.9",
] dependencies = [
//base
"appcompat-v7" : "com.android.support:appcompat-v7:${version["supportLibraryVersion"]}",
"cardview-v7" : "com.android.support:cardview-v7:${version["supportLibraryVersion"]}",
"design" : "com.android.support:design:${version["supportLibraryVersion"]}",
"constraint-layout" : "com.android.support.constraint:constraint-layout:1.0.2", //net
"gson" : "com.google.code.gson:gson:2.8.2",
"okhttp" : "com.squareup.okhttp3:okhttp:${version["okhttpVersion"]}",
"logging-interceptor" : "com.squareup.okhttp3:logging-interceptor:${version["okhttpVersion"]}",
"retrofit" : "com.squareup.retrofit2:retrofit:${version["retrofitVersion"]}",
"converter-gson" : "com.squareup.retrofit2:converter-gson:${version["retrofitVersion"]}",
"adapter-rxjava2" : "com.squareup.retrofit2:adapter-rxjava2:${version["retrofitVersion"]}", //dao
"greendao" : "org.greenrobot:greendao:3.2.2", //rx
"rxjava" : "io.reactivex.rxjava2:rxjava:2.1.5",
"rxandroid" : "io.reactivex.rxjava2:rxandroid:2.0.1",
"rxbinding" : "com.jakewharton.rxbinding2:rxbinding:2.1.0",
"rxpermissions" : "com.tbruyelle.rxpermissions2:rxpermissions:0.9.5@aar", //di
"javax_annotation" : "org.glassfish:javax.annotation:10.0-b28",
"butterknife" : "com.jakewharton:butterknife:${version["butterknifeVersion"]}",
"butterknife-compiler" : "com.jakewharton:butterknife-compiler:${version["butterknifeVersion"]}", //multidex
"multidex" : "com.android.support:multidex:1.0.3", //kotlin
"kotlin-stdlib" : "org.jetbrains.kotlin:kotlin-stdlib:1.2.10", //ui test
"espresso-core" : "com.android.support.test.espresso:espresso-core:3.0.2",
"espresso-idling-resource" : "com.android.support.test.espresso:espresso-idling-resource:3.0.2", //unit test , 为了整合mockito和PowerMockito,mockito暂时最高只支持2.8.9
"junit" : "junit:junit:4.12",
"mockito" : "org.mockito:mockito-core:2.8.9",
"powermock-module-junit4" : "org.powermock:powermock-module-junit4:1.7.4" ] }
2.在Project的build.gradle中添加
apply from: "config.gradle"
3.在modle的build.gradle中添加引用
apply plugin: 'com.android.application' android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
buildToolsVersion rootProject.ext.android["buildToolsVersion"] defaultConfig {
applicationId "json.chao.com.wanandroid"
minSdkVersion rootProject.ext.android["minSdkVersion"]
targetSdkVersion rootProject.ext.android["targetSdkVersion"]
versionCode rootProject.ext.android["versionCode"]
versionName rootProject.ext.android["versionName"]
//AndroidJunitRunner必须要显示指定在defaultConfig中,使用Gradle依赖管理无法使其生效
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
renderscriptTargetApi rootProject.ext.android["renderscriptTargetApi"]
renderscriptSupportModeEnabled true // Enable RS support
multiDexEnabled true
vectorDrawables.useSupportLibrary = true
}
} dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs') //base
implementation rootProject.ext.dependencies["appcompat-v7"]
implementation rootProject.ext.dependencies["cardview-v7"]
implementation rootProject.ext.dependencies["design"]
implementation rootProject.ext.dependencies["constraint-layout"] //net
implementation rootProject.ext.dependencies["gson"]
implementation rootProject.ext.dependencies["okhttp"]
implementation rootProject.ext.dependencies["retrofit"]
implementation rootProject.ext.dependencies["converter-gson"]
implementation rootProject.ext.dependencies["adapter-rxjava2"] //dao
implementation rootProject.ext.dependencies["greendao"] //rx
implementation rootProject.ext.dependencies["rxjava"]
implementation rootProject.ext.dependencies["rxandroid"]
implementation rootProject.ext.dependencies["rxbinding"]
implementation rootProject.ext.dependencies["rxpermissions"] //UI测试
androidTestImplementation (rootProject.ext.dependencies["espresso-core"]) {
exclude group: 'com.android.support', module: 'support-annotations'
}
implementation (rootProject.ext.dependencies["espresso-idling-resource"]) {
exclude module: 'support-annotations'
}
}
(两个文件中有不对应的依赖方法,that's ok, 只是粘贴代码的时候删除了一些,知道用法就行了)