Android multi-channel packaging: Use Gradle, Android Studio, and androidgradle

Source: Internet
Author: User

Android multi-channel packaging: Use Gradle, Android Studio, and androidgradle

Reprinted please indicate the source:

Http://blog.csdn.net/jjwwmlp456/article/details/45057067 ----------------- busy repeat


Gradle: this is complicated, but in Android, we know how to use it, and how it works is enough.

Gradle DSL, domain-specific languages (DSL), address: http://gradle.org/docs/2.3/dsl/

Gradle User Guide, address: http://gradle.org/docs/2.3/userguide/userguide.html

Android-Gradle-DSL Android integrates Gradle's DSL

: Https://developer.android.com/shareables/sdk-tools/android-gradle-plugin-dsl.zip

Brief Introduction Android-Gradle build plug-in official address: https://developer.android.com/tools/building/plugin-for-gradle.html

Android-Gradle build plug-in official address: http://tools.android.com/tech-docs/new-build-system


Basic Form of build. gradle

Android Studio creates a project under which multiple moudle can be created.

A project automatically generates a build. gradle, and a build. gradle is automatically generated under each module (this article mainly discusses build. gradle in the module)

Module/build. gradle:

Apply plugin: 'com. android. application '// enable the android application plug-in android {compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig {applicationId "com. stone. testbuild "minSdkVersion 8 targetSdkVersion 22 versionCode 1 versionName" 1.0 "} buildTypes {release {minifyEnabled false proguardFiles getdefadefaproguardfile('proguard-android.txt '), 'proguard-rules. pro' }}dependencies {compile fileTree (dir: 'libs', include :['*. jar ']) compile 'com. android. support: appcompat-v7: 22.0.0 '}

Android-Gradle DSL Introduction The android {} in the above example is what we need to know. The android {} module can contain the following direct configuration items:

  • defaultConfig{} The default configuration is ProductFlavor. It is shared to other ProductFlavor users.
  • SourceSets {} specifies the source file directory, which is of the AndroidSourceSet type.
  • BuildTypes {} BuildType
  • SigningConfigs {} signature configuration, SigningConfig type
  • ProductFlavors {} Product Style configuration, ProductFlavor type
  • TestOptions {} test configuration, TestOptions type
  • AaptOptions {} aapt configuration, AaptOptions type
  • LintOptions {} lint configuration, LintOptions type
  • DexOptions {} dex configuration, DexOptions type
  • CompileOptions {} compilation configuration, CompileOptions type
  • PackagingOptions {} PackagingOptions type
  • Jacco {} type. Used to set the version of jacco
  • Splits {} Splits type.

In the DSL document, each of the preceding types has its detailed configuration options.


Multi-channel Packaging

Automatic Signature, obfuscation, packaging, and injection.

For example, module> build. gradle:

Apply plugin: 'com. android. application '/* defines a method. If def declaration is used only, the return value can be directly written into String or def Stringgradle of any type (automatically determined). groovy supports the following default packages: java. io. * java. lang. * java. math. bigDecimal java. math. bigInteger java.net. * java. util. * groovy. lang. * groovy. util. **/def String computeVersionName () {return "8.8.8"} android {compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig {applicationId "com. stone. myappli Cation "minSdkVersion 8 targetSdkVersion 22 versionCode 1 versionName computeVersionName () // use the externally defined method/* manifestPlaceholders manifest to use placeholders, such as: <... android: name = "$ {YOUR_APP_KEY}"> Replace [YOUR_APP_KEY: "value"] */manifestPlaceholders = [YOUR_APP_KEY: "umeng background appkey"]} signingConfigs {// gradle assumerelease/* can define multiple signature configuration items, as shown in myConfig */myConfig {storeFile file ("stone. keystore ") sto RePassword "mypasswd" // storePassword System. console (). readLine ("\ nKeystore password:") keyAlias "stone" keyPassword "mypasswd" // keyPassword System. console (). readLine ("\ nKey password:") }}buildtypes {/* multiple buildType items can be configured, as shown in the following release, debug, aabbcc */release {minifyEnabled true // Translation: enabled. Enable the obfuscator // obfuscator file: sdk/tools/proguard/proguard-android.txt and proguard-rules.pro under the current module // proguardFiles getDefaultProguardFile('proguard-android-optimize.txt '), 'proguard-rules. pro' // getdefaproproguardfile('proguard-android.txt '), // getdefaproproguardfile('proguard-android-optimize.txt'), signingConfig signingConfigs. myConfig zipAlignEnabled true // zip Optimization after obfuscation. The default value is true. Do not write. When trueis not displayed, unaligned.apk} debug {debuggable true // buildType configuration with debug enabled} aabbcc {// custom configuration is not generated. Signature items are not configured, therefore, the unsigned apk multiDexEnabled true} productFlavors {/* productFlavors-Product Style is generated: configuration of different products, based on the configuration items under the above public configuration item defaultConfig and the configuration items in buildTypes {}, it will form a full join relationship similar to that in SQL. After executing the $ gradle build command, it will generate: module-flavor1-release-unaligned.apk module-flavor1-release.apk module-flavor1-debug-unaligned.apk module-fl Avor1-debug.apk module-flavor1-aabbcc.apk ...flavor2..apk applicationId is used to identify a unique identifier on the Google Play store by default not configured, with the AndroidManifest of the app. in xml, only the package attribute values in <manifest> are replaced. Other values are not affected */flavor1 {proguardFiles 'proguard-rules. pro 'applicationid "com. stone. myapplication. pro "// For example, Professional Edition manifestPlaceholders = [channelID:" Baidu application platform "]} flavor2 {proguardFile 'proguard-rules. pro 'applicationid "com. stone. myapplication. Free "// For example, free version manifestPlaceholders = [channelID:" "] }}dependencies {// Local binary dependency Local jar package compile fileTree (dir: 'libs', include: ['*. jar ']) // Module dependency references compile project (': eventbuslib ')/* Remote binary dependency download to local group: name: version: configure the jar package in the remote repository. During packaging, check whether there is a local package. If not, download it to the local device. */Compile 'com. android. support: appcompat-v7: 22.0.0 'compile 'com. android. support: support-v4: 22.0.0 'compile 'com. android. support: cardview-v7: 22.0.0 'compile 'com. android. support: recyclerview-v7: 22.0.0 '}
Multi-channel is actually defining multiple flavor. Use the manifestPlaceholders configuration to replace the placeholder $ {} in manifest {}.

Manifest. xml:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.stone.myapplication">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme">        <activity            android:name=".MainActivity"            android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>            <meta-data                android:name="UMENG_APPKEY"                android:value="${YOUR_APP_KEY}" />            <meta-data                android:name="channelName"                android:value="${channelID}" />        </activity>    </application></manifest>

Finally, execute the $ gradle build command.


Add an operation to reference the remote jar package, as shown in:





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.