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)