Front introduced Meituan multi-channel packaging solution to the problem of the package is slow, but as more and more channels, is more and more on the requirement of the packaging, such as our APP and a channel to do starting (activities) need us behind the name with different suffix, etc., also some channels do not allow us to use automatic updates, third party statistics, anyway, is all sorts of wonderful work requirements, We had to make a copy of the code to make changes.
If it’s just one or two, that’s fine. If it’s more than that, it’s a nightmare. We need to do these differentiated developments in the branch, and then we have to merge the main branch code back every time. Is there…
Thankfully, everything is easier now that Gradle Flavor is available. This article assumes that you have worked with Gradle before, and you are advised to read the documentation if you don’t already.
Flavor starts with the build.gradle file:
android {
....
productFlavors {
flavor1 {
minSdkVersion 14
}
}
}Copy the code
This example defines flavor: flavor1 and specifies the minSdkVersion of the application to be 14 (additional attributes can be configured, as described in the documentation). Gradle also associates the flavor with its sourceSet. The default location is SRC /, which in this case is SRC /flavor1.
The next step is to configure the flavor in the build.gradle file and add the necessary code and resource files based on your specific requirements. For example, run the gradle assembleFlavor1 command to generate an adaptation package. The following describes some adaptation cases of Android clients.
Configure different application names. Gradle preferentially uses resources with the same name in the dataSet to which flavor belongs when building applications. The solution is to add a string resource with the same name to the flavor’s dataSet to override the default resource. The application names for the Wandoujia channel are described below.
First, add the following flavor to the build.gradle configuration file:
android {
productFlavors {
wandoujia {
}
}
}Copy the code
The preceding configuration defaults the SRC/Wandoujia directory to the wandoujia flavor’s dataSet. Next, create wandoujia directory within the SRC directory, and add the following application name string resources (SRC/wandoujia/res/values/appname. XML) :
NewApp_Name
Copy the code
The default application name string resources as follows (SRC/main/res/values/strings. The XML) :
App_Name
Copy the code
Finally, run the gradle assembleWandoujia command to generate an application named NewApp_Name.
The strings. XML name is not used in the Wandoujia package because it will cause file duplication. By default, files in the main folder are not allowed to appear in other adaptation directories with the same file name. No difference files are created under the main file
Differentiation file.png
Controls whether to update automatically
Most Android clients check for updates by default when they start up and prompt users to download if there are any. However, some channels and app markets do not allow this default behavior, so you need to disable automatic updates when adapting to these channels.
Gradle generates a buildconfig.java file for flavor at the generateSources stage. The BuildConfig class provides constant fields by default, such as the version name of the application (VERSION_NAME), the package name of the application (PACKAGE_NAME), and so on. Even more powerful, developers can add custom fields. The following example assumes that the Wandoujia market disables automatic updates by default:
android {
defaultConfig {
buildConfigField "boolean", "AUTO_UPDATES", "true"
}
productFlavors {
wandoujia {
buildConfigField "boolean", "AUTO_UPDATES", "false"
}
}
}Copy the code
The code above generates the AUTO_UPDATES Boolean constant in the BuildConfig class. The default value is true, which is set to false when using Wandoujiaflavor. You can then use the AUTO_UPDATES constant in your code to determine whether automatic updates are enabled. Finally, run the gradle assembleWandoujia command to generate channel packages that do not have automatic upgrade enabled by default.
Finally, we can configure our Run’app package through the Build Variant in the lower left corner of Android Studio when we Run the test separately.
Build Variant configuration.png
- This is a small detail that can be used in separate debug and release tests
ProductFlavors includes flavors that can be configured with different codes, resources, configurations, and libraries. However, these differentiated configurations need to be rebuilt separately, i.e., repackaged. Although it is automated, but it still takes time to pack, can not enjoy the treatment of meituan multi-channel packaging 100 10s!!
If you like, you can pay attention to my wechat official number. I will write some related articles irregularly, blow some niubi, chop down some mountains, come to some chicken soup for the soul, and make some unknown stories. Learn to share, if it is helpful to you, please share this article to more friends in need!
Identify him and pay attention to him. JPG
Like friends can also follow my public account JonymobileCopy the code