Personal wechat public account practice, welcome to follow the exchange.

Through the previous several articles to learn the basic knowledge of Gradle and Gradle plug-in related knowledge, about Gradle and its plug-in related knowledge please read the following articles:

  • Gradle series first know Gradle
  • Gradle: Groovy Basics
  • The foundation of Gradle build scripts
  • Gradle series of Gradle task
  • Gradle plugin for Gradle series
  • Java Gradle plugin for Gradle series
  • Android Gradle plugin
  • Gradle series Android Gradle basic configuration
  • Android Gradle advanced configuration
  • The Gradle series builds the Maven private server from scratch

The purpose of learning Gradle is to better use Gradle in Android development, this article is the main content of Android Gradle plug-in related knowledge, the main content is as follows:

  1. Learn about Android Gradle plugins
  2. Android Gradle plugin classification
  3. Use the Android Gradle plugin
  4. Android Gradle project directory
  5. Android Gradle basic configuration
  6. Android Gradle task

Learn about Android Gradle plugins

As the name implies, Android Gradle plugin is a plugin for Android building projects. This plugin is developed by the Google Android development team. Android Studio uses Gradle to build projects. The following are the advantages of Android Gradle plugin:

  1. Easy to reuse code and resources;
  2. Easier to create derivative versions of applications, such as multi-channel packaging;
  3. Easy configuration. You can extend and customize the build process
  4. Android Studio is deeply integrated with Gradle

Android Gradle plugin classification

Android plug-ins are classified according to the attributes of Android projects. Android projects are divided into three categories, as follows:

  1. App application project: can generate running APK;
  2. Aar can be generated for use by other App application projects. It is used in the same way as JAR, which has related Android resource files
  3. Test Project: Used to Test an App application or Library project.

There are three different Android Gradle plugins. The three types of plugins are App plugin, Library plugin and Test plugin. The plugin ID is as follows:

/ / App id
com.android.application
/ / Library plug-in id
com.android.library
/ / Test plug-in
com.android.test
Copy the code

The most commonly used plug-ins are the App and Library plug-ins, which are used to build Android projects in Android Studio.

Use the Android Gradle plugin

If it is a third party plugin, you must specify its classpath in buildScript {}. Android Gradle plugin is a third party plugin, you must specify its classpath. Android Gradle plugin is hosted on jCenter, so you need to specify the corresponding repository when using it. For details, see the following:

buildscript {
    // Configure the plug-in repository
    repositories {
        google()
        jcenter()
    }
    // Configure dependencies
    dependencies {
        classpath 'com. Android. Tools. Build: gradle: 3.0.1'}}Copy the code

Configure the third-party plug-in repository and dependencies, and then use the apply plugin as follows:

// Use the App plugin
apply plugin: 'com.android.application'

android {
    // Compile the version of the Android SDK that you depend on
    compileSdkVersion 26
    // Build the version of the tool
    buildToolsVersion '26.0.2'
    / /...
}

dependencies {
    / /...
}
Copy the code

In Android development, the repository and dependencies of plugins are usually configured in the build.gradle file under the root project. The sub-module project does not need to be configured and can be used directly. The use of Libray plugin is the same as the use of Test plugin. You can specify the corresponding plugin ID when using Libray.

Android Gradle project directory

Here is the directory where Android Studio creates the Module of an Android project. This is also the basic directory structure of an Android project.

App ├ ─ libs ├ ─ proguard - rules. Pro ├ ─ build. Gradle └ ─ SRC ├ ─ androidTest │ └ ─ Java │ ├ ─ the main │ ├ ─ AndroidManifest. XML │ ├ ─ Java │ └ ─ res │ └ ─ test └ ─ JavaCopy the code

This directory under the SRC main, androidTest, the test is three SourceSet, said the main App source code and resource directory, android and android test said related test code directory, In addition, proguard-rules.pro is the obconfusion file of the current project, libs is used to store jar, AAR and other library files, and build.gradle is used for the current configuration file.

Android Gradle basic configuration

In order to introduce the basic configuration of Android Gradle plugin, create an Android project using Android Studio. The following app configuration file is as follows:

// Use the Android Gradle plugin
apply plugin: 'com.android.application'
// Android {} is the gateway to the Android project configuration
android {
    / * * * specify compilation depends on the Android SDK version, with * compileSdkVersion Android pileSdkVersion = 26-26 * android.com * android.compileSdkVersion = 'android-26' */
    compileSdkVersion 26
    // Specify the build tool version, which can also be set using the buildToolsVersion property value
    buildToolsVersion '26.0.2'
    /** * The default configuration is that defaultConfig is a ProductFlavor and can be used to generate different Apk values based on different requirements. For example, applicationID is a property of the productFlavor */
    defaultConfig {
        // Configure the unique package name
        applicationId "com.manu.androidgradleplugin"
        // The lowest supported Version of Android
        minSdkVersion 19
        // Configure the target Android version of the application
        targetSdkVersion 26
        // Used to control the version number of the application upgrade
        versionCode 1
        // The version name that the user sees
        versionName "1.0"
        // Use it for testing
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    / * * * is a NamedDomainObjectContainer buildTypes type, is a domain object, similar SourceSet (source) * buildTypes can build custom need type, Gradle automatically creates a BuildType such as release, debug */ by default
    buildTypes {
        release {
            // Set whether obfuscation is enabled for build types
            minifyEnabled false
            // If obfuscation is enabled, use the corresponding obfuscation file
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {}}// Dependency configuration
dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')Implementation 'com. Android. Support: appcompat - v7:26.1.0' implementation 'com. Android. Support. The constraint, the constraint - layout: 1.1.2' testImplementation junit: junit: '4.12' androidTestImplementation 'com. Android. Support. Test: runner: 1.0.2' androidTestImplementation 'com. Android. Support. Test. Espresso: espresso - core: 3.0.2' implementationfiles('libs/plugin.jar')
}

Copy the code

The above configuration file has marked some places where Android Gradle plug-in needs to be configured, which is also an understanding and learning of Android Gradle plug-in and project configuration file build. Gradle. This is the end of the basic configuration of Android Gradle plug-in, and will be added in the future.

Android Gradle task

Android Gradle plug-in is based on Java Gradle plug-in, Android Gradle plug-in contains some tasks of Java Gradle plug-in, such as assemble, check, build and other tasks, in addition, The Android Gradle plugin also adds some new tasks, such as connectedCheck, deviceCheck, Lint, install, unInstall, etc. Select Gradle TAB on the right side of Android Studio to view some tasks in Android development. Refer to the following:

This article is a preliminary understanding of Android Gradle plug-in knowledge from the perspective of learning Gradle, the following article will continue to learn Android Gradle. You can follow the public number: practice (Jzman-blog), communication and learning together.