Android Gradle plugin Development guide for beginners

  • Android Gradle Plugin development guide (a), explains the complete process of developing Gradle Plugin
  • Android Gradle Plugin development guide (ii), for Android Gradle Plugin development practice
  • Android Gradle plugin development guide (3), how to publish the plugin to jCenter

The target

In Tutorial 1 we implemented a simple Gradle universal plugin that can be used in any project built on Gradle. In tutorial 2, we need to make Gradle and Android really work together. We need to implement a plugin that changes the Apk output name of Gradle, so that the Apk file name contains the application name, VersionCode, VersionName, BuildType (BuildType: Debug/Release), package date, and Flavor information. Add the qq and Baidu Flavor information to our app Module first (the Flavor name is optional, baidu and QQ are just examples, don’t be single-track), so that we can verify the plug-in function later. There are two ways:

  1. If you are familiar with the Android Gradle plugin, write the variants directly into the build. Gradle script of your app

  1. You can also use the Project Structure window Settings provided by Android Studio





Reference Documents:Developer.android.com/studio/buil…

implementation

There are two important concepts in Gradle build systems. One is the repeatedly mentioned Task, the basic unit of build. The other is Extension, which we’ll use to customize the build process and build artifacts. Many of the information mentioned above, package date, application name belongs to the general information we can get directly, other information belongs to the extension information of Android Gradle plug-in, we want to get these extension information, we have to rely on (implementation) in Android Gradle plug-in, For more information about Android Gradle Plugin extensions, see the Android Plugin DSL Reference

Configure plug-in Module

Follow the steps in Guide 1 to create a new plugin Module named apkrename -Plugin and add a dependency on the Android Gradle plugin

dependencies {
    implementation 'com. Android. Tools. Build: gradle: 4.1.0'
}
Copy the code

Once the dependencies are added, we can implement our functionality in the apply function of the plug-in

Implement plug-in functions

We define the file name format of Apk as: Application name -versioncode -VersionCode -VersionCode – VersionName- BuildType-BuildType−Flavor-$date. apk, Let’s get all the information we need step by step:

1. Obtain the application name

public void apply(Project project) {
    // Application name
    def projectName = project.name
}
Copy the code

2. Get the date

public void apply(Project project) {
    // Application name
    def projectName = project.name
    // Package date in the format of year month day hour minute second
    def buildTime = new Date().format("yyyyMMddHHmmss")}Copy the code

3. Obtain the extension information of Android Gradle Plugin

The apply function entry project object contains all the extension information of the project. We only need the extension information of the Android Gradle Plugin here. Look at the document:Extension types

public void apply(Project project) {
    // Application name
    def projectName = project.name
    // Package date in the format of year month day hour minute second
    def buildTime = new Date().format("yyyyMMddHHmmss")
    / / project there are a lot of extension information, we need the expansion of the Android related information here, com. Android. Build. Gradle. AppExtension
    def appExtension = project.extensions.getByType(AppExtension)
}
Copy the code

appExtensionContains all extension information for Android Gradle Plugin, whereapplicationVariantsA property is a collection of information about all build variants, and we can iterate through this collection to get each variant instance and, in turn, other information about variants.



Complete information acquisition code:

public void apply(Project project) {
    // Application name
    def projectName = project.name
    // Package date in the format of year month day hour minute second
    def buildTime = new Date().format("yyyyMMddHHmmss")
    / / project there are a lot of extension information, we need the expansion of the Android related information here, com. Android. Build. Gradle. AppExtension
    def appExtension = project.extensions.getByType(AppExtension)
    // Go through all variants in the extension information, and combine the Apk file name according to the information of each variant
    appExtension.applicationVariants.all { variant ->
        // The actual type of variant is ApplicationVariant
        // val applicationVariants: DomainObjectSet<ApplicationVariant> =
        // dslServices.domainObjectSet(ApplicationVariant::class.java)
        def versionCode = ((ApplicationVariant) variant).versionCode
        def versionName = ((ApplicationVariant) variant).versionName
        def buildType = ((ApplicationVariant) variant).buildType.name
        def flavor = ((ApplicationVariant) variant).flavorName
    }
}
Copy the code

If you look at the Android Plugin DSL reference, you won’t find the applicationVariants description, so you won’t be able to count on the documentation at this point. Luckily, Command +B figured out where the method declaration was. It turns out that the ApplicationVariant element type is ApplicationVariant and that ApplicationVariant is just an interface implemented by ApplicationVariantImpl. With this information, other information will follow. By the way, we also found the attribute outputs that we will use later.

4. Change the apK output name

The apK file name belongs to the output attribute of the task, which must be in the previous outputs. Similarly, command+B found that the outputs element of the outputs was BaseVariantOutput. BaseVariantOutput is an interface. ApkVariantOutputImpl: ApkVariantOutputImpl: ApkVariantOutputImpl: ApkVariantOutputImpl Here is the setOutputFileName method we need:

public void apply(Project project) {
    // Application name
    def projectName = project.name
    // Package date in the format of year month day hour minute second
    def buildTime = new Date().format("yyyyMMddHHmmss")
    / / project there are a lot of extension information, we need the expansion of the Android related information here, com. Android. Build. Gradle. AppExtension
    def appExtension = project.extensions.getByType(AppExtension)
    // Go through all variants in the extension information, and combine the Apk file name according to the information of each variant
    appExtension.applicationVariants.all { variant ->
        // The actual type of variant is ApplicationVariant
        // val applicationVariants: DomainObjectSet<ApplicationVariant> =
        // dslServices.domainObjectSet(ApplicationVariant::class.java)
        def versionCode = ((ApplicationVariant) variant).versionCode
        def versionName = ((ApplicationVariant) variant).versionName
        def buildType = ((ApplicationVariant) variant).buildType.name
        def flavor = ((ApplicationVariant) variant).flavorName
        ((ApplicationVariant) variant).outputs.all { output ->
            // The variant outputs other files besides apk. Here we only change the file name of apk
            def outputFile = ((BaseVariantOutput) output).outputFile
            if(outputFile ! =null && outputFile.name.endsWith(".apk")) {
                ((ApkVariantOutput) output).outputFileName = "$projectName-$versionCode-$versionName-$buildType-$flavor-${buildTime}.apk"}}}}Copy the code

validation

Once the plugin functionality is developed, publish the plugin to the repo directory (no, go back to Tutorial 1) and apply the apkRename plugin to your app:

  • build.gradle
buildscript {
    repositories {
        ...
        // MavenLocal ()
        // mavenLocal()
        // A custom publishing address
        maven { url('./repo') }
    }
    dependencies {
        ...
        classpath "Com. Lenebf. Plugin: apk - rename: 0.0.1." "}}Copy the code
  • app/build.gradle
plugins {
    ...
    id 'com.lenebf.plugin.apk-rename'
}
Copy the code

performassembleDebugTask to see if the output APK name is what we expect



Perfect as always. I admire myself

code

The code address in the article: github.com/lenebf/Grad…