Frist VariantProcessor

Introducing Booster

准备好工程后,接下来在 Java Library 工程或者 Android 工程的 buildSrc 目录中的 build.gradle 文件中,引入 Booster 依赖:

buildscript {
    ext {
        agp_version = "3.5.0"
        kotlin_version = "1.5.31"
        booster_version = "4.16.3"
    }
    repositories {
        mavenCentral()
        google()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'kotlin'
apply plugin: 'kotlin-kapt'

repositories {
    mavenCentral()
    google()
}

sourceSets {
    main {
        java {
            srcDirs += []
        }
        kotlin {
            srcDirs += ['src/main/kotlin', 'src/main/java']
        }
    }
    test {
        java {
            srcDirs += []
        }
        kotlin {
            srcDirs += ['src/main/kotlin', 'src/main/java']
        }
    }
}

compileKotlin {
    kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8
}

compileTestKotlin {
    kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8
}

dependencies {
    api gradleApi()
    /* 👇👇👇👇 引用这三个模块 👇👇👇👇 */
    kapt "com.google.auto.service:auto-service:1.0"
    api 'com.android.tools.build:gradle:$agp_version'
    api "com.didiglobal.booster:booster-api:$booster_version"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

Custom VariantProcessor

package io.johnsonlee.booster.demo

import com.android.build.gradle.api.BaseVariant
import com.didiglobal.booster.gradle.project
import com.didiglobal.booster.task.spi.VariantProcessor
import com.google.auto.service.AutoService

@AutoService(VariantProcessor::class)
class SimpleVariantProcessor(val project: Project) : VariantProcessor {

    init {
        println("${project.name}")
    }

    override fun process(variant: BaseVariant) {
        println("${variant.project.name}: ${variant.name}")
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

TIP

VariantProcessor 构造方法中的 Project 参数是可选的

Verifying FirstVariantProcessor

Android 工程下,执行 assemble 任务:

$ ./gradlew assembleDebug
1

观察控制台的标准输出,是否有如下内容:

app: debug
app: release
1
2