After learning the basics of Gradle and Gradle plugin in the previous articles, please read the following articles for more information about Gradle and its plugins:
- Introduction to Gradle
- Groovy Basics for Gradle
- Basics of Gradle Build Scripts
- Gradle Tasks
- Gradle Plugins
- Java Gradle Plugin
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, and the main topics are as follows:
- Understanding the Android Gradle plugin
- Classification of Android Gradle plugins
- Using the Android Gradle plugin
- Android Gradle project directory
- Basic configuration of Android Gradle
- 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, and the Android development IDE, Android Studio, uses Gradle for project building. The advantages of the Android Gradle plugin are as follows:
- Convenient code and resource reuse
- Easier creation of derivative versions of applications, such as multi-channel packaging
- Easy configuration with the ability to extend and customize the build process
- Deep integration with Android Studio and Gradle
Classification of Android Gradle Plugins#
Android plugins are classified based on the attributes of Android projects. Android projects can be divided into three categories, as follows:
- App projects: These projects can generate runnable APK files.
- Library projects: These projects can generate AAR files for other app projects to use. They can also contain Android resource files, similar to JAR files.
- Test projects: These projects are used for testing app or library projects.
Correspondingly, there are three different Android Gradle plugins: the App plugin, the Library plugin, and the Test plugin. The plugin IDs for these plugins 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 the Library plugin. By using these plugins in Android Studio, you can build Android projects.
Using the Android Gradle Plugin#
Gradle plugins are identified by their IDs. If a plugin is a third-party plugin, its classpath must be specified in the buildscript{}
block. The Android Gradle plugin is a third-party plugin, so its classpath must be specified. Since the Android Gradle plugin is hosted on jcenter, the corresponding repository must be specified. The following 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 apply the plugin using the apply
keyword. Here is an example of applying the App plugin:
// Apply the App plugin
apply plugin: 'com.android.application'
android {
// Compile against the specified Android SDK version
compileSdkVersion 26
// Build tools version
buildToolsVersion '26.0.2'
// ...
}
dependencies {
// ...
}
In Android development, it is common to configure the plugin repository and dependencies in the root build.gradle
file of the project. The sub-module projects do not need to configure them again and can directly use the plugins. This example only shows the usage of the App plugin, but the usage of the Library plugin and the Test plugin is similar. You just need to specify the corresponding plugin ID.
Android Gradle Project Directory#
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 SourceSets: 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, and libs
is used to store library files such as JAR and AAR files. build.gradle
is the configuration file for the project.
Basic Configuration of Android Gradle#
To illustrate 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:
// Apply 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.
* Can also be set using the property value buildToolsVersion.
* Equivalent to android.compileSdkVersion = 26.
* Equivalent to 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.
* The specific configurations, such as applicationID, are properties of ProductFlavor.
*/
defaultConfig {
// Configure the unique package name
applicationId "com.manu.androidgradleplugin"
// Minimum supported Android version
minSdkVersion 19
// 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.
* You can customize the build types that need to be built, and Gradle will automatically create the corresponding BuildType, such as the default release and debug.
*/
buildTypes {
release {
// Enable obfuscation for the build type
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.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 provides an understanding and learning of the Android Gradle plugin and the project configuration file build.gradle
. This covers the basic configuration of the Android Gradle plugin, and 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:
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 further explore Android Gradle.