新的工程建立,多個Moudle的gradle的管理就是一個很繁瑣的問題。
你有沒有見過這樣
“The given artifact contains a string literal with a package reference 'android.support.v4.content'”之類的問題,各種v4 v7和androidx之間的衝突,或者依賴包版本不向下兼容,或者衝突包的問題?
本文采用config.gradle來統一控制整個工程的各個依賴的版本控制。代替原來的在project-gradle工程中自定義變量的方式來控制
第一步:新建config.gradle
打開工程和setting.gradle同一級目錄下新建file,名稱採用全命名"config.gradle"
第二步:引用config.gradle(必要步驟)
在project-gradle下添加“apply from : 'config.gradle'”
// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply from: 'config.gradle'
buildscript {
repositories {
maven {
url 'https://repo.huaweicloud.com/repository/maven/'
}
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
maven {
url 'https://repo.huaweicloud.com/repository/maven/'
}
maven { url "https://jitpack.io" }
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
第三步:編寫config.gradle
本工程導入的依賴包,感覺挺好用的推薦使用,後續的架構中也會用到
ext{
android = [
compileSdkVersion : 29,
minSdkVersion : 21,
targetSdkVersion : 29,
versionCode : 1,
versionName : "1.0",
buildToolsVersion : "29.0.1"
]
dependencies = [
//基礎庫
appcompat : "androidx.appcompat:appcompat:1.1.0",
junit_junit : "junit:junit:4.12",
androidx_junit : "androidx.test.ext:junit:1.1.1",
espresso_core : "androidx.test.espresso:espresso-core:3.2.0",
//app主工程基礎庫
constraintlayout : "androidx.constraintlayout:constraintlayout:1.1.3",
//ARout
arouter_api : "com.alibaba:arouter-api:1.2.2",
arouter_compiler : "com.alibaba:arouter-compiler:1.1.3",
//butterknif插件
butterknif : "com.jakewharton:butterknife:10.0.0",
butterknife_compiler : "com.jakewharton:butterknife-compiler:10.0.0",
//RxJava+Retrofit+okhttp3
rxjava : "io.reactivex.rxjava2:rxjava:2.1.5",
rxandroid : "io.reactivex.rxjava2:rxandroid:2.0.1",
retrofit : "com.squareup.retrofit2:retrofit:2.3.0",
adapter_rxjava : "com.squareup.retrofit2:adapter-rxjava2:2.3.0",
converter_scalars : "com.squareup.retrofit2:converter-scalars:2.3.0",
gson : "com.squareup.retrofit2:converter-gson:2.3.0",
okhttp : "com.squareup.okhttp3:okhttp:3.9.0",
logging_interceptor : "com.squareup.okhttp3:logging-interceptor:3.9.0",
cookie : "com.github.franmontiel:PersistentCookieJar:v1.0.1",
//qmui組件庫
qmui : "com.qmuiteam:qmui:2.0.0-alpha08",
qmui_arch : "com.qmuiteam:arch:2.0.0-alpha08",
qmui_arch_compiler : "com.qmuiteam:arch-compiler:2.0.0-alpha08"
]
}
第四步:在base,app以及其他module的gradle中使用,隨便打開一個gradle看調用方式
apply plugin: 'com.android.library'
android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
defaultConfig {
minSdkVersion rootProject.ext.android["minSdkVersion"]
targetSdkVersion rootProject.ext.android["targetSdkVersion"]
versionCode rootProject.ext.android["versionCode"]
versionName rootProject.ext.android["versionName"]
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
javaCompileOptions {
annotationProcessorOptions {
arguments = [ moduleName : project.getName() ]
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//QMUI
api rootProject.ext.dependencies["qmui"]
api rootProject.ext.dependencies["qmui_arch"]
//內存分析工具
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
//ARouter
api rootProject.ext.dependencies["arouter_api"]
//butterknif插件
api rootProject.ext.dependencies["butterknif"]
//RxJava+Retrofit+okhttp3
api rootProject.ext.dependencies["rxjava"]
api rootProject.ext.dependencies["rxandroid"]
api rootProject.ext.dependencies["retrofit"]
api rootProject.ext.dependencies["adapter_rxjava"]
api rootProject.ext.dependencies["converter_scalars"]
api rootProject.ext.dependencies["gson"]
api rootProject.ext.dependencies["okhttp"]
api rootProject.ext.dependencies["logging_interceptor"]
//Cookie持久化管理(推薦)
api rootProject.ext.dependencies["cookie"]
}
這個是base的gradle,在這裏順便說一下爲什麼都是使用api
從官網介紹可以看出,implementation可以讓module在編譯時隱藏自己使用的依賴,但是在運行時這個依賴對所有模塊是可見的。而api與compile一樣,無法隱藏自己使用的依賴。
有一個結構清晰的gradle的管理會爲後期工程的維護,依賴包的升級減少大量的工作量。