Demand:
1. Using Android Studio, build with Gradle
2. In actual development, we need to use Jenkins for packaging. We need to configure our Gradle script to support parameterization.
3. Want to obtain a configurable package script method, allow the configuration personnel to modify the server address as needed, versioncode, versionname, etc.
4. The configuration of the isolated source code, the user is configured in Jenkins.
Overview:
First show the parameters I 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
Parameter description:
2. The-P flag followed by the parameters, such as:
-pversion_code_para=101 means passing in a Version_code_para parameter whose value is 101
The parameters here are all custom, I am here to participate in a number of parameters, there is versionname,versioncode, input file path, and the specified server address.
Realize:
Modify Versioncode and Versionname
In the above demo, we passed in the Gradle parameter, how to use it in Gradle? Here is the code I configured for Versioncode and Versionname, as shown in the following example:
defaultconfig {minsdkversion 14 tar Getsdkversion 19 Signingconfig Signingconfigs.debug Buildconfigfield ("/ span> "String", "Api_host_user", "\" Http://10.0.1.232:9000\ "" ) Buildconfigfield ("/ span> "String", "Api_host_cabzoo", "\" http://10.0.1.232:9002\ "" ) Buildconfigfield ("/ span> "Boolean", "Is_check_version_update", "true" ) if (Project.hasproperty (' Version_code_para ' if (Project.hasproperty (' version_name_p ARA '
All arguments passed through the command line or as properties of the project's built-in object determine whether the specified parameter name exists. How do I use parameters? You can use it directly, such as the following:
Versioncode integer.parseint (Version_code_para) note here, the transformation from string to int type
Use the same method as the normal variable. We also encounter the use of strings, which can be referenced using expressions, such as:
${parameter name}
Example:
FileName = Filename.replace (". apk", "-${android.defaultconfig.versionname}. apk")
By understanding the way variables (attributes, parameters) are read, we can encode them like normal code. Let's go back to our subject line. We need the Buildtypes node (Task)
Assemblebeta "Beta will call this our configured task, the demo code is as follows:
if (Project.hasproperty (' Api_host_user_para ') && project.hasproperty (' Api_host_cabzoo_para ')) { beta { true signingconfig signingconfigs.debug 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 ") } }
The name and storage path of the APK that controls the output
We continue to configure the directory of the APK output, which requires the configuration of the file name after compilation, how to get and set the input path? The code is as follows:
Android.applicationVariants.all {Variant -----... }}
I want to add the version name (versionname) to the output APK file name and write down the code:
if NULL { = Filename.replace (". apk", "-${android.defaultconfig.versionname}.apk") }
Adds the specified suffix to the APK file name you entered
if (Project.hasproperty (' Out_put_apk_suffix_para ')) { = Filename.replace (". APK", "-${out_put_apk_ suffix_para}.apk ") }
Increase the current date portion of the APK file name for the output
New Date (). Format (' yymmddhhmm '); = Filename.replace (". apk", "-${today}.apk")
I also want to specify the APK storage directory:
if (Project.hasproperty (' Out_put_dir_para ')) { = file ("${out_put_dir_para}"); New File (Output_dir1, fileName) " Output file Location: "+ output.outputfile //}
The sample code for this section is as follows:
Android.applicationVariants.all {variant-Variant.outputs.each {Output-def OutputFile=Output.outputfileif(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=NewDate (). 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=NewFile (Output_dir1, fileName) println"Output File location:" +Output.outputfile//}}Else{output.outputfile=NewFile (outputfile.parent, fileName) println"Output File location:" +output.outputfile} }}}
My entire Gradle script, the 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‘ ‘}} compilesdkversion19buildtoolsversion"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 {minifyenabledtrue //confusing the location of a fileProguardfiles 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 {debuggabletruesigningconfig signingconfigs.debug minifyenabledfalseBuildconfigfield ("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 {minsdkversion14targetsdkversion19signingconfig 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 { Abortonerrorfalse}}android.applicationvariants.all {Variant-Variant.outputs.each {Output-def OutputFile=Output.outputfileif(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=NewDate (). 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=NewFile (Output_dir1, fileName) println"Output File location:" +Output.outputfile//}}Else{output.outputfile=NewFile (outputfile.parent, fileName) println"Output File location:" +output.outputfile} }}}
Configure Gradle in Android studio to "generate the specified Versioncode from the command line prompt, Versionname, specify the package output path of the APK"