Android Studio Gradle multi-channel packaging, androidgradle

Source: Internet
Author: User

Android Studio Gradle multi-channel packaging, androidgradle

Some services provided by UMeng have been integrated with multi-channel packaging before. UMeng should make some statistics and analysis based on the channel number of each application market, such as the number of downloads, activity, and automatic updates of each channel. UMeng provides a packaging tool (here), but it has not been updated for a long time. It also switches to Android Studio IDE. The Gradle plug-in can meet the multi-channel packaging needs well.
The general idea of multi-channel packaging is (for UMeng): dynamically change an attribute value (channel number) in the AndroidManifest. xml file to identify different channels.
Gradle's Product flavors feature (here) can be easily implemented. Simply put, the implementation method is: different flavor specifies AndroidManifest. xml files of different channels. This method has a lot of drawbacks and involves too much repetitive work. We only need to maintain too many AndroidManifest. xml files to change one of the attributes. Fortunately, Android Studio 1.0 provides a more powerful Manifest Merger (here) feature. The Placehodler Support (here) feature can greatly simplify the above practices, no need to maintain so many AndroidManifest. xml file! However, all the channels are configured in build. after all, it is difficult to maintain the gradle file (dozens of applications on the application market). The following describes how to read the configuration file through a program and generate different channel packages.
The Placehodler Support feature must also be used through the program. Therefore, upgrade Android Studio to 1.0 or above.
The specific idea is to dynamically generate build type by reading the channel number in the configuration file. The specific code is as follows:

// Multi-channel package def channels () {if (project. hasProperty ("channel") {// channel No. configuration file path def path = ". /build-types/channels.txt "file (path ). eachLine {line-> if (! Line. startsWith ("//") {// remove the comment row android. buildTypes. create (line, {manifestPlaceholders = [channel: line]}) }}}

The AndroidManifest. xml file is changed as follows:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.tubb.cityindex" >    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.tubb.cityindex.CitySelectorActivity"            android:label="@string/app_name" >            <intent-filter><meta-data android:name="UMENG_CHANNEL" android:value="${channel}"/>    </application></manifest>

Add the channels configuration file in the build-types folder under the project app directory.
// Baidu application market baidu // 360 application market m360

The test code is hosted here. You are welcome to discuss it ~~







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.