標籤:
需求:
1. 使用 Android studio ,使用 gradle 進行構建
2. 在實際開發中,我們需要使用jenkins進行打包。就需要配置我們的 gradle 指令碼以支援參數化的方式。
3. 想獲得一個可配置打包指令碼的方法,允許 配置人員根據需要修改 伺服器位址,versionCode, versionName 等
4. 隔離的原始碼的配置,使用者在 jenkins裡進行配置。
概述:
先展示我配置好的 參數,可以在命令提示行下執行,如下:
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
參數說明:
1. assembleBeta 其中 Beta是我配置好的 構建任務,
2. -P標示後面跟的內容是參數,比如:
-PVERSION_CODE_PARA=101 表示 傳入一個 VERSION_CODE_PARA 參數,它的值是 101
這裡的參數都是自訂的,我在這裡參入了多個參數,有 versionName,versionCode ,輸入檔案路徑,和 指定的伺服器位址。
實現:
修改versionCode和 versionName
上面的示範中,我們傳入了gradle的參數,如何在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 節點,讀取上面傳入的參數的值作為 versionCode或者 versionName。在讀取參數的時候,我們先檢查參數是否存在,使用代碼:
project.hasProperty(‘參數名‘)
所有通過命令列傳入的參數都或作為 project 內建對象的屬性,我們這裡判斷了 指定的參數名 是否存在。如何使用參數呢?直接使用即可,比如下面:
versionCode Integer.parseInt(VERSION_CODE_PARA) 注意這裡,進行了 轉型,從字串轉型為 int 類型
versionName VERSION_NAME_PARA
和普通的變數使用方法是一樣的。我們還會遇到在 字串中使用的時候,可以使用 運算式 來引用,比如:
${參數名}
樣本:
fileName = fileName.replace(".apk", "-${android.defaultConfig.versionName}.apk")
明白了變數(屬性,參數)的讀取方式,我們就可以像普通代碼那樣編碼了。我們繼續回到我們的主旨列來。我們需要 在 buildTypes 節點(任務)
下,添加一個 自訂的打包方式,比如 名稱叫做 beta 的配置。beta 是我自訂的,在開頭我們見過這個參數的使用,在 “gradle
assembleBeta ” 中的Beta就會調用這個我們配置好的任務,示範代碼如下:
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的 名稱和存放路徑
我們繼續配置 apk 輸出 的目錄的配置,這就需要獲得 編譯完成後的檔案名稱的配置,如何獲得和設定輸入路徑呢?代碼如下:
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檔案名稱增加 當前日期 部分
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 } } }}
我的整個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 } } }}
在 Android studio 中 配置Gradle 做到 “根據命令列提示符產生指定versionCode, versionName,指定apk的打包輸出路徑”