Due to business needs, we may need to be the same for different companies for custom development of the project, the logic in the code under the condition of basically the same, we can use in gradle for different companies to build the specified packaging configuration information, and the most main information application package name, version information, icon, and other configuration information.

So let’s see how to configure it:

android { ... // Add productFlavors {// The first apK package fashionlazy1 {// Application package name applicationId'com.fashionlazy.demo1'
            versionName "1.0.0"
            versionCode 100
            minSdkVersion 21
            targetSdkVersion 21
            resValue "string"."app_name"."FashionLazy"ManifestPlaceholder = [authorities:"com.fashionlazy.demo1.fileprovider"]
            falvorDimensions "demo1"} // The packaging configuration for the second APK, fashionlazy2 {// Application package name, applicationId'com.fashionlazy.demo2'
            versionName "1.0.0"
            versionCode 100
            minSdkVersion 21
            targetSdkVersion 21
            resValue "string"."app_name"."FashionLazy"ManifestPlaceholder = [authorities:"com.fashionlazy.demo2.fileprovider"]
            falvorDimensions "demo2"}}... }Copy the code

In gradle, we have already configured two packages for APK. Some of the more common values are not explained. Let’s look at the following configuration instructions

resValue

Add a string to the resource file. Do not add this field to the string.xml file. Otherwise, an error may occur. In this example, we mainly set the value app_name

manifestPlaceholder

This parameter can be interpreted as adding an object to the manifest, which can then be referenced in AndroidManifest. AndroidManifest = AndroidManifest; AndroidManifest = AndroidManifest; AndroidManifest = AndroidManifest; AndroidManifest = AndroidManifest; AndroidManifest = AndroidManifest

<provider
    android:authorities="${authorities}"
    android:name=".provider.FileProvider"
    android:exported="false">... </provider>Copy the code

${authorities} = / / AndroidManifest provider = / / AndroidManifest provider = / / AndroidManifest provider = / / AndroidManifest provider = / / AndroidManifest provider = / /

Generate signed Bundle/APK option in Build

Then select a signature file for package signature and enter the relevant information of the file, as shown below:

Then let’s click Next again, as shown below

As can be seen from the picture, we can type four packages at this time, namely, debug and release of fashionlazy1 package name, and Debug and release of fashionlazy2 package name. Fashion1 and fashion2 are the productFlavors properties that we add to the Gradle file.

conclusion

With gradle files, we can easily customize apK packages for different channels and different companies. We can define the package name and version information for each channel. At the same time, we can add customized parameter information, such as app_name and app_icon, to each channel through resValue and manifestPlaceholder for resource files and some configurations in AndroidManifest. As for the specific business logic code, we can also judge the specific code execution logic by comparing different package name data, so as to achieve better channel customization.

The above is some of my experience, my technical level is general, do not like spray, also hope that you have a better plan to share with me, let us progress together! thank you