This article will cover the use of the Android Gradle plugin in a real project, using it as an example

1, cited as Android Gradle plugin

Build. Gradle:

Dependecies buildscript {repositories {jcenter ()} {the classpath 'com. Android. View the build: gradle: 1.5.0'}}Copy the code

To import third-party Gradle libraries, first use classpath in the root build.gradle to import paths. Since it is configured in the root project, all sub-projects do not need to be configured

For other modules, such as the App module, in the build.gradle file under the module:

Apply plugin: 'com.android.application' android{compileSdkVersion 23 buildToolsVersion "23.0.1"}Copy the code

Apply plugin: ‘com.android. Library ‘apply plugin: ‘com.android. Library’ apply plugin: ‘com.android. ‘com.android.test’ creates the test project

2. Common Android configuration

Build the SDK version and build the tool version

Android {// Build android SDK version compileSdkVersion 23 // Build Android tool version buildToolsVersion "23.0.1"}Copy the code

DefaultConfig: The default, which is a ProductFlavor

DefaultConfig {// Config package name applicationId "com.breeze. Qrcode "// Minimum supported Android version minSdkVersion 15 // Which Android version is developed based on TargetSdkVersion 26 // versionCode 1 // versionName "1.0.0"}Copy the code

Configuring Signature Information

SigningConfigs {release {// Path to the signing certificate file storeFile file("myrelease.keystore") // Password of the signing certificate file storePassword "password" // keyAlias in the signing certificate keyAlias "Alias" // keyPassword in the signing certificate keyPassword "password"} // configure multiple debug {//.... SigningConfig signingConfigs.release}Copy the code

Type of application built

Android {buildTypes {release {// Whether to confuse minifyEnabled true // Whether to enable automatic splitting of multiple Dex multiDexEnabled true // Whether to automatically clean up unused resources, By default, no false shrinkResources true // signingConfig SigningConfigs. release // Obturate file proguardFiles getDefaultProguardFile('proguard-android.txt'), 'ProGuard-rules.pro' // Enable zip alignment zipAlignEnabled true} // Configure other compilation types debug {// Do not confuse minifyEnabled false zipAlignEnabled true signingConfig signingConfigs.debug } } }Copy the code

Changing the name of the generated APK starts with a concept: variant: a combination of packaging channels and compilation types, corresponding to an APK package

this.afterEvaluate { tasks['build'].doLast { android.applicationVariants.all { variant -> variant.outputs.each { output-> def fileName = "xxxxx.apk" if(variant.buildType.isDebuggable()) { fileName = "yyyy.apk" } def outputFile = output.outputFile File targetFile = new File(outputFile.parent, fileName) if (targetFile.exists()) { targetFile.delete() } if (outputFile ! = null && outputFile.name.endsWith(".apk")) { output.outputFile.renameTo(targetFile) } } } } }Copy the code

Multi-channel packaging

android { productFlavors { google { applicationId "com.xxx.game.google" manifestPlaceholders.put("UMENG_CHANNEL", <meta-data android:value="${UMENG_CHANNEL}" Android :name="UMENG_CHANNEL"/>Copy the code

3, modular use

Do modular framework will encounter module independent application compilation and run, and module as libiary, and use different Manifest, different source code

If (isBuildModule.toboolean ()) {apply plugin: 'com.android.application' } else { apply plugin: 'com.android.library'} // Configure the corresponding manifest sourceSets {main {if (isBuildModule.toboolean ()) {manifest.srcfile 'src/main/debug/AndroidManifest.xml' } else { manifest.srcFile 'src/main/release/AndroidManifest.xml' // In integrated development mode, exclude all Java files in the debug folder Java {exclude 'debug/**'}}}}Copy the code