Original address:blog.csdn.net/u0108184…

Gradle:

“Gradle” “Gradle” “Gradle” “Gradle” “Gradle” “Gradle” “Gradle” “Gradle”


This post will follow the style of the previous posts, starting with the basic concepts that will help us understand the rest of the post. If you forget the meaning of a concept later in the code, you can review the introduction of the concept by going back to the header.

In this paper, the implementation principle of general batch packaging scheme is introduced in detail, and then the basic implementation principle of Meituan batch packaging is introduced, and several implementation schemes are cited for your reference


I. Introduction of basic concepts

1. package

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" >Copy the code

A package name serves two purposes: it is a unique identifier for the application on the device and in the application market. The second is the package that is used to name your resource class (and the class name that resolves any associated Activity), such as com.any.appname.r

2. PlaceHolder

Variables in the AndroidManifest file are expressed through${PlaceHolder}PlaceHolder represents variables that can be assigned values, such as channels in THE PlaceHolder statistics:

    <meta-data
        android:name="UMENG_CHANNEL"
        android:value="${UMENG_CHANNEL_VALUE}">
    </meta-data>
Copy the code

3. applicationId

Corresponds to the package in AndroidManifest

    android {
        defaultConfig {
            applicationId "com.company.appname"
        }
    }
Copy the code

4. buildTypes

4.1 Used to generate packages of different compilation types, such as debug and Release packages

android{ buildTypes { debug { ... } release { ... }}}Copy the code

Debug and Release are two build types that come with Gradle by default.

4.2 Customizing different Build types, for example

android{ buildTypes { debug { ... } release { ... } beta { ... }}}Copy the code

5. productFlavors

Packages for generating different channels

Android {productFlavors {xiaomi {} Baidu {} wandoujia {} _360 {} // or "360"{}Copy the code

Execute./gradlew assembleRelease, which will type the release package for all channels; Executing./gradlew assembleWandoujia will print the release and debug packages for the Pea pod channel; Executing./gradlew assembleWandoujiaRelease will generate the release package for the pea pod.

BuildType and productFlavor can therefore be combined to produce different Build Variants, or combinations of types and channels

6. signingConfigs

Signature configuration, release compilation type configuration as follows:

release { storeFile file(".. /yourapp.keystore") // Signature certificate file storePassword "your password" // Signature certificate password keyAlias "your alias" // alias keyPassword "your Password "// Alias password}Copy the code

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

7. META-INFfile

Add an empty file to the meta-INF directory. The application does not need to re-sign.


Two, multi-channel packaging configuration

1. General packaging scheme

Def releaseTime() {return new Date(). Format (" YYYY-MM-DD ", Timezone. getTimeZone("UTC"))} Android {compileSdkVersion 23 buildToolsVersion "23.0.3" defaultConfig {applicationId "Com.pion. appName" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" // The default channel is the official website manifestPlaceholders = [UMENG_CHANNEL_VALUE: "Official "]} lintOptions {checkReleaseBuilds false abortOnError false} sourceCompatibility org.gradle.api.JavaVersion.VERSION_1_7 targetCompatibility org.gradle.api.JavaVersion.VERSION_1_7 } 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" // Signature certificate password keyAlias "test" // alias KeyPassword "test" // alias password}} buildTypes {debug {buildConfigField(" Boolean ", "LOG_ON", "True ")// Set the log switch versionNameSuffix "-debug" // The package name suffix is "-debug" minifyEnabled false // Whether to confuse zipAlignEnabled false //Zipalign optimize shrinkResources false // remove unnecessary resource file signingConfig SigningConfigs. debug // use debug certificate signature} release { buildConfigField "boolean", "LOG_ON", "False" // do not display Log minifyEnabled true // enable obfuscated zipAlignEnabled true // Enable Zipalign to optimize shrinkResources true // Remove useless resource files, ProguardFiles getDefaultProguardFile(' proGuard-android.txt '), 'proguard-rules.pro' signingConfig signingConfigs.release variants {variant ->  variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile ! = null && outputfile.name.endswith ('.apk')) {// The output apK name is test_v1.0_2016-08-15_wandoujia.apk def fileName = "test_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk" output.outputFile = new File(outputFile.parent, }}}}}) productFlavors {wandoujia {} _360 {} Baidu {} xiaomi {}} productFlavors. All {flavor -> Flavor. ManifestPlaceholders = [UMENG_CHANNEL_VALUE: name] / / dynamically modify the channel name}} in AndroidManifestCopy the code

Note: The value of this variable can be obtained in Java code, as shown in the following example:

if(BuildConfig.LOG_ON){
    Log.d("test","xxx");    
}
Copy the code

2. Meituan packaging scheme

Implementation principle: The Apk file of the Android application installation package is a compressed file. You can change its suffix to zip to decompress the file directly. When unzipped, you will find a meta-INF directory in the root directory. If an empty file is added to the meta-INF directory, the application does not need to be re-signed. Therefore, a channel can be uniquely identified by adding different empty files for different channel applications.

In this way, for each channel package, just copy an APK and add an empty file named with the channel number to the META-INF.

  • Meituan Android automation tour – channel package generation

  • Meituan Android automation journey – adaptation channel package

  • Implementation Reference 1

  • Implementation Reference 2


Learn more

Android Studio is a quick and easy way to pack your Android products. Gradle is a quick and easy way to pack your Android products