Gradle plug-ins can be developed in two ways as far as we know

1. Write directly in Gradle file in Groovy and reference directly

2. Create a buildSrc directory to build and reference separately

The implementation principle is the same, but the second method is easier to manage and distribute, so this article uses the second method as the practice code attached to the first method at the end of the article

Tips: Don’t record groovy based on buildSrc, it’s more or less the same. Focus on documenting the use of Kotlin to define plug-ins

1. Create a Java or Kotlin Module named buildSrc

We can only call it that. BuildSrc is the directory that Gradle reserves for plug-in development and that’s just the way it works in Android Studio, For other methods, refer to the official documentation. The prompt ‘buildSrc’ cannot be used as a project name as it is a reserved name is displayed Just remove buildSrc from settings.gradle and recompile it. After all, this is under Android development tools, so it’s a little more acclimatizing

2. Configure compilation dependencies

If you don’t use Kotlin, you can ignore it. If you use Groovy, you can remove everything except the Java version number. By the way, both SRC /main/kotlin and SRC /main/ Java work, but there’s only one valid, preferred Java directory

// Use the plugins block syntax to apply plugins
plugins {
    // Apply the Kotlin plugin
    id 'org.jetbrains.kotlin.jvm' version '1.3.72'
}
repositories {
    google()
    jcenter()
}
dependencies {
    // Use grdale-API dependencies only at compile time
// compileOnly gradleApi() currently does not know what gradleApi does so keep comments
    // Add kotlin library dependencies to the plug-in source code
    implementation 'org.jetbrains.kotlin:kotlin-stdlib'
}
java {
    sourceCompatibility = JavaVersion.VERSION_1_7
    targetCompatibility = JavaVersion.VERSION_1_7
}
Copy the code

3. Create a plug-in

Under android Studio IDE, creating buildSrc automatically generates a plug-in class, but it does not inherit the plug-in interface

It needs a little tweaking

class GrockPlugin :Plugin<Project> {
    override fun apply(project: Project) {
       println("GrockPlugin apply")}}Copy the code

Don’t panic if you can’t find the Plugin interface. To add a dependency, open build.gradle in buildSrc and add a dependency. Gradle_version is the gradle plug-in version in the project root directory

dependencies {
  implementation "com.android.tools.build:gradle:$gradle_version"
}
Copy the code

4. Provide references

According to the document requirements of the entire declaration file

Create a [plug-in name when external references].properties file in the following directory

|____main
| |____resources
| | |____META-INF
| | | |____gradle-plugins
| | | | |____com.grock.properties
Copy the code

Open the [plug-in name when external references].properties file with the plug-in name com.grock above and specify the plug-in class in it

implementation-class=com.grock.plugin.GrockPluginJava
Copy the code

5. Reference the plug-in

Create a new Module reference

apply plugin:'com.grock'
Copy the code

Sync Viewing logs

GrockPlugin apply
Copy the code

6. Customize an extension function

Why not start with an extension function that can be configured based on current thinking, so try to learn something like a common Android {} function first

6.1 Define an extension function class that cannot be final

open class GrockExtension {
    var name= "GrockExtension original-name"
}
Copy the code

6.2 Create an extension function grockName

    override fun apply(project: Project) {
        // Create extension function, extension function name, extension function class
        val extensionFun = project
            .extensions.create("grockName",GrockExtension::class.java)
        project.afterEvaluate {
            println("GrockPlugin-kotlin afterEvaluate:${extensionFun.name}")}}Copy the code

Creating code is easy to understand, but what’s the point of the last two lines? Why not just print it? The reason for this is simple: after creating the extension function, the insert is just creating the extension function. By the time afterEvaluate callback, the plug-in is ready, the extension function has been read, and the value in the extension function is obtained

6.3 Using extension functions

apply plugin:'com.grock'

grockName{
    name "newName"
}
Copy the code

Print content:

GrockPlugin-kotlin afterEvaluate:newName
Copy the code

Use Groovy custom plugins directly in Gradle

Plug-in code:

class GrockPlugin implements Plugin<Project>{
    @Override
    void apply(Project target) {
        println "GrokcPlugin apply"
// Create the extension function, declare the name of the extension function and pass it to the extension class
        def extension = target.extensions.create("nameFunction",GrockExtension)
        target.afterEvaluate {// Execute the extension function after the plugin is added, so that the extension function cannot be accessed before it is created
            println " apply extension.name=${extension.name}"}}}class GrockExtension{
    def name = "GrockExtension"
}
// Execute from top to bottom
apply plugin:GrockPluginJava
nameFunction {
    name("extension-name") //setName("s")
}
Copy the code

Plug-in references: Direct references to plug-in classes

Groovy syntax allows you to omit class

apply plugin:GrockPlugin
Copy the code

Reference:

  • HencoderPlus course
  • gradle doc
  • Custom Gradle plug-in development