banner
jzman

jzman

Coding、思考、自觉。
github

Android Gradle Plugin in the Gradle Series

Learned the basics of Gradle and Gradle plugin-related knowledge in previous articles. Please read the following articles for more information about Gradle and its plugins:

The main purpose of learning Gradle is to better use it in Android development. This article focuses on the knowledge related to the Android Gradle plugin, including:

  1. Understanding the Android Gradle plugin
  2. Classifying Android Gradle plugins
  3. Using the Android Gradle plugin
  4. Android Gradle project structure
  5. Basic configuration of Android Gradle
  6. Android Gradle tasks

Understanding the Android Gradle Plugin#

As the name suggests, the Android Gradle plugin is a Gradle plugin used for building Android projects. It is developed by the Google Android development team. Android Studio, the Android development IDE, uses Gradle to build projects. The advantages of the Android Gradle plugin are as follows:

  1. Convenient code and resource reuse
  2. Easier creation of derivative versions of applications, such as multi-channel packaging
  3. Easy configuration, extensibility, and customization of the build process
  4. Deep integration between Android Studio and Gradle

Classifying Android Gradle Plugins#

Android plugins are classified based on the attributes of Android projects. Android projects can be divided into three categories:

  1. App projects: Generates runnable APK files
  2. Library projects: Generates AAR files that can be used by other app projects. Similar to JAR files, they contain Android resource files.
  3. Test projects: Used for testing app or library projects.

Correspondingly, there are three types of Android Gradle plugins: App plugin, Library plugin, and Test plugin. The plugin IDs for these three types are as follows:

// App plugin ID
com.android.application
// Library plugin ID
com.android.library
// Test plugin ID
com.android.test

The most commonly used plugins are the App plugin and Library plugin. With these plugins, you can build Android projects in Android Studio.

Using the Android Gradle Plugin#

When using Gradle plugins, the plugin ID is used as a unique identifier. If it is a third-party plugin, its classpath must be specified in the buildscript{} section. The Android Gradle plugin is a third-party plugin, so its classpath must be specified. The Android Gradle plugin is hosted on jcenter, so the corresponding repository must be specified. Here is an example:

buildscript {
    // Configure the plugin repository
    repositories {
        google()
        jcenter()
    }
    // Configure the dependencies
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
    }
}

After configuring the repository and dependencies for the third-party plugin, you can use the apply keyword to apply the plugin. Here is an example of using the App plugin:

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

android {
    // Compile SDK version
    compileSdkVersion 26
    // Build tools version
    buildToolsVersion '26.0.2'
    // ...
}

dependencies {
    // ...
}

In Android development, the plugin repository and dependencies are usually configured in the root build.gradle file of the project. The sub-module projects do not need to be configured separately and can be used directly. This example only shows the usage of the App plugin, but the usage of the Library plugin and Test plugin is similar. Just specify the corresponding plugin ID.

Android Gradle Project Structure#

The following is the directory structure of an Android project module created in Android Studio. This is the basic directory structure of an Android project:

app
├─libs
├─proguard-rules.pro
├─build.gradle
└─src
    ├─androidTest
    │  └─java

    ├─main
    │  ├─AndroidManifest.xml
    │  ├─java
    │  └─res

    └─test
        └─java

In the above directory structure, src contains three source sets: main, androidTest, and test. main represents the source code and resource directory of the app, androidTest and test represent the directories for Android-related testing code. proguard-rules.pro is the obfuscation file for the project, libs is used to store library files such as JAR and AAR files, and build.gradle is the configuration file for the project.

Basic Configuration of Android Gradle#

To demonstrate the basic configuration of the Android Gradle plugin, let's create an Android project using Android Studio. Here is an example configuration file for the app module:

// Use the Android Gradle plugin
apply plugin: 'com.android.application'
// android{} is the entry point for Android project configuration
android {
    /**
     * Specify the Android SDK version to compile against.
     * Equivalent to compileSdkVersion android-26
     * android.compileSdkVersion = 26
     * android.compileSdkVersion = 'android-26'
     */
    compileSdkVersion 26
    // Specify the build tools version. Can also use the buildToolsVersion property.
    buildToolsVersion '26.0.2'
    /**
     * Default configuration. defaultConfig is a ProductFlavor that can generate different APKs based on different requirements.
     * If no custom ProductFlavor is configured, the default configuration will be used to generate the APK.
     * These configurations, such as applicationId, are properties of ProductFlavor.
     */
    defaultConfig {
        // Configure the unique package name
        applicationId "com.manu.androidgradleplugin"
        // Minimum supported Android version
        minSdkVersion 19
        // Configure the target Android version for the application
        targetSdkVersion 26
        // Version code for controlling application updates
        versionCode 1
        // Version name displayed to users
        versionName "1.0"
        // Used for testing
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    /**
     * buildTypes is a NamedDomainObjectContainer, similar to SourceSet.
     * buildTypes can be used to define different build types that Gradle will automatically create, such as the default release and debug types.
     */
    buildTypes {
        release {
            // Enable obfuscation for the build type
            minifyEnabled false
            // If obfuscation is enabled, use the appropriate 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.constraint: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'
    implementation files('libs/plugin.jar')
}

The above configuration file highlights some places where the Android Gradle plugin needs to be configured. It is an understanding and learning of the Android Gradle plugin and the project configuration file build.gradle from the perspective of learning Gradle. This concludes the basic configuration of the Android Gradle plugin. More will be added in future articles.

Android Gradle Tasks#

The Android Gradle plugin is based on the Java Gradle plugin and includes some tasks from the Java Gradle plugin, such as assemble, check, and build. In addition, the Android Gradle plugin adds some new tasks specific to Android development, such as connectedCheck, deviceCheck, lint, install, and uninstall. You can view some of these tasks in Android Studio by selecting the Gradle tab on the right side of Android Studio. Here is an example:

image

This article provides a preliminary understanding of the Android Gradle plugin from the perspective of learning Gradle. More articles will be added in the future to continue learning about Android Gradle.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.