Overview
The previous article (Android Automation Tour-build channel bundle) introduced several ways to generate channel packages in Android, basically solved the problem of slow packaging.
However, as more and more channels, different channels of application requirements are not the same. For example, some channels require the United States to use the client's application name of the United States, some channels require the application name of the United States purchase. For another example, some channels require that apps not use third-party statistical tools (such as flurry). In summary, these channels need to be adapted for each package.
The previous practice was to create a git branch for each channel that needed to be adapted, then switch to the appropriate branch and merge the code of the main branch. This approach is acceptable if you have fewer channels to match, and it's a nightmare for developers if you have more branches. Fortunately, since the gradle flavor, everything has become simple. This article assumes that the reader has used gradle, and if you do not know it, read the relevant documentation first.
Flavor
First look at the code in the Build.gradle file:
Android { ... .. productflavors { Flavor1 { minsdkversion }} }
The example above defines a flavor:flavor1 and specifies that the app's minsdkversion is 14 (and of course you can configure more properties, which can refer to related documents). At the same time, Gradle will also associate the corresponding Sourceset for the flavor, the default location is the src/<flavorname> directory, which corresponds to this example Src/flavor1.
The next thing to do is to configure the flavor in the Build.gradle file according to the specific requirements and add the necessary code and resource files. Take Flavor1 as an example, run the Gradle assembleFlavor1 command to generate the desired adapter package. The following is mainly about the United States to buy some of the Android client adaptation cases.
CaseUse a different package name
There are two versions of the Android client: Mobile version (Com.meituan.group) and HD (COM.MEITUAN.GROUP.HD), and two versions using different codes. Currently the HD version of the corresponding code is no longer maintained, I would like to directly use the phone version of the code. There are several ways to solve this problem, but using flavor is relatively straightforward, as shown in the following example:
productflavors { HD { applicationid "Com.meituan.group.hd" }}
The code above adds a flavor named HD and specifies that the app's package name is COM.MEITUAN.GROUP.HD, and running the Gradle ASSEMBLEHD command generates the HD adapter package.
Control whether updates are automatic
Shopping Android The client will check the client for updates by default at startup, and will prompt the user to download if there is an update. However, some channels and application markets do not allow this default behavior, so you will need to disable the Automatic Updates feature when adapting to these channels.
The solution is to provide a configuration field that will check the value of the field when it launches to determine whether the Automatic Updates feature is turned on. The use of flavor can be a perfect solution to such problems.
Gradle will generate a Buildconfig.java file for flavor at the generatesources stage. The Buildconfig class provides some constant fields by default, such as the app's version name (Version_name), the app's package name (Package_name), and so on. More powerful, developers can also add custom fields. The following example assumes that the Wandoujia market disables the automatic updating feature by default:
Android { Defaultconfig { Buildconfigfield "boolean", "Auto_updates", "true" } productflavors { Wandoujia { Buildconfigfield "boolean", "Auto_updates", "false"}} }
The above code generates a auto_updates Boolean constant in the Buildconfig class, with the default value of True, which is set to False when Wandoujia flavor is used. You can then use the Auto_updates constant in your code to determine whether the Automatic Updates feature is turned on. Finally, it is not easy to run the Gradle Assemblewandoujia command to generate a channel pack that does not turn on the auto-upgrade feature by default.
Use a different app name
The most common type of adaptation is a resource that modifies an application. For example, the United States to buy Android client app name is the United States, but some channels need to change the application name for the United States purchase; Also, clients often work with some application distribution market, need to add the third party market logo in the application launch interface, there are many similar to this kind of adaptation form.
Gradle when building an app, it takes precedence over the same name resource in the dataset that the flavor belongs to. So, the idea is to add a string resource of the same name to the flavor dataset to overwrite the default resource. The following is an example of the application of the Wandoujia channel to the United States.
First, add the following flavor in the Build.gradle configuration file:
Android { Productflavors { Wandoujia { } }}
The above configuration defaults to the Src/wandoujia directory as a Wandoujia flavor dataset.
Next, create the Wandoujia directory within the SRC directory and add the following application name string resource (Src/wandoujia/res/values/appname.xml):
<resources> <string name= "App_name" > US shopping </string></resources>
The default application name string resource is as follows (Src/main/res/values/strings.xml):
<resources> <string name= "App_name" > American tour </string></resources>
Finally, run the Gradle Assemblewandoujia command to create an app called the American shopping application.
Using a third-party SDK
Some channels require clients to embed a third-party SDK to meet specific adaptation needs. For example, the 360 application market requires us to buy the Android client's boutique application module using the SDK they provide. The challenge is how to add the SDK only for specific channels, and not the SDK for other channels. The use of flavor can be a good solution to this problem, the following thought qihu360 flavor introduced com.qihoo360.union.sdk:union:1.0 SDK as an example to illustrate:
Android { Productflavors { qihu360 { } }}...dependencies { provided ' COM.QIHOO360.UNION.SDK: union:1.0 ' qihu360compile ' com.qihoo360.union.sdk:union:1.0 '}
The previous example adds a flavor named qihu360, and specifies that both compilation and runtime depend on com.qihoo360.union.sdk:union:1.0. Other channels simply rely on the SDK when they are built, and do not add it when packaged.
Next, you need to use reflection technology in your code to determine whether your application has added the SDK, and decide whether you want to display the best-of-the-line apps provided by the SDK. Some of the code is as follows:
Class MyActivity extends Activity { private boolean useqihusdk; @override public void OnCreate (Bundle savedinstancestate) { try { class.forname (" Com.qihoo360.union.sdk.UnionManager "); USEQIHUSDK = true; } catch (ClassNotFoundException ignored) { }}}
Finally, run the Gradle assembleQihu360 command to generate a channel bundle with 360 premium application modules.
Summarize
Adaptation is a dirty work, especially when it comes to matching channels more often. The above describes several examples of using gradle flavor for adaptation, which basically solves the complicated adaptation work.
Resources
- http://www.gradle.org/
- Product Flavors
American group Android Automation Tour-Adapter channel pack