Android studio multi-channel packaging (super compact version)

Recently, I have been using it android studio for development. When developing and publishing, I encountered a multi-channel packaging problem. Since there are seven channels for distribution in the company, it is AndroidManifest.xmlvery troublesome to modify the value of the channel every time. . .

Finally, I went to google and found that some methods are gradleto switch between various files in the file. I AndroidManifest.xmlfeel that this is too troublesome. . . I searched a bit later, and posted some that everyone wrote about the same, and found a method of gradleusing manifestPlaceholdersthis attribute to replace the channel value, but I followed this method and failed, so I went to the official document to get it completely. Let's talk about how to do it below.

Generally, the statistics of the channels are nothing more than UF or others. Today, let's take the example of UF.
Channel information generally  AndroidManifest.xmlmodify the following values ​​in:

 

<meta-data android:name="UMENG_CHANNEL" android:value="wandoujia" />

 

First you have to AndroidManifest.xmlmodify in the meta-datafollowing look like:

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

The ${UMENG_CHANNEL_VALUE}value in it is the value you gradlecustomize in the configuration.

build.gradleThe file is productFlavorswritten like this:

 

copy code
productFlavors {
 
wandoujia {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
}
 
baidu {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
}
 
c360 {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "c360"]
}
 
uc {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "uc"]
}
 
}
 
copy code

where [UMENG_CHANNEL_VALUE: "wandoujia"]is the corresponding ${UMENG_CHANNEL_VALUE}value.

The latest update
later found that the above repeated code was too much, so I found a more concise way of writing on the Internet

 

copy code
productFlavors {
 
wandoujia {}
baidu {}
c360 {}
uc {}
 
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
 
}
 
copy code

The namevalue pair corresponds to each productFlavorsoption value, so as to achieve the purpose of automatically replacing the channel value.

In this way, when generating apk, you can select the corresponding Flavorspackage to generate the specified channel, and the generated apk will automatically add the suffix of the corresponding channel for you, which is very convenient and intuitive. You can decompile and verify by yourself.

Generate all channel packs at once

There is a command line tool in the bottom bar of android studio , which can automatically switch to the directory of the current project Terminalafter opening .CMD

Some projects will have graldew.batthis file, you can enter this command:

gradlew assembleRelease
can generate all the channel packages at one time,
but I generally do not recommend you to use this command. The version of gralde of the gradlew command cannot be controlled, and sometimes the old version of gradle will be downloaded for no reason,
so I personally recommend that you use the following usage.

First find graldethe root directory, add two environment variables to the system variables:

The variable name is: GRADLE_HOME, and the variable value is the root directory of gradle;
so the variable value is:C:\Users\yazhou\.gradle\wrapper\dists\gradle-2.1-all\27drb4udbjf4k88eh2ffdc0n55\gradle-2.1

There is also a bin directory PATHadded in the system variable, mine isgradle
C:\Users\yazhou\.gradle\wrapper\dists\gradle-2.1-all\27drb4udbjf4k88eh2ffdc0n55\gradle-2.1\bin

The configuration is complete here, and then you can generate all the channel packages at one time by typing in the Terminalmiddle  .gradle assembleRelease

All generated apks are under the project build\outputs\apk.

 

Reprint http://www.cnblogs.com/0616--ataozhijia/p/4203997.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326718302&siteId=291194637