How to automate the packaging process of different channels by adding channel information for Android applications

Source: Internet
Author: User

Why do we need to add channel information in the application?

The release of Android applications must face various markets, which are called channels. Sometimes, we need to know the channel from which the application is downloaded. For example, we may need to calculate which markets bring a large number of users. For another example, we may have some profit needs to be divided into specific channels. These are all statistics channel information.

How do I add channel information to an application?

To collect channel information, you have to add channel information somewhere in the program, and then pack different packages for different channels. Generally, you can add channel numbers to the Manifest file instead of directly writing them in the code. In this way, you can automatically modify the channel numbers in the Manifest file for different channels, and then automatically package the channel.

The Manifest file supports Meta Data tags. We recommend that you use these custom tags. For example, the following file fragment.

Copy codeThe Code is 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, named CHANNEL, and its value is C_001, which is the CHANNEL number we specified.

To package for different channels, manually or automatically modify C_001 to become C_002, C_003, and other channel numbers we define, and then create different packages.

How does the program read the package channel number?

In the program code, you can read the meta-data defined in the Manifest file. The following is a code example.

Copy codeThe Code is 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 to automate the packaging process?

We want to automate the process of creating different packages for different channels in two cases. One is with source code, and the other is with no source code but an APK file.

Source code.

It is relatively simple to have source code. We can use automated script tools for packaging, such as Ant.

Ant packaging has two key issues: one is to support the For Loop in Ant to automatically perform multiple packaging actions; the other is how to modify the Manifest file in Ant, to support different markets. We only need to solve these two key problems and cooperate with Ant's basic functions to achieve our requirements.

Supports loop in Ant

There are no related For loop tasks in the Ant core package. You need to download the corresponding extension package. You can use the open-source Ant-contrib package.

: Http://ant-contrib.sourceforge.net/

After the download is complete, copy the lib package in ant-contrib to the installed Ant library apache-ant-XXX \ lib for use.

For details about how to use it, refer to ant-contrib's official website.

Modify the Manifest file in Ant

The <replaceRegExp> task provided by the Ant extension task can also be replaced based on regular expressions.

For example, to replace the line "C_001" in the AndroidManifest. xml with "C_002", you can use the following Ant script:

<Replaceregexp

File = "AndroidManifest. xml"

Byline = "true"

Match = "C_001"

Replace = "C_002"

/>

<ReplaceRegExp> the task is powerful. This is just a simple example.

Only the APK file is available.

If there is no source code and only the APK file is available, it will be a little more complicated. We know that there is an open-source APKTOOL that can decompile or repackage the APK file. For details, refer to the official APKTOOL file.

With the help of APKTOOL, you can have a basic idea.

First, use APKTOOL to decompile the APK file and decompile the resource file and AndroidManifest. xml file.

Use the script code to modify the channel ID text in the AndroidManifest. xml file.

Use APKTOOL to repackage it into an APK file.

Use the jarsigner tool to sign the APK file.

You only need to repeat steps 2-4 to create different APK installation packages for different channels.

Based on this idea, the specific implementation is relatively simple. You can write a BAT script file or a Java application.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.