[Android] Configuring Build.gradle Dynamic Parameters

Source: Internet
Author: User
Tags hasproperty

(1) An Android project has one build.gradle that is responsible for project scope, and the module has its own build.gradle that are specifically responsible for the modules.

(2) In Gradle the task is a class citizen, the specified task can be executed directly through the Gradlew + task name, for example, the following command is executed: task Releaseautoblforaar

Gradlew Releaseautoblforaar

(3) Variable names can be customized in Defaultconfig, which can be referenced in Java code at compile time:

defaultconfig {        "com.test        "        5        1.1.0                 Buildconfigfield ("String", "Api_host", "${api_dev_host}")}

The api_host in this is string (the third argument is the value of the pass) and you can see the Java code generation so that you can build a bridge between the code and the configuration:

 Public Final classBuildconfig { Public Static Final BooleanDEBUG = Boolean.parseboolean ("true");  Public Static FinalString application_id = "Com.test";  Public Static FinalString build_type = "Debug";  Public Static Final intVersion_code = 6;  Public Static FinalString version_name = "1.1.1"; //fields from the Default Config.           Public Static FinalString api_host = "http://test.api.cn";}

(4) Different task parameters can be configured by buildtypes

buildtypes {release {/*on-line environment*/Buildconfigfield"Boolean", "Log_debug", "false"//do not display logBuildconfigfield "String", "Api_host", "${api_release_host}"//API HostMinifyenabledtrue                      //whether to confuseZipalignenabledtrue                    //whether to set zip alignment optimizationsShrinkresourcestrue                    //removing useless resource filesSigningconfig Signingconfigs.release//SignatureProguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.pro '} prerelease {/*Pre-hair environment*/Buildconfigfield"Boolean", "Log_debug", "false"//do not display log//..... } debug {/*Local development environment*/minifyenabledfalse} beta {/*test Environment*/Buildconfigfield"Boolean", "Log_debug", "true"//Show Log//.....     }}

Can be seen through the Androidstudio gradle panel to see a number of compilation tasks, the original default is only Assembledebug, now more Assemblebeta, Assembleprerelease, Assemblerelease, Double-click to execute.

(5) How to dynamically configure the contents of different androidmanifest.xml variables through Gradle? By customizing the manifestplaceholders property value.

First specify in the Androidmanifest.xml file that you want to use Gradle dynamic configuration value ${talking_data_app_id}:

    <!-- TalkingData Configuration  -    < Meta-data         Android:name = "td_app_id"         android:value= "${talking_data_app_id}"/>

You can configure a special variable property in Build.gradle:

 def test_talking_data_app_id = "6e5389ead0c2c2cfb7b379701f6d2ba8" defaultconfig {    ApplicationID  "com.test"  minsdkversion  15    Targetsdkversion  23 Versioncode  5 Versionname  1.1.0 Buildconfigfield ( "String", "Api_host", "${api_dev_ HOST} ") manifestplaceholders   = [ /*   TalkingData test ring Border  */  talking_data_app_id:  "${tes T_TALKING_DATA_APP_ID} "/* can add multiple key-value pairs, representing variables with corresponding values */ ]}  

Similarly, we can specify release, Debug and other configuration manifestplaceholders in buildtypes to achieve different configuration effects.

(6) How do I dynamically get parameter options in Build.gradle? The dynamic parameters are obtained by Project.hasproperty ('version_code').

 defaultconfig {applicationid  "com.ixwork"  Minsdkversion  15 targetsdkversion  //
     key look at these two lines  versioncode project.hasproperty  (' Version_code ')?  Integer.parseint (version_code): Def_version_code versionname project.hasproperty  ( ' version_name ')? Version_name: "${def_version_name}"  Buildconfigfield ( "String", "Api_host", "${ Api_dev_host} ")}  

(7) How to send a reference? With the form of- pvar_name=var_value , where-P is the plus parameter, for example:

- P - P - P - P file_name=test.apk

[Android] Configuring Build.gradle Dynamic Parameters

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.