为什么要对 Gradle 统一管理
在实际项目中,经常会出现一个 Project 中有多个 Module 的情况,如果每个 Module 又同时拥有相同的依赖,比如 v4 包,那么,每次 v4 包升级的时候修改多个 Module 中的 build.gradle 文件,更新其版本都是一件麻烦的事情,而且很有可能忘记某个 Module。所以,将依赖进行统一管理,对于一个 Project 下拥有多个 Module 的情况来说是非常必要的。
build.gradle
apply plugin: 'com.android.application'
//apply plugin: 'android-apt'
apply plugin: 'realm-android'
android {
compileSdkVersion rootProject.ext.android.compileSdkVersion
buildToolsVersion rootProject.ext.android.buildToolsVersion
defaultConfig {
applicationId rootProject.ext.android.applicationId
minSdkVersion rootProject.ext.android.minSdkVersion
targetSdkVersion rootProject.ext.android.targetSdkVersion
versionCode rootProject.ext.android.versionCode
versionName rootProject.ext.android.versionName
multiDexEnabled = true
vectorDrawables.useSupportLibrary = true
//注解处理器
javaCompileOptions {
annotationProcessorOptions {
arguments = [moduleName: project.getName()]
}
}
}
//执行lint检查,有任何的错误或者警告提示,都会终止构建,我们可以将其关掉。
lintOptions {
abortOnError false
}
signingConfigs {
debug {
storeFile file("$rootDir/zcy.keystore")
storePassword "zcyghost"
keyAlias "zcy.keystore"
keyPassword "zcyghost"
}
release {
storeFile file("$rootDir/zcy.keystore")
storePassword "zcyghost"
keyAlias "zcy.keystore"
keyPassword "zcyghost"
}
}
buildTypes {
debug {
// 显示Log
buildConfigField "boolean", "LOG_DEBUG", "true"
versionNameSuffix "-debug"
minifyEnabled false
zipAlignEnabled false
shrinkResources false
signingConfig signingConfigs.debug
}
release {
// 不显示Log
buildConfigField "boolean", "LOG_DEBUG", "true"
//Zipalign优化
zipAlignEnabled true
// 移除无用的resource文件
shrinkResources false
//混淆
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
//签名
signingConfig signingConfigs.release
}
}
// applicationVariants.all { variant ->
// variant.outputs.each { output ->
// def outputFile = output.outputFile
// if (outputFile != null && outputFile.name.endsWith('.apk')) {
// def fileName = outputFile.name.replace(".apk", "-${defaultConfig.versionName}.apk")
// output.outputFile = new File(outputFile.parent, fileName)
// }
// }
// }
}
repositories {
maven { url "https://jitpack.io" }
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
//base
compile rootProject.ext.dependencies["appcompat-v7"]
//network
compile rootProject.ext.dependencies["retrofit"]
compile rootProject.ext.dependencies["retrofit-converter-gson"]
compile rootProject.ext.dependencies["retrofit-adapter-rxjava"]
compile rootProject.ext.dependencies["okhttp-logging-interceptor"]
compile rootProject.ext.dependencies["glide"]
compile rootProject.ext.dependencies["glide-transformations"]
//rx
compile rootProject.ext.dependencies["rxjava"]
compile rootProject.ext.dependencies["rxandroid"]
//ui
compile rootProject.ext.dependencies["easyrecyclerview"]
compile rootProject.ext.dependencies["rollviewpager"]
compile rootProject.ext.dependencies["smarttablayout"]
compile rootProject.ext.dependencies["smarttablayout:utils"]
compile rootProject.ext.dependencies["jiecaovideoplayer"]
compile rootProject.ext.dependencies["cardstack"]
compile rootProject.ext.dependencies["roundedimageview"]
compile rootProject.ext.dependencies["cardview"]
compile('com.github.afollestad.material-dialogs:core:0.8.5.4@aar') {
transitive = true
}
compile('com.github.afollestad.material-dialogs:commons:0.8.5.4@aar') {
transitive = true
}
compile rootProject.ext.dependencies["iconics-core"]
compile rootProject.ext.dependencies["material-design-iconic-typeface"]
compile rootProject.ext.dependencies["fontawesome-typeface"]
compile rootProject.ext.dependencies["foundation-icons-typeface"]
//di
compile rootProject.ext.dependencies["dagger"]
annotationProcessor rootProject.ext.dependencies["dagger-compiler"]
compile rootProject.ext.dependencies["butterknife"]
annotationProcessor rootProject.ext.dependencies["butterknife-compiler"]
provided rootProject.ext.dependencies["annotation"]
compile rootProject.ext.dependencies["annotations"]
//other
compile rootProject.ext.dependencies["logger"]
compile rootProject.ext.dependencies["fragmentation"]
compile rootProject.ext.dependencies["androideventbus"]
compile rootProject.ext.dependencies["pgyersdk"]
compile rootProject.ext.dependencies["slf4j"]
//canary
debugCompile rootProject.ext.dependencies["blockcanary-android"]
releaseCompile rootProject.ext.dependencies["blockcanary-no-op"]
debugCompile rootProject.ext.dependencies["leakcanary-android"]
releaseCompile rootProject.ext.dependencies["leakcanary-android-no-op"]
}
项目根目录下新建一个config.gradle文件
代码如下:
ext {
android = [
compileSdkVersion: 24,
buildToolsVersion: '26.0.2',
applicationId : "xxx.xxx",
minSdkVersion : 17,
targetSdkVersion : 24,
versionCode : 1,
versionName : "1.3.0"
]
def dependVersion = [
support : "24.0.0",
retrofit : "2.1.0",
okhttp : "3.3.1",
dagger2 : "2.0.2",
butterknife: "8.4.0",
blockcanary: "1.2.1",
leakcanary : "1.4-beta2"
]
dependencies = [
//base
"appcompat-v7" : "com.android.support:appcompat-v7:${dependVersion.support}",
"recyclerview-v7" : "com.android.support:recyclerview-v7:${dependVersion.support}",
"design" : "com.android.support:design:${dependVersion.support}",
//ui
"easyrecyclerview" : "com.jude:easyrecyclerview:4.0.6",
"rollviewpager" : "com.jude:rollviewpager:1.4.5",
"glide" : "com.github.bumptech.glide:glide:3.7.0",
"glide-transformations" : "jp.wasabeef:glide-transformations:2.0.1",
"smarttablayout" : "com.ogaclejapan.smarttablayout:library:1.6.1@aar",
"smarttablayout:utils" : "com.ogaclejapan.smarttablayout:utils-v4:1.6.1@aar",
"jiecaovideoplayer" : "fm.jiecao:jiecaovideoplayer:4.8.2",
"cardstack" : "com.daprlabs.aaron:cardstack:0.3.1-beta0",
"roundedimageview" : "com.makeramen:roundedimageview:2.2.1",
"cardview" : "com.android.support:cardview-v7:24.0.0",
"material-dialogs" : "com.github.afollestad.material-dialogs:core:0.8.5.4@aar",
"material-dialogs:commons" : "com.github.afollestad.material-dialogs:commons:0.8.5.4@aar",
"iconics-core" : "com.mikepenz:iconics-core:2.5.5@aar",
"material-design-iconic-typeface": "com.mikepenz:material-design-iconic-typeface:2.2.0.1@aar",
"fontawesome-typeface" : "com.mikepenz:fontawesome-typeface:4.5.0.1@aar",
"foundation-icons-typeface" : "com.mikepenz:foundation-icons-typeface:3.0.0.1@aar",
//rx
"rxjava" : "io.reactivex:rxjava:1.1.6",
"rxandroid" : "io.reactivex:rxandroid:1.2.1",
//network
"gson" : "com.google.code.gson:gson:2.7",
"retrofit" : "com.squareup.retrofit2:retrofit:${dependVersion.retrofit}",
"retrofit-converter-gson" : "com.squareup.retrofit2:converter-gson:${dependVersion.retrofit}",
"retrofit-adapter-rxjava" : "com.squareup.retrofit2:adapter-rxjava:${dependVersion.retrofit}",
"okhttp" : "com.squareup.okhttp3:okhttp:${dependVersion.okhttp}",
"okhttp-logging-interceptor" : "com.squareup.okhttp3:logging-interceptor:${dependVersion.okhttp}",
"glide" : "com.github.bumptech.glide:glide:3.7.0",
"glide-okhttp3-integration" : "com.github.bumptech.glide:okhttp3-integration:1.4.0@aar",
"jsoup" : "org.jsoup:jsoup:1.10.1",
//di
"dagger" : "com.google.dagger:dagger:${dependVersion.dagger2}",
"dagger-compiler" : "com.google.dagger:dagger-compiler:${dependVersion.dagger2}",
"butterknife" : "com.jakewharton:butterknife:${dependVersion.butterknife}",
"butterknife-compiler" : "com.jakewharton:butterknife-compiler:${dependVersion.butterknife}",
"annotation" : "org.glassfish:javax.annotation:10.0-b28",
//other
"logger" : "com.orhanobut:logger:1.15",
"fragmentation" : "me.yokeyword:fragmentation:0.7.10",
"androideventbus" : "org.simple:androideventbus:1.0.5.1",
"annotations" : "org.jetbrains:annotations-java5:15.0",
"pgyersdk" : "com.pgyersdk:sdk:2.4.0",
"slf4j" : "org.slf4j:slf4j-api:1.7.12",
//canary
"blockcanary-android" : "com.github.moduth:blockcanary-android:${dependVersion.blockcanary}",
"blockcanary-no-op" : "com.github.moduth:blockcanary-no-op:${dependVersion.blockcanary}",
"leakcanary-android" : "com.squareup.leakcanary:leakcanary-android:${dependVersion.leakcanary}",
"leakcanary-android-no-op" : "com.squareup.leakcanary:leakcanary-android-no-op:${dependVersion.leakcanary}"
]
}
项目的根目录的build.gradle
/ Top-level build file where you can add configuration options common to all sub-projects/modules.
apply from: "config.gradle" //注意:加上这句,才能生效
buildscript {
repositories {
jcenter()
mavenCentral()
google()
}
dependencies {
// classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:3.0.0'
// classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath "io.realm:realm-gradle-plugin:3.1.1"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven { url "https://raw.githubusercontent.com/Pgyer/mvn_repo_pgyer/master" }
google()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
按照以上方式进行配置即可!
参考自:https://www.jianshu.com/p/a092bcc3de7d 和http://blog.youkuaiyun.com/lsyz0021/article/details/54377150
https://www.jianshu.com/p/7c2b666d7302?utm_campaign=haruki&utm_content=note&utm_medium=reader_share&utm_source=qq