Android Gradle multi-channel packages, dynamic configuration AppName

I. Introduction

Because the status of the domestic Android application distribution market, when we publish APP, generally need to generate multiple channel packages, uploaded to a different application markets. These channels require packages contain different information sources, or the interaction of APP and background data reporting, it will bring the respective information channels. In this way, we will be able to key statistical data of the number of downloads for each distribution market, the number of users.

Two, Gradle multi-channel package

1, the general practice

Under the general practice in the AndroidManifestincreased number of channels:

<meta-data android:name="SDK_CHANNEL" android:value="Channel ID" />

To change the settings manually, and then packed again. Thus, repetitive workload greatly increased. So there will be multi-channel package on AndroidStudio.

2, Gradle multi-channel package

In this case, Android Gradle provides a very convenient way for us to replace the contents of the file AndroidManifest, he is manifestPlaceholder, Manifestplaceholders.

ManifestPlaceholdersIt is ProductFlavora property type is a Map, so we can configure many placeholders at the same time. Here we have to demonstrate the use of manifestPlaceholders by this example configuration channel number.

In build.gradlethe androidadd the following code in the tag:

android {
	...

	productFlavors{
        google{
            manifestPlaceholders = [
                    SDKChannel: "google",
                    app_name : "@string/app_name",
            ]
        }
        baidu{
            manifestPlaceholders = [
                    SDKChannel: "baidu",
                    app_name : "@string/app_name",
            ]
        }
    }
}

In the AndroidManifest.xmlfile code it is as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sdk">
	<application ...
			android:label="${app_name}">

		<meta-data
            android:name="SDKChannel"
            android:value="${SDKChannel}" />

		...
	</application>
</manifest>

Next we take a look at the effect of the pack.
Here Insert Picture DescriptionHere Insert Picture Description
If the channel number, then a lot of App, we can not configure it one by one, much too tired, maintenance is trouble, we can also be modified by means of an iterative productFlavors volume.

android {
	...

	productFlavors{
        google{
        }
        baidu{
        }
    }

	productFlavors.all { flavor ->
        flavor.manifestPlaceholders = [SDKChannel: name, app_name : "@string/app_name"]
    }
}

We iterate through all the function of each ProductFlavor, then put their name as a channel name, is very convenient. Here you can not just do this one thing, when traversing ProductFlavor, you can do a lot of you want to do, this is the place Gradle flexible, when the program write the script.

Android Gradle provides application mode manifestPlaceholders placeholder, so that we can replace any file AndroidManifest $ {Var} placeholder format. So his name usage scenarios is not limited to this one channel, for example, as well as the auth ContentProvider authorization, or other dynamic configuration meta information. Flexible use of it can help us to do a lot of things, let us build more flexible and convenient.

Published 100 original articles · won praise 45 · views 640 000 +

Guess you like

Origin blog.csdn.net/wangzhongshun/article/details/104844228