需求:
1. 使用 Android studio ,使用 gradle 進(jìn)行構(gòu)建
2. 在實(shí)際開發(fā)中,我們需要使用jenkins進(jìn)行打包。就需要配置我們的 gradle 腳本以支持參數(shù)化的方式。
3. 想獲得一個(gè)可配置打包腳本的方法,允許 配置人員根據(jù)需要修改 服務(wù)器地址,versionCode, versionName 等
4. 隔離的源代碼的配置,使用者在 jenkins里進(jìn)行配置。
概述:
先展示我配置好的 參數(shù),可以在命令提示行下執(zhí)行,如下:
gradle assembleBeta -PVERSION_CODE_PARA=101 -PVERSION_NAME_PARA=fd21.0 -POUT_PUT_DIR_PARA=/Users/zhangyunfei/Desktop/yyy -PAPI_HOST_USER_PARA=http://10.0.1.245:9000 -PAPI_HOST_CABZOO_PARA=http://10.0.1.245:9002 -POUT_PUT_APK_SUFFIX_PARA=245
參數(shù)說明:
1. assembleBeta 其中 Beta是我配置好的 構(gòu)建任務(wù),
2. -P標(biāo)示后面跟的內(nèi)容是參數(shù),比如:
-PVERSION_CODE_PARA=101 表示 傳入一個(gè) VERSION_CODE_PARA 參數(shù),它的值是 101
這里的參數(shù)都是自定義的,我在這里參入了多個(gè)參數(shù),有 versionName,versionCode ,輸入文件路徑,和 指定的服務(wù)器地址。
實(shí)現(xiàn):
修改versionCode和 versionName
上面的演示中,我們傳入了gradle的參數(shù),如何在gradle中使用呢? 下面是我配置 versionCode和 versionName 的代碼,示例如下:
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
signingConfig signingConfigs.debug
buildConfigField("String", "API_HOST_USER", "\"http://10.0.1.232:9000\"")
buildConfigField("String", "API_HOST_CABZOO", "\"http://10.0.1.232:9002\"")
buildConfigField("Boolean", "IS_CHECK_VERSION_UPDATE", "true")
if (project.hasProperty('VERSION_CODE_PARA')) {
versionCode Integer.parseInt(VERSION_CODE_PARA)
}
if (project.hasProperty('VERSION_NAME_PARA')) {
versionName VERSION_NAME_PARA
}
}
我們需要配置 defaultConfig 節(jié)點(diǎn),讀取上面?zhèn)魅氲膮?shù)的值作為 versionCode或者 versionName。在讀取參數(shù)的時(shí)候,我們先檢查參數(shù)是否存在,使用代碼:
project.hasProperty('參數(shù)名')
所有通過命令行傳入的參數(shù)都或作為 project 內(nèi)建對(duì)象的屬性,我們這里判斷了 指定的參數(shù)名 是否存在。如何使用參數(shù)呢?直接使用即可,比如下面:
versionCode Integer.parseInt(VERSION_CODE_PARA) 注意這里,進(jìn)行了 轉(zhuǎn)型,從字符串轉(zhuǎn)型為 int 類型
versionName VERSION_NAME_PARA
和普通的變量使用方法是一樣的。我們還會(huì)遇到在 字符串中使用的時(shí)候,可以使用 表達(dá)式 來引用,比如:
${參數(shù)名}
示例:
fileName = fileName.replace(".apk", "-${android.defaultConfig.versionName}.apk")
明白了變量(屬性,參數(shù))的讀取方式,我們就可以像普通代碼那樣編碼了。我們繼續(xù)回到我們的主題行來。我們需要 在 buildTypes 節(jié)點(diǎn)(任務(wù))下,添加一個(gè) 自定義的打包方式,比如 名稱叫做 beta 的配置。beta 是我自定義的,在開頭我們見過這個(gè)參數(shù)的使用,在 “gradle assembleBeta ” 中的Beta就會(huì)調(diào)用這個(gè)我們配置好的任務(wù),演示代碼如下:
if (project.hasProperty('API_HOST_USER_PARA') && project.hasProperty('API_HOST_CABZOO_PARA')) {
beta {
debuggable true
signingConfig signingConfigs.debug
minifyEnabled false
buildConfigField("String", "API_HOST_USER", "\"" + API_HOST_USER_PARA + "\"")
buildConfigField("String", "API_HOST_CABZOO", "\"" + API_HOST_CABZOO_PARA + "\"")
buildConfigField("Boolean", "IS_CHECK_VERSION_UPDATE", "false")
}
}
控制輸出的APK的 名稱和存放路徑
我們繼續(xù)配置 apk 輸出 的目錄的配置,這就需要獲得 編譯完成后的文件名稱的配置,如何獲得和設(shè)置輸入路徑呢?代碼如下:
android.applicationVariants.all { variant ->
variant.outputs.each { output ->{
.......
}
}
我想在輸出的 apk 文件名中添加 版本名稱(versionName),寫下代碼:
if (android.defaultConfig.versionName != null) {
fileName = fileName.replace(".apk", "-${android.defaultConfig.versionName}.apk")
}
為輸入的apk文件名增加指定的后綴
if (project.hasProperty('OUT_PUT_APK_SUFFIX_PARA')) {
fileName = fileName.replace(".apk", "-${OUT_PUT_APK_SUFFIX_PARA}.apk")
}
為輸出的apk文件名增加 當(dāng)前日期 部分
def today = new Date().format('yyMMddHHmm');
fileName = fileName.replace(".apk", "-${today}.apk")
我還想指定 apk的存放 目錄:
if (project.hasProperty('OUT_PUT_DIR_PARA')) {
File output_dir1 = file("${OUT_PUT_DIR_PARA}");
output.outputFile = new File(output_dir1, fileName)
println "輸出文件位置: " + output.outputFile
//}
}
這部分的示例代碼如下:
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = outputFile.name;
if (android.defaultConfig.versionName != null) {
fileName = fileName.replace(".apk", "-${android.defaultConfig.versionName}.apk")
}
if (project.hasProperty('OUT_PUT_APK_SUFFIX_PARA')) {
fileName = fileName.replace(".apk", "-${OUT_PUT_APK_SUFFIX_PARA}.apk")
}
def today = new Date().format('yyMMddHHmm');
fileName = fileName.replace(".apk", "-${today}.apk")
if (project.hasProperty('OUT_PUT_DIR_PARA')) {
File output_dir1 = file("${OUT_PUT_DIR_PARA}");
output.outputFile = new File(output_dir1, fileName)
println "輸出文件位置: " + output.outputFile
//}
} else {
output.outputFile = new File(outputFile.parent, fileName)
println "輸出文件位置: " + output.outputFile
}
}
}
}
我的整個(gè)gradle 腳本,build.gradle 文件如下:
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':jlb_common')
compile project(':JlbLibUmeng')
compile project(':zyf.util.bluetoothprinter')
}
android {
signingConfigs {
release {
keyAlias 'jlb.scanner.apk'
keyPassword ' '
storeFile file(' ')
storePassword ' '
}
debug {
keyAlias 'androiddebugkey'
keyPassword ' '
storeFile file(' ')
storePassword ' '
}
}
compileSdkVersion 19
buildToolsVersion "22.0.1"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
buildTypes {
debug {
signingConfig signingConfigs.debug
buildConfigField("String", "API_HOST_USER", "\"http://10.0.1.232:9000\"")
buildConfigField("String", "API_HOST_CABZOO", "\"http://10.0.1.232:9002\"")
buildConfigField("Boolean", "IS_CHECK_VERSION_UPDATE", "false")
}
release {
minifyEnabled true
// 混淆文件的位置
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
signingConfig signingConfigs.release
buildConfigField("String", "API_HOST_USER", "\"http://uc.jinlinbao.com\"")
buildConfigField("String", "API_HOST_CABZOO", "\"http://cabzoo.jinlinbao.com\"")
buildConfigField("Boolean", "IS_CHECK_VERSION_UPDATE", "true")
}
// product_245 {
// debuggable true
// signingConfig signingConfigs.debug
// minifyEnabled false
// buildConfigField("String", "API_HOST_USER", "\"http://10.0.1.245:9000\"")
// buildConfigField("String", "API_HOST_CABZOO", "\"http://10.0.1.245:9002\"")
// }
if (project.hasProperty('API_HOST_USER_PARA') && project.hasProperty('API_HOST_CABZOO_PARA')) {
beta {
debuggable true
signingConfig signingConfigs.debug
minifyEnabled false
buildConfigField("String", "API_HOST_USER", "\"" + API_HOST_USER_PARA + "\"")
buildConfigField("String", "API_HOST_CABZOO", "\"" + API_HOST_CABZOO_PARA + "\"")
buildConfigField("Boolean", "IS_CHECK_VERSION_UPDATE", "false")
}
}
}
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
signingConfig signingConfigs.debug
buildConfigField("String", "API_HOST_USER", "\"http://10.0.1.232:9000\"")
buildConfigField("String", "API_HOST_CABZOO", "\"http://10.0.1.232:9002\"")
buildConfigField("Boolean", "IS_CHECK_VERSION_UPDATE", "true")
if (project.hasProperty('VERSION_CODE_PARA')) {
versionCode Integer.parseInt(VERSION_CODE_PARA)
}
if (project.hasProperty('VERSION_NAME_PARA')) {
versionName VERSION_NAME_PARA
}
}
productFlavors {
}
}
android {
lintOptions {
abortOnError false
}
}
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = outputFile.name;
if (android.defaultConfig.versionName != null) {
fileName = fileName.replace(".apk", "-${android.defaultConfig.versionName}.apk")
}
if (project.hasProperty('OUT_PUT_APK_SUFFIX_PARA')) {
fileName = fileName.replace(".apk", "-${OUT_PUT_APK_SUFFIX_PARA}.apk")
}
def today = new Date().format('yyMMddHHmm');
fileName = fileName.replace(".apk", "-${today}.apk")
if (project.hasProperty('OUT_PUT_DIR_PARA')) {
File output_dir1 = file("${OUT_PUT_DIR_PARA}");
output.outputFile = new File(output_dir1, fileName)
println "輸出文件位置: " + output.outputFile
//}
} else {
output.outputFile = new File(outputFile.parent, fileName)
println "輸出文件位置: " + output.outputFile
}
}
}
}