Gradle Custom Plugins
To create a custom plug-in in Gradle, Gradle provides three ways to:
- Use directly in the Build.gradle script
- Use in BUILDSRC
- Use in standalone module
Development Gradle plug-ins can be developed in idea or in Android studio, but the only difference is that idea provides a plugin for gradle development, which makes it easier to create files and directories, and in Android studio, Developers need to create them manually (but in fact, these directories are not many and complex and can be created manually).
Using in Projects
To create a standard Android project in Android Studio, the entire directory structure is as follows:
├──app│├──build. Gradle│├──libs│└──src│├──androidtest││└──java│├──main││├──androidmanifest. XML││├──java││└──res│└──test├──build. Gradle├──buildsrc│├──build. Gradle---1│└──src│└──main│├──groovy---2│└──resources---3├──gradle│└──wrapper│├──gradle-wrapper. Jar│└──gradle-wrapper. Properties├──gradle. Properties├──gradlew├──gradlew. Bat├──local. Properties└──settings. Gradle
Among them, in addition to the BUILDSRC directory, is the standard Android directory, and Buildsrc is gradle provided in the project to configure the custom plug-in default directory, development gradle to create the directory, that is rootproject/src/main/ Groovy and rootproject/src/main/resources two directories.
After the configuration is complete, if configured correctly, the corresponding folder will be recognized by the IDE as the corresponding category folder.
Create Buildsrc/build.gradle-1
First of all, to configure the Build.gradle file under the BUILDSRC directory, this configuration is fixed, the script is as follows:
‘groovy‘{ compile gradleApi() compile localGroovy()}
Create a groovy Script-2
Next, in the groovy directory, create a groovy class (similar to Java, you can bring the package name, but the groovy class ends with. Grovvy):
In the script, implement the Apply method by implementing the Gradle plugin interface, the script is as follows:
package com.xysimport org.gradle.api.Pluginimport org.gradle.api.Projectpublicclass MainPluginForBuildSrc implements Plugin<Project> { @Override void apply(Project project) { project.task(‘testPlugin‘) << { "Hello gradle plugin in src" } }}
In the Apply method of the script shown above, the author simply implements a task named Testplugin, which executes the task and outputs a row of logs.
Extension to create a groovy script
The extension of the so-called groovy script is, in fact, a configuration message similar to Gradle, which can be passed through extension in the Build.gradle script of the main project when the master project uses the custom Gradle plug-in.
To create a extension, you just need to create a groovy class:
As shown above, the author names a groovy class called Myextension, whose script is as follows:
package com.xys;class MyExtension { String message}
The Myextension code is simple enough to define the parameter variables to be configured, which I'll show you how to use later.
Using extension in Groovy scripts
After creating the extension, you need to modify the groovy class that you created earlier to load extension, and the modified script looks like this:
package com.xysimport org.gradle.api.Pluginimport org.gradle.api.Projectpublicclass MainPluginForBuildSrc implements Plugin<Project> { @Override void apply(Project project) { project.extensions.create(‘pluginsrc‘, MyExtension) project.task(‘testPlugin‘) << { println project.pluginsrc.message } }}
The Project.extensions.create method is used to configure a extension to Gradle.
Create Resources-3
The resources directory is the directory that identifies the entire plug-in, and the structure under its directory is as follows:
└──resources
└──meta-inf
└──gradle-plugins
The directory structure, like BUILDSRC, is the default directory for the Gradle plugin and cannot be modified. After creating these directories, create in the Gradle-plugins directory-the plug-in name. properties file,:
As shown above, here the author is named Pluginsrc.properties, where the code is as follows:
implementation-class=com.xys.MainPluginForBuildSrc
Use the code above to specify the groovy class that you created first.
Using plug-ins in the main project
In the main project's Build.gradle file, the custom plugin is loaded with the Apply directive, as shown in the following script:
‘pluginsrc‘
The name of plugin, which is the name--PLUGINSRC in the previous creation of Pluginsrc.properties, is loaded with custom plugins in this way.
Configure extension
In the main project's Build.gradle file, load the extension with code such as the following:
pluginsrc{ message = ‘hello gradle plugin‘}
Similarly, the domain name is the plug-in name, and the configured parameters are the parameter names defined in the extension.
After the configuration is complete, you can use the custom plugin in the main project, execute the gradle testplugin instruction at the terminal, and the result is as follows:
:app:testPluginhello gradle plugin
Use in local repo
Creating a custom Gradle plugin in BUILDSRC is only available in the current project, so for a generic plug-in, it is usually a separate module to create a custom Gradle plugin.
Create an android Library Module
First, in the main project project, create a normal Android Library Module, and delete its default created directory, modify the directory required for the Gradle plug-in, that is, all directories in the BUILDSRC directory:
As shown, the file created is identical to the one created in the BUILDSRC directory, except that the plugin is created in a custom module instead of the default BUILDSRC directory.
Deploy to local repo
Because the plugin is created by customizing the module, it is not possible to get gradle to do the plug-in loading and need to be deployed manually, so the configuration of Maven needs to be added to the plugin's build.gradle script as follows:
‘groovy‘‘maven‘dependencies { compile gradleApi() compile localGroovy()}repositories { mavenCentral()}group=‘com.xys.plugin‘version=‘2.0.0‘uploadArchives { repositories { mavenDeployer { repository(url: uri(‘../repo‘)) } }}
Compared to the Build.gradle script in Buildsrc, this adds MAVEN support and uploadarchives as a task that deploys the module to the local repo directory. Execute the gradle uploadarchives directive in the terminal and deploy the plug-in to the repo directory:
When the plug-in is deployed locally, the plugin can be referenced in the main project.
When the plugin is officially released, you can publish the plugin to the Central library like any other module, so that you can use the plug-in like a library project in a central library.
Referencing plugins
In Buildsrc, the system automatically helps developers customize plug-ins with reference support, but in custom module plug-ins, developers need to add their own reference support for custom plug-ins. In the main project's Build.gradle file, add a script such as the following:
‘com.xys.plugin‘{ repositories { maven { url uri(‘../repo‘) } } { classpath ‘com.xys.plugin:plugin:2.0.0‘ }}
Where the path specified by classpath is similar to the compile reference, that is, the plug-in name: Group:version
After the configuration is complete, you can use the custom plugin in the main project, execute the gradle testplugin instruction at the terminal, and the result is as follows:
:app:testPluginHello gradle plugin
If you do not use the local maven repo to deploy, you can also get the generated plug-in jar file, copied to the Libs directory, and referenced by the code as follows:
classpath fileTree(dir‘libs‘‘\*.jar‘// 使用jar
Reference: https://docs.gradle.org/current/userguide/custom_plugins.html
Gradle Custom Plugins