Android uses Gradle to realize multi-channel packaging, with different package names, logos, names, themes, etc. for the same project

Keywords: Mobile Android xml Gradle

Recently, the company has a new requirement. According to the previous project, change the name, logo and some resource files to repackage a new app

Implementation ideas

1. Copy a project name, logo, etc. (inefficient, troublesome) ❌
2. Multi channel packaging (convenient and fast)

Implementation method

Build.gradle > Android

android {
	...
	...
    //Multi channel packaging
    productFlavors {
        //Baidu channel
        baidu {
            //Modify package name
            applicationId "com.product.baidu"
            //Modify the app name. The string resource cannot contain "app_name"
            resValue "string", "app_name", "Baidu"
            //Modify some fields of Android manifest.xml (including logo)
            manifestPlaceholders = [CHANNEL_VALUE: "baidu",app_icon: "@mipmap/logo"]
        }
        //Millet channel
        xiaomi {
            applicationId "com.product.xiaomi"
            resValue "string", "app_name", "millet"
            //Modify some fields of Android manifest.xml (including logo)
            manifestPlaceholders = [CHANNEL_VALUE: "xiaomi",app_icon: "@mipmap/logo"]
        }
    }

Replace Logo

From the above code, you can see that this line of code exists in both channels:

Baidu

manifestPlaceholders = [CHANNEL_VALUE: "baidu",app_icon: "@mipmap/logo"]

millet

manifestPlaceholders = [CHANNEL_VALUE: "xiaomi",app_icon: "@mipmap/logo"]

This line of code passes values to Android manifest.xml, but Android manifest.xml needs to be received using placeholders

 <application
        android:name=".MoneyApp"
        android:allowBackup="true"
        android:icon="${app_icon}"  <!--placeholder,Reception above Gradle Incoming values-->
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/NoActionBar"
        tools:replace="android:icon,android:theme">
        ...
        ...
	</application>
        

However, the values of the above two channels are all @ mipmap/logo images. Here, you need to use resource files with different images of the same name. Files with the same name cannot be in the same folder. Here, you need to create a separate channel folder under app/res / / main

Note! The folder name must be the same as the channel name, and then create the logo resource file under the respective channel folder

When packaging, each channel will first load the resources of its own channel folder, and then different logo s can be implemented

Finally, you can sign the package. The two channel packages are in baidu and xiaomi folders respectively~

Posted by candle21428 on Thu, 12 Dec 2019 10:41:53 -0800