Android Studio Gradle multi-channel packaging

Source: Internet
Author: User


before integrating some of the services provided by Umeng to multi-channel packaging, Umeng to each application market channel number to do some statistics and analysis, such as the number of downloads, activity, Automatic Updates and so on. Umeng provides a packaging tool (here), but the official has not been updated for a long time, and also switch to the Android Studio IDE, Gradle plug-in can be very good for multi-channel packaging requirements.
The idea of multi-channel packaging is (for Umeng): Dynamically changing one of the attribute values (channel numbers) in the Androidmanifest.xml file to identify different channels.
Gradle Product Flavors features (here) can be very convenient to implement the function, the simple implementation of the way: different flavor to specify different channels of androidmanifest.xml files. There are a lot of drawbacks to this approach, too many repetitive work, just to change one of these properties and to maintain too many androidmanifest.xml files. It is gratifying that Android Studio 1.0 provides a more powerful manifest merger (here) feature, where the Placehodler support (here) feature can greatly simplify the above approach, No need to maintain so many androidmanifest.xml files! But all the channels are configured in the Build.gradle file is not good maintenance (up to dozens of of the application market), the following main introduction through the program to read the configuration file, generate a different channel package.
the way to go through the program also relies onPlacehodler support features, so Android Studio please upgrade to more than 1.0.
the specific idea is to dynamically generate the build type by reading the channel number in the configuration file. The specific code is as follows:
Multi-channel Packaging def channels () {    if (Project.hasproperty ("channel")) {        //channel number profile path        def path = "./build-types/ Channels.txt "        file" (path). eachline {line-and            if (!line.startswith ("//")) {//Exclude comment lines                Android.buildTypes.create (line, {                    manifestplaceholders = [channel:line]                })            }        }    }

The androidmanifest.xml file changes 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>

Finally, just add the channels profile to the Build-types folder now in the Engineering app directory
Baidu Application Market baidu//360 Application Market m360

test code hosted in (here), Welcome to discuss ~ ~







Android Studio Gradle multi-channel packaging

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.