Configure Gradle for "Dynamic compilation, specify remote server address, build multiple installation packages" in Android Studio

Source: Internet
Author: User

Demand:

in product development, it is often necessary to publish each version, each version of the server address has a different server address.    like

      • The development server uses the 192.168.1.232 server,
      • The test server uses the 192.168.1.245 server,
      • After the official launch, the server address is http://xxxx.com.

In conjunction with GIT development also branch management, commonly used are:

      • Dev
      • Test
      • Master

Our initial development process was to:

    1. At the beginning of the project, the developer writes the code, checks in the code into the dev branch. Developers use the server address of the development server
    2. When you start the test, the development group owner merges the code into the Test branch. Test group owner to modify the source file for the server address in the code, change the address of the test server, and then package the distribution to the test group members to begin testing.
    3. After the test is complete, the test group owner merges the code into the Master branch, modifies the server address to the on-line server address, and tests it to the official release.

In the above steps we see that the server address needs to be modified several times, the relevant technical capabilities have certain requirements, the operation is also easy to operate, such as writing a nonexistent service address.

Improved:

With the introduction of the Jenkins service for automatic packaging, Jenkins is an excellent automated build tool that can provide a great experience for our packaging. Allows testers to package themselves when they need it, without any additional action required. Of course, this requires us to configure the build script. After using Android Studio, you need to know the Gradle script.

Technology Key words : Buildconfigfield, Buildconfig

Ideas:

In the Gradle script, a field is declared with Buildconfigfield, and after compilation succeeds, a static constant is generated for that field, which we can use in our code. In conjunction with Gradle's buildtypes configuration different compilation types (such as test, development, online three types), each type specifies the value of a different field. After the successful build, Gradle will generate different APK packages for different types of artists, such as a one-time test, development, online three APK package.

Implementation method:

1. Modify the Build.gradle, configure the Buildconfigfield field under Buildtypes

  

buildtypes {release {minifyenabledfalse Buildconfigfield ("String", "api_host", "\" http://www.jinlinbao.com\ "" ) signingconfig Signingconfigs.zyfkey} debug245 {debuggabletrueminifyenabledfalse Buildconfigfield ("String", "api_host", "\" Http://192.168.1.245\ "" ) signingconfig Signingconfigs.zyfkey}}

Note the section with the underlined font above.

This method requires three parameters, namely, the field variable type, the variable name, and the value. Like here.

"String", "api_host", "\" Http://192.168.1.245\ ""    //  must pay attention to the right slash followed by the escape character semicolon
It will generate:
public static final String api_host = "http://192.168.1.245";

  

2. Modify the Build.gradle, configure the Defaultconfig under the Buildconfigfield field, this is for the convenience of code compilation, so that in various environments have api_host this field.

Buildconfigfield ("String", "api_host", "\" Http://192.168.1.232\ "")

3. Start the build and, when finished, you can use it in your code:

    String str = buildconfig.api_host;

  

The following is the complete configuration script:

Apply plugin: ' Com.android.application 'Android {signingconfigs {zyfkey {keyalias' Zhangyf 'Keypassword' Yunfei 'storefile file ('/users/zhangyunfei/git/vocabulary/zhangyf.keystore ') Storepassword' Yunfei '}} compilesdkversion19buildtoolsversion"22.0.1"Defaultconfig {ApplicationID"Vir56k.vecabulary"minsdkversion14targetsdkversion19Versioncode1Versionname"1.0"Buildconfigfield ("String", "api_host", "\" http://192.168.1.232\ "" ) } buildtypes {release {minifyenabledfalseproguardfiles Getdefaultproguardfile (' Proguard-android.txt '), ' Proguard-rules.pro 'Buildconfigfield ("String", "api_host", "\" http://www.jinlinbao.com\ "" ) signingconfig Signingconfigs.zyfkey} debug245 {debuggabletrueminifyenabledfalseBuildconfigfield ("String", "api_host", "\" Http://192.168.1.245\ "" ) signingconfig Signingconfigs.zyfkey}} productflavors {}}dependencies {com Pile Filetree (dir:' Libs ', include: [' *.jar '])}

Here is the Code demo:

  

 Packagevir56k.vecabulary;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);       Setcontentview (R.layout.activity_main);  String str  = buildconfig.api_host; TextView txt1=(TextView) Findviewbyid (R.ID.TXT1);    Txt1.settext (str); }}

Configure Gradle for "Dynamic compilation, specify remote server address, build multiple installation packages" in Android Studio

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.