Steps for developing a plug-in
- Create a New Android Library module and delete it
src
In the directoryandroidTest
.test
Directory. - in
src/main
Create a new directorygroovy
Table of contents, used to put relevantgroovy
file - in
src/main/groovy
Create a new directory under the directorycom.fred.asm
Note that this is a directory namedcom.fred.asm
Instead of building onecom/fred/asm
Directory) - in
src/main/groovy/com.fred.asm/
Directory, create a new class, this class is our plug-in entry, my initial idea was to write a plug-in to count the execution time of the method, so I wrote a classMethodStatPlugin.groovy
public class MethodStatPlugin implements Plugin<Project> {
@Override
void apply(Project target) {
println "plugin execute"; }}Copy the code
- in
src/main
Create a new one under the directoryresources
directory - in
src/main/resources/
Create a new directoryMETA-INF
In theMETA-INF
Let’s create another directorygradle-plugins
- Once the above steps are complete, there you have it
src/main/resources/META-INF/gradle-plugins
Directory, and then under this directory, we’ll create aperformance.methodstat.properties
file - in
performance.methodstat.properties
Add the following code to the file
implementation-class=com.fred.asm.MethodStatPlugin
Copy the code
- in
build.gradle
Add the following code to the file:
apply plugin: 'groovy' apply plugin: 'maven' dependencies { implementation fileTree(dir: 'libs', include: [' *. Jar ']) implementation gradleApi () implementation localGroovy () implementation 'com. Android. View the build: gradle: 3.4.2' Implementation 'org.ow2.asm:asm:7.1'} group='com.fred.asm. Plugin ' Version ='1.0.0' uploadArchives {repositories {mavenDeployer {// Repository (url: uri('.. /asm_plugin_repo')) } } }Copy the code
This completes our plug-in Module, which looks like this:
Need to pay attention to
- In the picture
META-INF.gradle-plugins
Is the display effect after Android Studio folds, and its corresponding physical path isMETA-INF/gradle-plugins
.performance.methodstat.properties
The document must be in thesrc/main/resources/META-INF/gradle-plugins
Directory.
At the same time, we double-click to execute uploadArchivestask on the right side of Android Studio as follows:
A local repository will be generated and the jar packages will be placed in this directory.
The use of plug-in
In the build.gradle file of our application, we need to introduce this plugin:
- Setting up a dependency library
Buildscript {repositories {Google () jCenter () // Custom plugin maven {url '.. /asm_plugin_repo'} dependencies {classpath 'com.fred.asm. Plugin :asm-tool:1.0.0'} }Copy the code
- Add the plug-in
apply plugin: 'performance.methodcost'
Remember when we plug-in development above have a SRC/main/resources/meta-inf/gradle – a performance under the plugins directory. Methodstat. Not the properties file, apply here the plugin: {pluginId}, {pluginId} is and the performance. The above methodstat. Corresponding to the properties file.
3. When we compile an application like assembleDebug, we can see that the console prints plugin execute
Let’s do it again in Java
We know that Groovy is also the language for JVM classes, and it can do all the things that Java can do, so let’s write it in Java once. Same thing:
- Create a New Android Library module and delete it
src
In the directoryandroidTest
.test
Directory. - in
src/main/java
Create a new packagecom.fred.asm
Note that this is the Java package structure, corresponding to the physical directory issrc/main/java/com/fred/asm
- in
src/main/java/com/fred/asm
Next, create a new Java classMethodStatPlugin.java
And ingroovy
I’ll write it the same way, so that oursrc/main/resources/META-INF/gradle-plugins
And the pluginId can remain the same. MethodStatPlugin.java
, write the following code:
public class MethodStatPlugin implements Plugin<Project> {
@Override
public void apply(Project target) {
System.out.println("plugin execute [write by java]"); }}Copy the code
- The same with
uploadArchives
Compile, package, and upload plug-ins to a local repository - Recompile the module that uses the plug-in, and you can see the console output
plugin execute [write by java]
The plug-in is successfully applied
usingbuildSrc
Simplify the development
So what is buildSrc? When we need to write some plug-ins, we can create a buildSrc Module in Android Studio. We can only have one module per Android project.
- When Gradle builds a module, it builds it first
buildSrc
This project is configured to the classpath - if
buildSrc
Gradle is a plugin project, Gradle automatically configures the Gradle plugin in the project directlyapply
A plug-in can be developed.
Development steps
- Create a New Java type Module with Android Studio and name it
buildSrc
(The name must be this) - Modify the
build.gradle
Add related dependencies as follows:
apply plugin: 'java-library'
repositories {
maven { url 'https://jitpack.io' }
maven {
url 'http://maven.aliyun.com/nexus/content/groups/public/'
}
google()
jcenter()
}
dependencies {
// Add gradle version
implementation 'com. Android. Tools. Build: gradle: 3.4.2'
// Add the gradle API
implementation gradleApi()
}
Copy the code
- Writing a plug-in
public class MyPlugin implements Plugin<Project> {
@Override
public void apply(Project target) {
System.out.println("[App Log]-------------->MyPlugin"); }}Copy the code
- Plug-in configuration
- The reference added in the place where the plug-in is needed
apply plugin: 'com.fred.plugin'
Copy the code
So far the development of Gradle plug-in has been introduced, the follow-up is to be familiar with the relevant API, according to their own project needs, do relevant business development.
reference
- Docs.gradle.org/current/use…
- Blog.csdn.net/Notzuonotdi…