Android 创建插件buildSrc流程

这个世界里,虽然没有最美好的相遇,却应该有为了相遇或者重逢,所做的最美好的努力。

前言

当需要编写类似代码注入的插件的时候,就需要创建一个名称为buildSrc的Android插件工程。值得注意的是,这个工程在一个Android工程中,仅仅有一个。并且,这个工程的名称只能叫做buildSrc

  • Gradle会在配置初始化之前,编译BuildSrc这个项目(可以是Java、Groovy、Kotlin),并配置到classpath下。

  • 如果buildSrc是一个Groovy项目,Gradle默认会自动配置该Gradle插件,在项目中直接apply开发的插件即可。

buildSrc创建流程

  • Android工程允许创建插件,插件的名称固定叫做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

  • 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

创建buildSrc/build.gradle

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

MyApplicationPlugin.groovy

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
  • 插件工程可以Groovy和Java混写~

附录

  • AndroidStudio 中 buildSrc/src/main/groovy 的构建

  • Gradle新建插件buildSrc

  • Using BuildSrc for Custom Logic in Gradle Builds

官方

  • Organizing Gradle Projects

你可能感兴趣的:(Android进阶,android,buildSrc,android,studio,groovy)