Android Gradle study notes (seven): Android Gradle plugin

We know that Android is actually a Gradle Gradle of a third-party plug-ins, it was developed by Google's Android team, based Gradle build, and Android Studio perfect match. Compared to the old build system (for example: Eclipse + Ant), it is more flexible and easier to configure, but also easy to create a derivative version, which is our common multi-channel package.

The following is a description of its official Android:

(1) can be easily reuse code and resources.

(2) you can easily create a derivative version of the application, such as: creating multiple apk, to create applications with different functions.

(3) can be easily arranged, extensions and custom build process.

(4) and can be seamlessly integrated IDE.

A, Android Gradle plugin Categories

Categories Android Gradle plugin is based on Android project attribute categories. There are three types of works in Android, App is a class of engineering, it can generate a running application apk. One is the Library project, which can generate AAR public works contracted out to other App, just as our jar package, but it contains information about Android's resources, it is a special jar package. The last category is Test test engineering for the project or the Library of App works for unit testing.

App plug-id: com.android.application

Library 插件 id : com.android.library

Test Plug-id: com.android.test

Three different plug-in applications above, you can configure our project is a project Android App, Android Library or a project, or an Android Test test project. Then we can cooperate with Android Studio, compile them, testing, publishing and other operations.

Second, the application Android Gradle plugin

Android Gradle Gradle plug-in as a third-party plug-ins, which is hosted on Jcenter, before you apply, we must first configure dependent classpath, so when we use plug-ins, Gradle to find them:

buildscript {
    repositories {
        jcenter()
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.0'
        
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

We configured the warehouse jcenter, so when we depend on the configuration of the time, Gradle will go to the warehouse to find our dependence. Then we declare the dependencies {} configuration, we need that Android Gradle 3.4.0 version of the plug.

buildscript {} This configuration section can be written to the root of the project build.gradle script file, so that all the sub-projects will not have to repeat the configuration. After the above configuration is good, we can apply our Android Gradle plugin:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
}

android {} Android is an extension of the type provided by the plug, it allows us to customize Android Gradle project.

The above example is a project App plug-ins and applications Android Library Android Test plug-in is similar, only you need to replace the corresponding id.

Three, Android Gradle use the configuration

1. compileSdkVersion 

compileSdkVersion project is configured to compile Android version of the SDK.

2. buildToolsVersion

buildToolsVersion the Android build tools version. This tool can be found in the Android SDK directory, which is a kit that includes appt, dex and other tools.

3. defaultConfig

defaultConfig is the default configuration. It is a ProductFlavor. ProductFlavor allows us to generate a plurality of different APK package while depending on the circumstances, such as multi-channel packing. If not for ProductFlavor our custom configured separately, it will use the default configuration for this defaultConfig ProductFlavor. The main configuration includes the following fields:

  • applicationId: package name configuration.
  • minSdkVersion: Android API Level Minimum supported.
  • targetSdkVersion: Based on the development of a given version of Android.
  • versionCode: App application build number, generally used to control APP upgrade.
  • versionName: name App version of the application.

4. buildTypes

buildTypes is a domain object. We can add buildTypes {} where we need to build any number of types, such as debug, Gradle will help us to automatically create a corresponding buildTypes, our name is the name defined.

Common types of builds:

  • minifyEnabled: whether to enable confusion for the building type, false representation is not enabled, enable can be set to true.
  • proguardFiles: When we opened confusion, proguard configuration file used, through which we can configure how we proguard confusion, such as the level of confusion, which classes and methods without confusion and so on. It corresponds proguardFiles BuildType method can accept a variable argument.

Four, Android Gradle task

Android is based on Java plug-in plug-ins, basically contains all the features of Java plug-ins, including inherited tasks, such as assemble, check, build and so on. In addition, Android in the categories also added connectedCheck, deviceCheck, lint, install, uninstall tasks, these are part of Android-specific features. among them:

  • connectedCheck: Run check or checks on all the devices connected to the emulator.
  • deviceCheck: remote devices to run checks via the API.
  • lint: Run lint checks on all ProductFlavor.
  • install & uninstall: you can install or uninstall your APP directly on our connected devices.

Our common task is general: build, assemble, clean, lint, check.

 

Guess you like

Origin www.cnblogs.com/renhui/p/10961999.html