Build variants create apps with different configurations (illustrated)

Source: Internet
Author: User

description of the phenomenon

The development of an app can not avoid running in a variety of environments, such as development environment, test environment, formal environment, and depending on the environment to configure different variables, in the past, I have to comment on the relevant code implementation, obviously this is more troublesome.

Get to the point, so how can you change the configuration with one click?

Step One: Configure the Productflavors node of the Build.gradle file
 productFlavors {        moon {//配置的名称,可以随便取           // 具体配置,见后面讲解        }        moon_test {//配置的名称,可以随便取           //具体配置,见后面讲解        }        ...//还可以创建很多个    }
Step Two: Create a different configuration file

In the project SRC directory, create a package with the name of the configuration and create the Java.com.pengkv.moon package accordingly

    -moon        -...        -build        -src            -main            -moon(配置名)                -java.com.pengkv.moon                    -各种配置文件            -moon_test(配置名)                -java.com.pengkv.moon                    - 各种配置文件(各个配置包下的对应名称要一样)

Where the Logutil code under the Moon package is as follows

publicclass LogUtil {    publicstaticvoidlogTag() {        Log.v("---->""我是版本一");    }}

The Logutil code under the Moon_test package is as follows

publicclass LogUtil {    publicstaticvoidlogTag() {        Log.v("---->""我是版本二");    }}
Step Three: Click on the build variants on the left menu bar and select the appropriate configuration version to run

When the code runs, it can be found that the same code runs with different results

Result one:

Result two:

Step Four: Configure the Androidmanifest.xml file

There are times when you need to configure different parameters in Androidmanifest.xml, such as app name, version number, app ID, and so on, such as (generate different app names)

At this point, we need to configure the productflavors in detail.

productFlavors {        moon {            "zQDqX7GP3avIQTN0BoFSyhrF",                                    APP_NAME           "@string/appName"]        }        moon_test {            "kBONkZGEe0S2lYd6yOqq9MRx",                                    APP_NAME           "@string/appName_test"]        }    }

Then the values of the different configurations are referenced in the Androidmanifest.xml

<application        android:icon="@mipmap/ic_launcher"        android:label="${APP_NAME}"//引用productFlavors里面的配置值        android:theme="@style/AppTheme">        <activity>        //...        </activity>    </application>
attached: build.gradle illustration

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Build variants create apps with different configurations (illustrated)

Related Article

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.