Why do I need to add channel information to my application?
The launch of Android apps has to face a variety of markets, which we call channels. Sometimes, we need to know which channel the application is downloaded from. For example, we may need to count which markets bring a larger number of users. For example, we may have some profit needs and specific channels to divide. These are all statistical channels of information.
How do you usually add channel information to your application?
In order to statistical channel information, you have to add the channel information somewhere in the program, and then hit different packages for different channels. You can typically add a channel number to a manifest file instead of writing it directly in your code. The advantage of this is that you can automate the modification of the channel number in the manifest file for different channels, and then automatically package the channel.
The manifest file supports the Meta data label, and it is recommended that you use this custom label. For example, the following file fragment.
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:versioncode= "X"
Android:versionname= "x.x.x"
Package= "Com.xxx" >
......
<application android:icon= "@drawable/icon"
Android:label= "@string/app_name" >
......
<meta-data android:name= "CHANNEL" android:value= "c_001"/>
</application>
</manifest>
In this code example, we added a meta-data tag under the application node, the name is channel, and the value is c_001, which is the number of a channel we specify.
For different channels to pack, it is necessary to manually or automatically modify c_001 to become c_002, c_003, and other other channels we define, and then dozen different packages.
How does the program read the packaged channel number?
In program code, you can read the Meta-data defined in the manifest file. The following is a code instance.
Copy Code code as follows:
public static String Getchannelcode (context context) {
String Code = GetMetaData (Context, "CHANNEL");
if (code!= null) {
return code;
}
return "c_000";
}
private static string GetMetaData (context context, String key) {
try {
ApplicationInfo ai = Context.getpackagemanager (). Getapplicationinfo (
Context.getpackagename (), packagemanager.get_meta_data);
Object value = Ai.metaData.get (key);
if (value!= null) {
return value.tostring ();
}
catch (Exception e) {
//
}
return null;
}
How do I automate the packaging process?
We want to automate the process of playing different packages for different channels, in two cases. One is the case of the source code, one is that there is no source code only apk files.
The case of a source code.
The case of the source code is relatively simple. We can use automated scripting tools for packaging, such as using Ant.
With ant packaging, there are two key issues: one is to support a for loop in ant to automate multiple packaging actions, and how to modify manifest files in ant to support different markets. As long as we solve these two key problems, with Ant's Basic functions, we can achieve our requirements.
Support Loops in Ant
There is no task for the for loop in Ant's Core package to download the corresponding expansion pack. You can use an open source Ant-contrib package.
Download Address: http://ant-contrib.sourceforge.net/
When the download is complete, copy the Lib package in the Ant-contrib to the installed ant library Apache-ant-xxx\lib below, and you can use it.
Specific how to use, you can refer to the official website of Ant-contrib.
Modify the manifest file in ant
With the <replaceRegExp> tasks provided by the Ant extension task, you can also implement a regular expression replacement.
For example, to replace the header "c_001" string in the Androidmanifest.xml file with "c_002", you can use the following ant script:
<replaceregexp
File= "Androidmanifest.xml"
Byline= "true"
Match= "c_001"
Replace= "c_002"
/>
The <replaceRegExp> mission is strong, and this is just a simple example.
Only the case of apk files.
If there is no source code, only apk files, things are a little more complicated. We know that there is an open source Apktool, you can decompile the apk file, or repackage it. Please refer to Apktool's official documents for specific reference.
With the help of Apktool, you can have a basic idea.
First use Apktool to decompile the apk file, decompile the resource file and Androidmanifest.xml file.
Use scripting code to modify the channel ID text in the Androidmanifest.xml file.
Use Apktool to repackage into apk files.
Use the Jarsigner tool to sign the APK file.
You can play different APK installation packages for different channels as long as you repeat the 2-4 steps repeatedly.
According to this idea, the concrete realization is relatively simple. Can be written as a bat script file or written as a Java application.