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.xml
very troublesome to modify the value of the channel every time. . .
Finally, I went to google and found that some methods are gradle
to switch between various files in the file. I AndroidManifest.xml
feel that this is too troublesome. . . I searched a bit later, and posted some that everyone wrote about the same, and found a method of gradle
using manifestPlaceholders
this 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.xml
modify the following values in:
<meta-data android:name="UMENG_CHANNEL" android:value="wandoujia" />
First you have to AndroidManifest.xml
modify in the meta-data
following look like:
<meta-data android:name="UMENG_CHANNEL" android:value="${UMENG_CHANNEL_VALUE}" />
The ${UMENG_CHANNEL_VALUE}
value in it is the value you gradle
customize in the configuration.
build.gradle
The file is productFlavors
written like this:
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"]
}
}
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
productFlavors {
wandoujia {}
baidu {}
c360 {}
uc {}
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}
The name
value pair corresponds to each productFlavors
option value, so as to achieve the purpose of automatically replacing the channel value.
In this way, when generating apk, you can select the corresponding Flavors
package 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 Terminal
after opening .CMD
Some projects will have
graldew.bat
this 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 gralde
the 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 PATH
added 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 Terminal
middle .gradle assembleRelease
All generated apks are under the project build\outputs\apk
.
Reprint http://www.cnblogs.com/0616--ataozhijia/p/4203997.html