Requirements:
1. Build with Android Studio and Gradle 2. In actual development, we need to use Jenkins for packaging. We need to configure our Gradle scripts to support parameterization. 3. Want to get a configurable package script method that allows the configuration personnel to change the server address, versionCode, versionName, etc. 4. Isolated source code configuration, user configuration in Jenkins.
Summary:
Show me the parameters I have configured, which can be executed at the command prompt, as follows:
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 AssembleBeta, where Beta is my configured build task, 2. -P indicates that the following content is the parameter, such as: -PVERSION_CODE_PARA=101 Indicates that a VERSION_CODE_PARA parameter is passed with a value of 101Copy the code
The parameters here are all custom, I entered multiple parameters here, including versionName,versionCode, input file path, and the specified server address.
Implementation:
Change the versionCode and versionName
In this example, we passed in gradle parameters. How do we use them in Gradle? Here is my code for configuring versionCode and versionName as shown in the following example:
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 } }Copy the code
We need to configure the defaultConfig node to read the value of the parameter passed above as versionCode or versionName. When reading a parameter, we first check if the parameter exists, using this code:
Project.hasproperty (' parameter name ')Copy the code
All arguments passed through the command line are either properties of the project built-in object, and we check whether the specified parameter name exists. How do you use parameters? Use it directly, for example:
VersionCode Integer.parseInt(VERSION_CODE_PARA) Notice that a transformation has been made from a string to an int versionName VERSION_NAME_PARACopy the code
It’s the same as normal variables. We will also encounter situations where we can use expressions to refer to strings, such as:
${parameter name}Copy the code
Example:
fileName = fileName.replace(".apk", "-${android.defaultConfig.versionName}.apk")
Copy the code
Knowing how variables (attributes, parameters) are read, we can code like normal code. Let’s go back to our subject line. We need to add a custom packaging under the buildTypes node (task), such as a configuration named beta. Beta is my custom. We saw the use of this parameter at the beginning. The beta in “gradle assembleBeta” will call the task we have configured.
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")
}
}
Copy the code
Controls the name and location of the APK output
We continue to configure the apK output directory configuration, this needs to obtain the compiled file name configuration, how to obtain and set the input path? The code is as follows:
android.applicationVariants.all { variant -> variant.outputs.each { output ->{ ....... }}Copy the code
I want to add the versionName to the output APK file name, writing the code:
if (android.defaultConfig.versionName ! = null) { fileName = fileName.replace(".apk", "-${android.defaultConfig.versionName}.apk") }Copy the code
Adds the specified suffix to the entered APK file name
if (project.hasProperty('OUT_PUT_APK_SUFFIX_PARA')) {
fileName = fileName.replace(".apk", "-${OUT_PUT_APK_SUFFIX_PARA}.apk")
}
Copy the code
Adds the current date part to the output APK file name
def today = new Date().format('yyMMddHHmm');
fileName = fileName.replace(".apk", "-${today}.apk")
Copy the code
I also want to specify the directory where APK is stored:
if (project.hasProperty('OUT_PUT_DIR_PARA')) { File output_dir1 = file("${OUT_PUT_DIR_PARA}"); OutputFile = new File(output_dir1, fileName) println "outputFile location:" + output.outputFile //}}Copy the code
Example code for this section is as follows:
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}"); OutputFile = new File(output_dir1, fileName) println" "+ output.outputFile //}} else {output.outputFile = new File(outputFile. Parent, fileName) println" " + output.outputFile } } }Copy the code
}
My entire Gradle script,build.gradle file is as follows: 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}"); OutputFile = new File(output_dir1, fileName) println" "+ output.outputFile //}} else {output.outputFile = new File(outputFile. Parent, fileName) println" " + output.outputFile } } } }Copy the code