这个世界里,虽然没有最美好的相遇,却应该有为了相遇或者重逢,所做的最美好的努力。
当需要编写类似代码注入的插件的时候,就需要创建一个名称为buildSrc
的Android插件工程。值得注意的是,这个工程在一个Android工程中,仅仅有一个。并且,这个工程的名称只能叫做buildSrc
。
Gradle会在配置初始化之前,编译BuildSrc这个项目(可以是Java、Groovy、Kotlin),并配置到classpath下。
如果buildSrc
是一个Groovy项目,Gradle默认会自动配置该Gradle插件,在项目中直接apply
开发的插件即可。
buildSrc
。buildSrc
的空文件夹。mkdir buildSrc
。➜ MyApplication git:(master) ✗ mkdir buildSrc
➜ MyApplication git:(master) ✗ tree -L 1
.
├── MyApplication.iml
├── app
├── build.gradle
├── buildSrc # 插件目录
├── gradle
├── gradle.properties
├── gradlew
├── gradlew.bat
├── local.properties
└── settings.gradle
3 directories, 7 files
settings.gradle
的头部增加一句:include ':buildSrc'
。rootProject.name='My Application'
include ':app'
include ':buildSrc'
sync
,同步完工程后,buildSrc
目录下的内容如下所示:➜ MyApplication git:(master) ✗ tree buildSrc
buildSrc
├── build
│ ├── libs
│ │ └── buildSrc.jar
│ └── tmp
│ └── jar
│ └── MANIFEST.MF
└── buildSrc.iml
4 directories, 3 files
mkdir -p buildSrc/src/com/test/myapplication
src
后的路径根据自己的包名配置即可。➜ MyApplication git:(master) ✗ mkdir -p buildSrc/src/com/test/myapplication
➜ MyApplication git:(master) ✗ tree buildSrc
buildSrc
├── build
│ ├── libs
│ │ └── buildSrc.jar
│ └── tmp
│ └── jar
│ └── MANIFEST.MF
├── buildSrc.iml
└── src
└── com
└── test
└── myapplication
8 directories, 3 files
MyApplication/buildSrc/build.gradle
build.gradle
➜ MyApplication git:(master) ✗ touch buildSrc/build.gradle
➜ MyApplication git:(master) ✗ vim buildSrc/build.gradle
build.gradle
中增加如下内容:// 语言使用groovy,也可以采用其他的,自行修改即可。
apply plugin: 'groovy'
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
// 包含了AMS字节码注入工具
implementation "com.android.tools.build:gradle:3.4.2"
implementation gradleApi()
}
sourceSets {
main {
groovy {
srcDir 'src/main/groovy'
}
resources {
srcDir 'src/main/resources'
}
}
}
allprojects {
repositories {
google()
jcenter()
}
}
MyApplication/buildSrc/src/main/groovy/com/test/myapplication/MyApplicationPlugin.groovy
touch buildSrc/src/main/groovy/com/test/myapplication/MyApplicationPlugin.groovy
➜ MyApplication git:(master) ✗ touch buildSrc/src/com/test/myapplication/MyApplicationPlugin.groovy
➜ MyApplication git:(master) ✗ tree buildSrc
buildSrc
├── build
│ ├── libs
│ │ └── buildSrc.jar
│ └── tmp
│ └── jar
│ └── MANIFEST.MF
├── build.gradle
├── buildSrc.iml
└── src
└── com
└── test
└── myapplication
└── MyApplicationPlugin.groovy
8 directories, 5 files
MyApplication/buildSrc/src/main/groovy/com/test/myapplication/MyApplicationPlugin.groovy
package com.test.myapplication
import org.gradle.api.Plugin
import org.gradle.api.Project
class MyApplicationPlugin implements Plugin {
@Override
void apply(Project project) {
// 实现插件
}
}
touch buildSrc/src/main/resources/META-INF/gradle-plugins/com.test.myapplicationplugin.properties
implementation-class=com.test.myapplication.MyApplicationPlugin
MyApplication/app/build.gradle
配置Gradle:
apply plugin: 'com.android.application'
// 新增我们配置的插件
apply plugin: 'com.test.myplugin'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.test.myapplication"
minSdkVersion 14
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
如果可以在buildSrc/build/libs下看到buildSrc.jar
则证明成功创建插件了,可以愉快的进行开发了。
➜ MyApplication git:(master) ✗ tree buildSrc/build -L 3
buildSrc/build
├── classes
│ └── groovy
│ └── main
├── generated
│ └── sources
│ └── annotationProcessor
├── libs
│ └── buildSrc.jar # 插件生成的Jar包
├── resources
│ └── main
│ └── META-INF
└── tmp
├── compileGroovy
│ └── groovy-java-stubs
└── jar
└── MANIFEST.MF
14 directories, 2 files
AndroidStudio 中 buildSrc/src/main/groovy 的构建
Gradle新建插件buildSrc
Using BuildSrc for Custom Logic in Gradle Builds