Gradle Combat: Android Multi-channel Packaging solution Summary

Source: Internet
Author: User

View Original: http://blog.csdn.net/u010818425/article/details/52319382

Gradle Combat Series Articles:
"Gradle basic knowledge point and common configuration"
Gradle: Co-existence of packages with different types of compilation
"Gradle: Release AAR package to maven warehouse"
"Gradle: Execute SQL Operations Hive database"

This article will continue the previous several blog style, first from the basic concept, which helps us understand the latter, in the subsequent code if you forget the specific meaning of a concept, you can review the head to revisit the concept of the introduction.

In this paper, the implementation principle of the General Batch packaging scheme is introduced in detail, and the basic realization principle of the mass packaging is introduced, and several implementation schemes are quoted for everyone's reference.

Introduction of Basic concepts 1. Package
    • The package name in the Androidmanifest file

      <manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.company.appname"    android:versionCode="1"    android:versionName="1.0" >

      The package name has two functions:
      The first is the unique identification of the application on the device and the unique identification on the application market;
      The second is the package that is used to name your resource class (and the class name that parses any related activity), such ascom.company.appname.R

2. PlaceHolder
    • The variables in the Androidmanifest file are expressed by ${PlaceHolder} means that placeholder is a variable that can be assigned, such as the channel in the Friend League statistic:

      <meta-data    android:name="UMENG_CHANNEL"    android:value="${UMENG_CHANNEL_VALUE}"></meta-data>
3. ApplicationID
    • Corresponds to the package in Androidmanifest

      android {    defaultConfig {        applicationId "com.company.appname"    }}
4. Buildtypes
    • Packages for generating different compilation types, such as Debug and release packages

      android{    buildTypes {        debug {            ...        }        release {            ...        }    }}

      Debug and release are the two build type that are gradle by default, and in the project auto-generated buildconfig, the difference is as follows:

      // release版本生成的BuildConfig特性信息public final class BuildConfig {    public static final boolean DEBUG = false;    public static final String BUILD_TYPE = "release";}// debug版本生成的BuildConfig特性信息public final class BuildConfig {    public static final boolean DEBUG = true;    public static final String BUILD_TYPE = "debug";}
    • Customize a different build type, such as

      android{    buildTypes {        debug {            ...        }        release {            ...        }        beta {            ...        }    }}
5. Productflavors
    • Packages for generating different channels

      android {      productFlavors {        xiaomi {}        baidu {}        wandoujia {}        _360 {}        // 或“"360"{}”,数字需下划线开头或加上双引号    }}

      Execution ./gradlew assembleRelease , will be out of all channels of release package;
      Execution ./gradlew assembleWandoujia , will be out of the Pea pod Channel release and debug version of the package;
      Perform ./gradlew assembleWandoujiaRelease a release package that will generate pea pods.

      Therefore, you can combine BuildType and productflavor to generate different build variants, which is a combination of different types and channels

6. Signingconfigs
    • Signature configuration, release compilation type configuration such as:

      release {    storeFile file("../yourapp.keystore") //签名证书文件    storePassword "your password"         //签名证书密码    keyAlias "your alias"                 //别名    keyPassword "your password"           //别名密码}

      Of course, signature information can be hidden by reading the configuration file (see previous article) and configuration file ignore, which is not detailed in this article.

7. Meta-infFile
    • Some information to store the signature, add an empty file within the Meta-inf directory, and the app does not need to be re-signed.
Second, multi-channel packaging configuration 1. General Packaging Solutions
    Generate Package Time Def releasetime () {return new Date (). Format ("Yyyy-mm-dd", Timezone.gettimezone ("UTC"))} and roid {compilesdkversion buildtoolsversion "23.0.3" defaultconfig {applicationid "com. Company.appname "Minsdkversion targetsdkversion versioncode 1 Versionnam E "1.0"//default channel for official website manifestplaceholders = [Umeng_channel_value: "Official"]} Lintoptio NS {Checkreleasebuilds false Abortonerror false}//configure compiled JDK version compileoptions {sourcecompatibility org.gradle.api.JavaVersion.VERSION_1_7 targetcompatibility Org.gradle.api.Java                Version.version_1_7}//Signature configuration Signingconfigs {debug {//No debug Config                StoreFile file ("${rootdir}/keystores/xqsg_debug.jks")//debug certificate} release { StoreFile file ("${roOtdir}/keystores/xqsg.jks ")//release certificate Storepassword" test "//Signing Certificate Password Keyalias "Test"//alias Keypassword "Test"/ /alias Password}} buildtypes {debug {Buildconfigfield ("boolean", "log_on", "t Rue ")//config log switch by compile type Versionnamesuffix"-debug "//package name suffix"-debug "minifye Nabled false//whether to confuse zipalignenabled false//zipalig n Optimize shrinkresources false//Remove useless resource file Signingconfig signin Gconfigs.debug//Signing with Debug certificate} release {Buildconfigfield "boolean", "L Og_on "," false "//does not display log minifyenabled true//Open confusion Zipalignena               Bled true              Turn on Zipalign optimization shrinkresources true//Remove useless resource file, this entry only when the confusion is turned on Effective Proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.pro ' signin  Gconfig Signingconfigs.release//Signing with Release certificate//multi-channel packaging configuration Applicationvariants.all {variant-variant.outputs.each {output, def outputFile = Output.out                            Putfile if (outputFile! = null && outputFile.name.endsWith ('. apk ')) { The output apk name is called test_v1.0_2016-08-15_wandoujia.apk def fileName = "Test_v${defaultconfig.versi Onname}_${releasetime ()}_${variant.productflavors[0].name}.apk "output.outputfile = new File (ou Tputfile.parent, FileName)}}}}// Channel Configuration ProductflavoRS {wandoujia {} _360 {} Baidu {} Xiaomi {}} productflavors.al     L {flavor-flavor.manifestplaceholders = [Umeng_channel_value:name]//dynamically modify channel name in Androidmanifest} }

Note: The above log switch allows you to get the value of the variable in Java code, such as:

if(BuildConfig.LOG_ON){    Log.d("test","xxx");    }
2. US Package Solution

Implementation principle: Android App install package apk file is a compressed file, you can change the suffix to zip direct decompression. After extracting, you will find a Meta-inf directory under the root directory. If you add an empty file within the Meta-inf directory, the app does not need to be re-signed. Therefore, you can uniquely identify a channel by adding different empty files for different channels of application.
In this way, one channel package per dozen only needs to copy an apk and add an empty file named after the channel number in Meta-inf.

    • Android Automation Tour-Generate channel packs
    • American group Android Automation Tour-adaptation channel pack
    • Implementation Reference 1
    • Implementation Reference 2
Learn more about Android Studio Series Tutorial six –gradle multi-channel packaged Android product development (v) –> multi-channel packaging more convenient Android multi-channel packaging Android Packaging series-multi-channel fast packaging

View Original: http://blog.csdn.net/u010818425/article/details/52319382

Gradle Combat: Android Multi-channel Packaging solution Summary

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.