Configure Gradle in Android studio to "generate the specified Versioncode from the command line prompt, versionname, Specify the package output path of the apk"

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size: 18pt;"><strong>Demand:</strong></span></p></p><p><p>1. Using Android studio, build with Gradle</p></p><p><p>2. In actual development, we need to use Jenkins for Packaging. We need to configure our Gradle script to support Parameterization.</p></p><p><p>3. Want to obtain a configurable package script method, allow the configuration personnel to modify the server address as needed, versioncode, versionname, etc.</p></p><p><p>4. The configuration of the isolated source code, the user is configured in Jenkins.</p></p><p><p></p></p><p><p><strong><span style="font-size: 18pt;">Overview:</span></strong></p></p><p><p>First show the parameters I configured, which can be executed at the command prompt, as Follows:</p></p><pre><pre>Gradle assemblebeta-pversion_code_para=101-pversion_name_para=fd21.0-pout_put_dir_para=/users/zhangyunfei/ desktop/yyy-papi_host_user_para=http:<span style="color: #008000;">//</span><span style="color: #008000;">10.0.1.245:9000-papi_host_cabzoo_para=</span>/http//<span style="color: #008000; text-decoration: underline;"> 10.0.1.245</span><span style="color: #008000;">: 9002-pout_put_apk_suffix_para=245</span></pre></pre><pre><pre>Parameter description:<br><br>2. The-p flag followed by the parameters, such as:</pre></pre><pre><pre>-pversion_code_para=101 means passing in a Version_code_para parameter whose value is 101<br><br>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.<br><br><br><strong><span style="font-size: 18pt;">Realize:</span></strong><br><br><span style="font-size: 14pt;"><strong>Modify Versioncode and Versionname</strong></span></pre></pre><pre><pre><br>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:</pre></pre><pre><pre><span>Defaultconfig { minsdkversion<span> targetsdkversion<span> signingconfig signingconfigs.debug Buildconfigfield ("string", "api_host_user", "\" http://10.0.1.232:9000\ ""<span>) Buildconfigfield ("string", "API _host_cabzoo "," \ "http://10.0.1.232:9002\" "<span>) Buildconfigfield (" Boolean "," is_check_version_update "," True "<span><strong>if (project.hasproperty (' Version_code_para '<span>)) {versioncode integer.parseint (version_code_para )} if (project.hasproperty (' Version_name_para '</span></strong><span><strong>)) {versionname version_name_para}</strong> } </span></span></span></span></span></span></span></pre></pre><pre><pre></pre></pre><pre><pre><strong><br></strong>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:<br>  </pre></pre><pre><pre><strong>Versioncode integer.parseint (version_code_para) Note here, The transformation from string to int type</strong></pre></pre><pre><pre><span><strong><br></strong><br>Use the same method as the normal Variable. We also encounter the use of strings, which can be referenced using expressions, such as:<br>${parameter name}<br><strong>Example:</strong></span></pre></pre><pre><pre>FileName = Filename.replace (". apk", "<strong>-${android.defaultconfig.versionname}</strong>. Apk")<br><br>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 to add a custom packaging method under the Buildtypes node (task), such as a configuration named Beta. Beta is my custom, at the beginning we have seen the use of this parameter, in "gradle assemblebeta" in the beta will call this our configured task, the demo code is as Follows:<br><br></pre></pre><pre><pre> <span style="color: #0000ff;">if</span> (project.hasproperty (' Api_host_user_para ') && project.hasproperty (' api_host_cabzoo_para ')<span style="color: #000000;">) { Beta { </span><span style="color: #0000ff;">true</span><span style="color: #000000;"> signingconfig signingconfigs.debug </span><span style="color: #0000ff;"></span> false <span style="color: #000000;"> Buildconfigfield (</span>"String", "api_host_user", "\" "+ api_host_user_para +" \ ""<span style="color: #000000;">) Buildconfigfield ( </span>"String", "api_host_cabzoo", "\" "+ api_host_cabzoo_para +" \ ""<span style="color: #000000;">) Buildconfigfield (</span>"Boolean", " Is_check_version_update "," false "<span style="color: #000000;">) } }</span></pre></pre><p><p></p></p><p><p><span style="font-size: 14pt;"><strong>The name and storage path of the APK that controls the output</strong></span></p></p><p><p></p></p><p><p>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:</p></p><pre><pre>Android.applicationVariants.all {variant<span style="color: #000000;"> </span>-----...<span style="color: #000000;"> }}</span></pre></pre><p><p></p></p><p><p></p></p><p><p>I want to add the version name (versionname) to the output apk file name and write down the code:</p></p><pre><pre><span style="color: #0000ff;">if</span> <span style="color: #0000ff;">NULL</span> <span style="color: #000000;">{ </span>= filename.replace (". apk", "-${android.defaultconfig.versionname}.apk"<span style="color: #000000;">) }</span></pre></pre><p><p>Adds the specified suffix to the APK file name you entered</p></p><pre><pre><span style="color: #0000ff;">if</span> (project.hasproperty (' Out_put_apk_suffix_para '<span style="color: #000000;">)) { </span>= filename.replace (". APK", "-${out_put_apk_suffix _para}.apk "<span style="color: #000000;">) }</span></pre></pre><p><p>Increase the current date portion of the APK file name for the output</p></p><pre><pre><span style="color: #0000ff;">New</span> Date (). format (' yymmddhhmm '<span style="color: #000000;">); </span> = Filename.replace (". apk", "-${today}.apk")</pre></pre><p><p>I also want to specify the APK storage directory:</p></p><pre><pre><span style="color: #0000ff;">if</span> (project.hasproperty (' Out_put_dir_para ')<span style="color: #000000;">) { </span>= File ("${out_put_dir_para}")<span style="color: #000000;">; </span> <span style="color: #0000ff;">New</span> <span style="color: #000000;">File (output_dir1, fileName) </span>" Output file location: "+<span style="color: #000000;"> output.outputfile </span><span style="color: #008000;">//</span><span style="color: #008000;">}</span> </pre></pre><p><p>The sample code for this section is as Follows:</p></p><pre>Android.applicationVariants.all {variant-<span style="color: #000000;"><span style="color: #000000;">Variant.outputs.each {output</span></span>-<span style="color: #000000;"><span style="color: #000000;">def OutputFile</span></span>=<span style="color: #000000;"><span style="color: #000000;">Output.outputfile</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(outputFile! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>&& outputFile.name.endsWith ('. apk ')<span style="color: #000000;">) <span style="color: #000000;">{def FileName</span></span>=<span style="color: #000000;"><span style="color: #000000;">outputfile.name; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(android.defaultConfig.versionName! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {fileName</span></span>= Filename.replace (". apk", "-${android.defaultconfig.versionname}.apk"<span style="color: #000000;"><span style="color: #000000;">) } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(project.hasproperty (' Out_put_apk_suffix_para '<span style="color: #000000;">) <span style="color: #000000;">) {fileName</span></span>= Filename.replace (". apk", "-${out_put_apk_suffix_para}.apk"<span style="color: #000000;"><span style="color: #000000;">)} def today</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Date (). format (' yymmddhhmm '<span style="color: #000000;"><span style="color: #000000;">); FileName</span></span>= Filename.replace (". apk", "-${today}.apk"<span style="color: #000000;"><span style="color: #000000;">) </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(project.hasproperty (' Out_put_dir_para '<span style="color: #000000;">) <span style="color: #000000;">) {File Output_dir1</span></span>= File ("${out_put_dir_para}"<span style="color: #000000;"><span style="color: #000000;">); Output.outputfile</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">File (output_dir1, FileName) println</span></span>"output file location:" +<span style="color: #000000;"><span style="color: #000000;">Output.outputfile</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">}</span></span>}<span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{output.outputfile</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">File (outputfile.parent, FileName) println</span></span>"output file location:" +<span style="color: #000000;"><span style="color: #000000;">output.outputfile}</span> }}}</span></pre><p><p></p></p><pre><pre><span><strong><br>My entire gradle script, the Build.gradle file is as Follows:<br></strong></span></pre></pre><pre>Apply Plugin: ' Android '<span style="color: #000000;"><span style="color: #000000;">dependencies {compile filetree (dir:</span></span>' Libs ', include: ' *.jar '<span style="color: #000000;"><span style="color: #000000;">) Compile project (</span></span>': Jlb_common '<span style="color: #000000;"><span style="color: #000000;">) Compile project (</span></span>': Jlblibumeng '<span style="color: #000000;"><span style="color: #000000;">) Compile project (</span></span>': Zyf.util.bluetoothprinter '<span style="color: #000000;"><span style="color: #000000;">)}android {signingconfigs {release {keyalias</span></span>' jlb.scanner.apk '<span style="color: #000000;"><span style="color: #000000;">Keypassword</span></span>‘ ‘<span style="color: #000000;"><span style="color: #000000;">storefile file (</span></span>‘ ‘<span style="color: #000000;"><span style="color: #000000;">) Storepassword</span></span>‘ ‘<span style="color: #000000;"><span style="color: #000000;">} debug {keyalias</span></span>' Androiddebugkey '<span style="color: #000000;"><span style="color: #000000;">Keypassword</span></span>‘ ‘<span style="color: #000000;"><span style="color: #000000;">storefile file (</span></span>‘ ‘<span style="color: #000000;"><span style="color: #000000;">) Storepassword</span></span>‘ ‘<span style="color: #000000;"><span style="color: #000000;">}} compilesdkversion</span></span>19<span style="color: #000000;"><span style="color: #000000;">buildtoolsversion</span></span>"22.0.1"<span style="color: #000000;"><span style="color: #000000;">sourcesets {main {manifest.srcfile</span></span>' Androidmanifest.xml '<span style="color: #000000;"><span style="color: #000000;">Java.srcdirs</span></span>= [' src '<span style="color: #000000;"><span style="color: #000000;">] Resources.srcdirs</span></span>= [' src '<span style="color: #000000;"><span style="color: #000000;">] Aidl.srcdirs</span></span>= [' src '<span style="color: #000000;"><span style="color: #000000;">] Renderscript.srcdirs</span></span>= [' src '<span style="color: #000000;"><span style="color: #000000;">] Res.srcdirs</span></span>= [' Res '<span style="color: #000000;"><span style="color: #000000;">] Assets.srcdirs</span></span>= [' Assets '<span style="color: #000000;"><span style="color: #000000;">] } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Move The tests to tests/java, tests/res, etc</span> ...</span>Instrumenttest.setroot (' tests '<span style="color: #000000;"><span style="color: #000000;">) </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Move The build types to build-types/<type></span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">for <span style="color: #008000;">instance, build-types/debug/java, build-types/debug/androidmanifest.xml, ...</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">this <span style="color: #008000;">moves them out of them default location under src/<type>/... which</span> would</span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">conflict with src/being used by the main source Set. </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Adding New build types or product flavors should be accompanied</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">by <span style="color: #008000;">a similar Customization.</span></span>Debug.setroot (' Build-types/debug '<span style="color: #000000;"><span style="color: #000000;">) Release.setroot (</span></span>' Build-types/release '<span style="color: #000000;"><span style="color: #000000;">)} buildtypes {debug {signingconfig signingconfigs.debug Buildconfigfield (</span></span>"String", "api_host_user", "\" http://10.0.1.232:9000\ ""<span style="color: #000000;"><span style="color: #000000;">) Buildconfigfield (</span></span>"String", "api_host_cabzoo", "\" http://10.0.1.232:9002\ ""<span style="color: #000000;"><span style="color: #000000;">) Buildconfigfield (</span></span>"Boolean", "is_check_version_update", "false"<span style="color: #000000;"><span style="color: #000000;">)} Release {minifyenabled</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">confusing the location of a file</span></span>Proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-project.txt '<span style="color: #000000;"><span style="color: #000000;">signingconfig signingconfigs.release Buildconfigfield (</span></span>"String", "api_host_user", "\" http://uc.jinlinbao.com\ ""<span style="color: #000000;"><span style="color: #000000;">) Buildconfigfield (</span></span>"String", "api_host_cabzoo", "\" http://cabzoo.jinlinbao.com\ ""<span style="color: #000000;"><span style="color: #000000;">) Buildconfigfield (</span></span>"Boolean", "is_check_version_update", "true"<span style="color: #000000;"><span style="color: #000000;">) }</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">product_245 {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">debuggable True</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">signingconfig Signingconfigs.debug</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">minifyenabled false</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Buildconfigfield ("String", "api_host_user", "\"</span></span><span style="color: #008000; text-decoration: underline;"><span style="color: #008000; text-decoration: underline;">http://10.0.1.245</span></span><span style="color: #008000;"><span style="color: #008000;">: 9000\ "")</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Buildconfigfield ("String", "api_host_cabzoo", "\"</span></span><span style="color: #008000; text-decoration: underline;"><span style="color: #008000; text-decoration: underline;">http://10.0.1.245</span></span><span style="color: #008000;"><span style="color: #008000;">: 9002\ "")</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;"> }</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(project.hasproperty (' Api_host_user_para ') && project.hasproperty (' Api_host_cabzoo_para ')<span style="color: #000000;">) <span style="color: #000000;">{beta {debuggable</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">signingconfig signingconfigs.debug minifyenabled</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">Buildconfigfield (</span></span>"String", "api_host_user", "\" "+ Api_host_user_para +" \ ""<span style="color: #000000;"><span style="color: #000000;">) Buildconfigfield (</span></span>"String", "api_host_cabzoo", "\" "+ Api_host_cabzoo_para +" \ ""<span style="color: #000000;"><span style="color: #000000;">) Buildconfigfield (</span></span>"Boolean", "is_check_version_update", "false"<span style="color: #000000;">)}}} <span style="color: #000000;">defaultconfig {minsdkversion</span></span>14<span style="color: #000000;"><span style="color: #000000;">targetsdkversion</span></span>19<span style="color: #000000;"><span style="color: #000000;">signingconfig signingconfigs.debug Buildconfigfield (</span></span>"String", "api_host_user", "\" http://10.0.1.232:9000\ ""<span style="color: #000000;"><span style="color: #000000;">) Buildconfigfield (</span></span>"String", "api_host_cabzoo", "\" http://10.0.1.232:9002\ ""<span style="color: #000000;"><span style="color: #000000;">) Buildconfigfield (</span></span>"Boolean", "is_check_version_update", "true"<span style="color: #000000;"><span style="color: #000000;">) </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(project.hasproperty (' Version_code_para '<span style="color: #000000;">) <span style="color: #000000;">{versioncode integer.parseint (version_code_para)}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(project.hasproperty (' Version_name_para '<span style="color: #000000;">) <span style="color: #000000;">{versionname version_name_para}} productflavors {}}android {lintoptions { Abortonerror</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">}}android.applicationvariants.all {variant</span></span>-<span style="color: #000000;"><span style="color: #000000;">Variant.outputs.each {output</span></span>-<span style="color: #000000;"><span style="color: #000000;">def OutputFile</span></span>=<span style="color: #000000;"><span style="color: #000000;">Output.outputfile</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(outputFile! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>&& outputFile.name.endsWith ('. apk ')<span style="color: #000000;">) <span style="color: #000000;">{def FileName</span></span>=<span style="color: #000000;"><span style="color: #000000;">outputfile.name; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(android.defaultConfig.versionName! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {fileName</span></span>= Filename.replace (". apk", "-${android.defaultconfig.versionname}.apk"<span style="color: #000000;"><span style="color: #000000;">) } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(project.hasproperty (' Out_put_apk_suffix_para '<span style="color: #000000;">) <span style="color: #000000;">) {fileName</span></span>= Filename.replace (". apk", "-${out_put_apk_suffix_para}.apk"<span style="color: #000000;"><span style="color: #000000;">)} def today</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Date (). format (' yymmddhhmm '<span style="color: #000000;"><span style="color: #000000;">); FileName</span></span>= Filename.replace (". apk", "-${today}.apk"<span style="color: #000000;"><span style="color: #000000;">) </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(project.hasproperty (' Out_put_dir_para '<span style="color: #000000;">) <span style="color: #000000;">) {File Output_dir1</span></span>= File ("${out_put_dir_para}"<span style="color: #000000;"><span style="color: #000000;">); Output.outputfile</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">File (output_dir1, FileName) println</span></span>"output file location:" +<span style="color: #000000;"><span style="color: #000000;">Output.outputfile</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">}</span></span>}<span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{output.outputfile</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">File (outputfile.parent, FileName) println</span></span>"output file location:" +<span style="color: #000000;"><span style="color: #000000;">output.outputfile}</span> }}}</span></pre><p><p></p></p><pre><pre><span><strong></strong></span></pre></pre><pre><pre><br><br><br><br><br></pre></pre><p><p>Configure Gradle in Android studio to "generate the specified Versioncode from the command line prompt, versionname, Specify the package output path of the apk"</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.