Build. Gradle Plugin Plugin Plugin Plugin Plugin Plugin Plugin

class PluginDemo implements Plugin<Project> { @Override void apply(Project target) { println 'Hello author! ' } } apply plugin: PluginDemoCopy the code
  • Extension
class ExtensionDemo {
    def name = 'Li'
}

class PluginDemo implements Plugin<Project> {
    @Overridevoid apply(Project target) {
            def extension = target.extensions.create('extension', ExtensionDemo)
            target.afterEvaluate {
                println "Hello ${extension.name}!"
            }
    }
}

apply plugin: PluginDemo

extension {
    name 'xuanlin'
}
Copy the code

Write it in the buildSrc directory:

  • The directory structure

  1. Resources/MEAT – INF/gradle – plugins/xx. The properties of the xx is the name of the plug-in, such as com. Test. The plugin. The properties in the end, application of plug-in code should be:
apply plugin: 'com.test.plugin'
Copy the code
  1. .properties has only one line in the format:
implementation-class=com.test.plugin.DemoPlugin
Copy the code
  • About the buildSrc directory:
  1. This is a special directory for Gradle. This directory is automatically executed, even if it is not configured into settings.gradle. We can even remove build.gradle from buildSrc
  2. BuildSrc is executed earlier than any project, earlier than settings.gradle in lower versions, and later than Settings. gradle in higher versions
  3. BuildSrc plugins are automatically added to the classpath of each project during compilation, so they can be easily applied using apply Plugin: ‘XXX’
  4. If ‘:buildSrc’ is configured in settings.gradle, the buildSrc directory is treated as a sub-project, so it is executed twice, and the Plugin and Project object cannot be used. Gradle should delete the ‘:buildSrc’ configuration

Single mode

  1. Create New Module -> Java or Kotlin Library
  2. Modify build.gradle for module
plugins { id 'java-library' id 'maven-publish' } dependencies { compileOnly gradleApi() } java { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } publishing { publications { GroupId 'com.test.plugin' artifactId 'plugin' version '1.0.0' from component.java}} repositories { maven { url uri('./Android/repos') } } }Copy the code
  1. Write Gradle Plugin, same as buildSrc
  2. Publish to the local Maven repository. The command is as follows:
./gradlew publish
Copy the code
  1. Reference the local Maven repository and add the classpath
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() mavenCentral() maven { url uri('./plugin/Android/repos') } } dependencies { classpath "Com. Android. Tools. Build: gradle: 7.0.1" classpath group: 'com. Test. The plugin, name: "plugins", version: "1.0.0" / / NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } }Copy the code
  1. The use of plug-in
plugins {
    id 'com.android.application'
    id 'com.test.plugin'
}
Copy the code

Note: After module compileOnly gradleApi() is synchronized, Build was configured to prefer Settings repositories over project but repository ‘Gradle Libs’ was added Set (repositoriesmode.fail_on_project_repos)

dependencyResolutionManagement { // repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google()  mavenCentral() jcenter() // Warning: this repository is going to shut down soon } }Copy the code