前言

前几天看Android-CleanArchitecture 时候,注意到将构建方式进行了统一抽取,很实用,看着自己项目中构建方式还是使用系统默认写法,瞬间感觉 很low。接下来开始改造 !!!

​Android-CleanArchitecture 入口​

如下采用系统默认方式配置:

android {
compileSdkVersion 23
buildToolsVersion '25.0.0'
defaultConfig {
applicationId "com.nuoyuan.preone"
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:appcompat-v7:22.2.0'
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.amitshekhar.android:debug-db:1.0.0'
testCompile 'junit:junit:4.12'
}

一个module 一个配置文件,处理起来没有问题,一旦多个module,会发生什么?

重复引用异常,编译异常,维护配置文件成了噩梦,稍微的版本更改就会带来一些编译问题……

采用系统默认构建方式的 缺点:

1.项目中多个模块的build.gradle配置的依赖版本重复或compileSdkVersion等不统一;
2.会导致一些依赖重复导致冲突;
3.Android Support Library版本问题引发的一些列问题;
4.影响 build.gradle 文件扩展性、可读性、以及难以维护等;
5.针对以上问题,去寻找一个更清晰的打包系统,帮助开发者在组织打包脚本变得更简洁清晰

gradle build 优化

 方式一

抽取统一的依赖
在根目录新建一个config.gradle文件,里面键入要统一的依赖:

ext {
android = [
compileSdkVersion: 23,
buildToolsVersion: "23.0.3",
minSdkVersion : 15,
targetSdkVersion : 22,
versionCode : 1,
versionName : "1.0"
]

dependencies = [
"gson" : "com.google.code.gson:gson:2.6.2",
"eventbus" : 'org.greenrobot:eventbus:3.0.0',
"butterknife" : 'com.jakewharton:butterknife:7.0.1',
"support-design" : 'com.android.support:design:24.1.1',
"support-appcompatV7": 'com.android.support:appcompat-v7:24.1.1',
"support-percent" : 'com.android.support:percent:24.1.1',
"support-multidex" : 'com.android.support:multidex:1.0.1',
"glide" : 'com.github.bumptech.glide:glide:3.7.0',
"support-v4" : 'com.android.support:support-v4:24.1.1',
"okhttp3" : 'com.squareup.okhttp3:okhttp:3.3.1',
"nineoldandroids" : 'com.nineoldandroids:library:2.4.0'

]
}

然后在根目录的build.gradle文件里面头部新增一句引用

apply from: "config.gradle"

在module里面开始应用,下面是两种引用方式:

compileSdkVersion rootProject.ext.android.compileSdkVersion //android{}节点

compile rootProject.ext.dependencies["support-appcompatV7"] //dependencies{}节点

clean一下去External Libraries看看,是不是还有重复的,如果还有,说明前面config里面的依赖其他地方还有遗漏的,全局搜索一下在同样方式替换一下就好了。

方式二 (推荐)

此方式参照开源架构 Android-CleanArchitecture中的打包组织架构。根据功能的不同,将打包系统分为多个脚本文件。

打包系统的组织结构如下:

android gradle 优化构建系统_ide

jks,market 请自动屏蔽

ci.gradle 文件

ci.gradle 使用与否属于可选,处理muti-dex 时候用到。请自行选择是否加入该配置

def ciServer = 'TRAVIS'
def executingOnCI = "true".equals(System.getenv(ciServer))

// Since for CI we always do full clean builds, we don't want to pre-dex
// See http://tools.android.com/tech-docs/new-build-system/tips
subprojects {
project.plugins.whenPluginAdded { plugin ->
if ('com.android.build.gradle.AppPlugin'.equals(plugin.class.name) || 'com.android.build.gradle.LibraryPlugin'.equals(plugin.class.name)) {
project.android.dexOptions.preDexLibraries = !executingOnCI
}
}
}
config.gradle

里面键入要统一的依赖

allprojects {
repositories {
jcenter()
}
}

ext {
//Android
androidBuildToolsVersion = "25.0.2"
androidMinSdkVersion = 14
androidTargetSdkVersion = 25
androidCompileSdkVersion = 25
androidVersionCode = 1
androidVersionName = "0.4"

//Libraries
gsonVersion = '2.6.2'
supportv7Version = '25.0.0'
supportv4Version = '25.0.0'
recyclerViewV7Version = '25.0.0'
glideVersion = '3.7.0'
okHttpVersion = '3.8.1'
retrofit2Version = '2.3.0'
ConverterGsonVersion = '2.3.0'
adapterRxjavaVersion = '2.3.0'
rxAndroidVersion = '1.2.0'
rxpermissionsVersion = '0.9.4'
greendaoVersion = '3.2.0'
debugDbVersion = '1.0.0'
eventbusVersion = '3.0.0'
eventbusAnnotationProcessorVersion = '3.0.1'
PhotoViewVersion = '1.2.4'

//Testing
robolectricVersion = '3.0'
jUnitVersion = '4.12'
mockitoCoreVersion = '2.8.47'
mockitoAndroidVersion = '2.8.47'
espressoVersion = '2.2.2'

//Development
leakcanaryAndroidVersion = '1.5.1'

frameDependencies = [
supportV4 : "com.android.support:support-v4:${supportv4Version}",
supportV7 : "com.android.support:appcompat-v7:${supportv7Version}",
recyclerview : "com.android.support:recyclerview-v7:${recyclerViewV7Version}",
glide : "com.github.bumptech.glide:glide:${glideVersion}",
okhttp : "com.squareup.okhttp3:okhttp:${okHttpVersion}",
retrofit : "com.squareup.retrofit2:retrofit:${retrofit2Version}",
convertergson: "com.squareup.retrofit2:converter-gson:${ConverterGsonVersion}",
adapterrxjava: "com.squareup.retrofit2:adapter-rxjava:${adapterRxjavaVersion}",
rxandroid : "io.reactivex:rxandroid:${rxAndroidVersion}",
rxpermissions: "com.tbruyelle.rxpermissions:rxpermissions:${rxpermissionsVersion}",
]

frameTestDependencies = [
junit4 : "junit:junit:${jUnitVersion}",
espresso: "com.android.support.test.espresso:espresso-core:${espressoVersion}",
]




njsDependencies = [
gson : "com.google.code.gson:gson:${gsonVersion}",
eventbus : "org.greenrobot:eventbus:${eventbusVersion}",
debugDb : "com.amitshekhar.android:debug-db:${debugDbVersion}",
greendao : "org.greenrobot:greendao:${greendaoVersion}",
eventbusannotation: "org.greenrobot:eventbus-annotation-processor:${eventbusAnnotationProcessorVersion}",
PhotoView : "com.commit451:PhotoView:${PhotoViewVersion}",
leakcanary : "com.squareup.leakcanary:leakcanary-android:${leakcanaryAndroidVersion}",
supportv7 : "com.android.support:appcompat-v7:${supportv7Version}",
]


njsTestDependencies = [
mockitoandroid: "org.mockito:mockito-android:${mockitoAndroidVersion}",
mockitocore : "org.mockito:mockito-core:${mockitoCoreVersion}",
junit4 : "junit:junit:${jUnitVersion}",
espresso : "com.android.support.test.espresso:espresso-core:${espressoVersion}",
]

developmentDependencies = [
leakCanary: "com.squareup.leakcanary:leakcanary-android:${leakcanaryAndroidVersion}",
]

}
//compileSdkVersion rootProject.ext.android.compileSdkVersion //android{}节点

//compile rootProject.ext.dependencies["support-appcompatV7"] //dependencies{}节点

项目 project 目录下 build.gradle 中开头加入

apply from: 'buildsystem/config.gradle'
apply from: 'buildsystem/ci.gradle'

多个模块 module 目录下的 build.gradle 中进行相应替换

dependencies {

def njsDependencies = rootProject.ext.njsDependencies
def njsTestDependencies = rootProject.ext.njsTestDependencies
def developmentDependencies = rootProject.ext.developmentDependencies

compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile(njsTestDependencies.espresso, {
exclude group: 'com.android.support', module: 'support-annotations'
})

testCompile njsTestDependencies.junit4
compile njsDependencies.supportv7
compile project(':frame')
compile njsDependencies.greendao
//compile njsDependencies.debugDb
compile njsDependencies.eventbus
annotationProcessor njsDependencies.eventbusannotation
compile njsDependencies.PhotoView
//compile developmentDependencies.leakCanary;
testCompile njsTestDependencies.mockitoandroid
testCompile njsTestDependencies.mockitocore
androidTestCompile njsTestDependencies.mockitocore
}

clean project 或者 build project,看是否有遗漏。

引用:

Android-CleanArchitecture ​​https://github.com/android10/Android-CleanArchitecture​