我們都知道android項(xiàng)目采用的是一套gradle構(gòu)建機(jī)制通過(guò)android studio警医,我們可以很方便的對(duì)gradle進(jìn)行配置從而靈活高效的完成項(xiàng)目的編譯和打包屁使。一般android的項(xiàng)目構(gòu)建配置如下:
從圖中我們可以看到,主要是.gradle文件和.properties文件恋昼。我們分別來(lái)看些配置文件的內(nèi)容和用途拼苍。
- 第一個(gè)是項(xiàng)目根目錄下的腳本配置文件来庭,用行話來(lái)說(shuō)就是rootProject的build.gradle。它的內(nèi)容如下:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
這里主要定義了android插件的版本號(hào)丧肴,以及你項(xiàng)目中一些依賴庫(kù)的倉(cāng)庫(kù)地址残揉。這里android插件的版本號(hào)一般跟開(kāi)發(fā)者本機(jī)所安裝的android studio插件版本號(hào)一致。所以在多人協(xié)作的時(shí)候芋浮,由于每個(gè)開(kāi)發(fā)人員所使用的android studio版本不一致抱环,而導(dǎo)致在使用持續(xù)集成工具打包時(shí)需要頻繁提交對(duì)該文件的修改,這樣會(huì)造成提交日志的冗余纸巷,因?yàn)檫@些提交并沒(méi)有包含多少有效信息镇草,同時(shí)每個(gè)開(kāi)發(fā)者在同步版本庫(kù)的配置時(shí)都要重新修改一次這個(gè)文件也比較繁瑣。
- 第二個(gè)是項(xiàng)目中一個(gè)模塊的腳本配置文件瘤旨,用行話來(lái)說(shuō)就是subProject的build.gradle梯啤。它的內(nèi)容如下:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.xiaofei.gradleconfigtest"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
這里主要定義了每個(gè)module的編譯和打包配置,比如compileSdkVersion定義了編譯項(xiàng)目使用的android版本存哲,targetSdkVersion定義了項(xiàng)目可以兼容的最高android系統(tǒng)版本因宇,implementation 'com.android.support:appcompat-v7:27.1.1'定義了該模塊依賴的support包的版本號(hào)。這里同樣會(huì)存在多人協(xié)作時(shí)配置不一致的情況宏胯,比如compileSdkVersion羽嫡,targetSdkVersion等。
- 第三個(gè)為gradle工具自身的一些配置肩袍,如位置杭棵,發(fā)布地址等。內(nèi)容如下:
#Sat May 19 19:44:18 CST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
該文件我們一般不會(huì)將它放到版本管理中
- 第四個(gè)為項(xiàng)目混淆配置文件氛赐,一般在對(duì)外發(fā)布app時(shí)為了防止應(yīng)用代碼被破解會(huì)使用proguard工具將項(xiàng)目代碼進(jìn)行混淆處理魂爪。
- 第五個(gè)為項(xiàng)目的屬性配置文件,主要是對(duì)一些內(nèi)置屬性進(jìn)行配置艰管,如jvm參數(shù)等滓侍。內(nèi)容如下:
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
- 第六個(gè)為項(xiàng)目的腳本配置文件,主要是在gradle構(gòu)建的初始化階段告訴gradle該項(xiàng)目包含多少個(gè)module牲芋。內(nèi)容如下:
include ':app'
- 第七個(gè)為項(xiàng)目的屬性配置文件撩笆,一般為android sdk的路徑捺球,ndk的路徑等。內(nèi)容如下:
## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file should *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=C\:\\Users\\Administrator\\AppData\\Local\\Android\\Sdk
由于build.gradle文件中一些配置在多人協(xié)作時(shí)會(huì)被頻繁修改夕冲,所以我們需要將這些配置提取出來(lái)使得每個(gè)開(kāi)發(fā)者的修改不影響到版本庫(kù)中的配置氮兵,有以下兩種方法:
- 將這些配置,如android插件版本(com.android.tools.build:gradle:3.1.2)歹鱼,compileSdkVersion等泣栈,放到gradle.properties中。為什么要放到gradle.properties中弥姻,因?yàn)樵撐募械呐渲迷陧?xiàng)目構(gòu)建時(shí)會(huì)被自動(dòng)加載南片,而且我們并不會(huì)將gradle.properties放到版本管理中,可以隨意修改而不影響其他開(kāi)發(fā)者庭敦。具體做法參考如下:
項(xiàng)目根目錄的build.gradle文件
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
def ver = '3.1.2'//版本庫(kù)中的默認(rèn)配置疼进,一般只有管理員才能修改
if (hasProperty('androidPluginVer')) {//如果配置文件中定義了該屬性,則使用秧廉,否則使用默認(rèn)配置
ver = getProperty('androidPluginVer')
}
classpath "com.android.tools.build:gradle:$ver"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
模塊的build.gradle文件
apply plugin: 'com.android.application'
android {
def sdkVer = 26 //版本庫(kù)中的默認(rèn)配置颠悬,一般只有管理員才能修改
if (hasProperty('sdkVersion')) {//如果配置文件中定義了該屬性,則使用定血,否則使用默認(rèn)配置
sdkVer = getProperty('sdkVersion')
}
compileSdkVersion sdkVer
defaultConfig {
applicationId "com.xiaofei.activityhide"
minSdkVersion 15
targetSdkVersion sdkVer
versionCode 1
versionName "1.0"
if (rootProject.hasProperty("developer")) {
versionName = developer
}
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
gradle.properties文件
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
#android插件版本號(hào)赔癌,一般與android studio版本號(hào)一致
androidPluginVer=3.1.2
#編譯sdk版本號(hào)
sdkVersion=26
- 將這些配置放到一個(gè)單獨(dú)的配置文件中,如me.properties中澜沟,該文件不放到版本管理中灾票,然后在項(xiàng)目根目錄的build.gradle中加載該屬性文件從里面取出配置。該做法和第一種做法的區(qū)別是修改me.properites之后android studio工具不會(huì)強(qiáng)制要求你做一次同步操作(sync project with gradle files)茫虽。具體參考做法如下:
項(xiàng)目根目錄的build.gradle文件
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:$androidPluginVer"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
ext {
loadPropertyFile = { name ->
Properties properties = new Properties()
InputStream is = null
try {
is = new FileInputStream(name)
properties.load(is)
} catch (Throwable tr) {
tr.printStackTrace()
println tr.getMessage()
} finally {
if (is != null) {
try {
is.close()
} catch (Throwable tr) {
tr.printStackTrace()
}
}
}
return properties
}
Properties myProperties = loadPropertyFile('me.properties')
androidPluginVer = '3.1.2'//版本庫(kù)中的默認(rèn)配置刊苍,一般只有管理員才能修改
if (myProperties.hasProperty('androidPluginVer')) {//如果配置文件中定義了該屬性,則使用濒析,否則使用默認(rèn)配置
androidPluginVer = myProperties.getProperty('androidPluginVer')
}
}
me.properties文件正什,放在項(xiàng)目根目錄下。
#android插件版本號(hào)号杏,一般與android studio版本號(hào)一致
androidPluginVer=3.1.2
通過(guò)以上兩種方法就可以靈活的管理自己的個(gè)人配置婴氮,而不再擔(dān)心與版本庫(kù)的配置發(fā)生沖突啦。