1, create a Java library project named BUILDSRC, delete the contents of Build.gradle, add the following content
apply plugin: ‘groovy‘dependencies { compile gradleApi()//gradle sdk compile localGroovy()//groovy sdk}
2. Delete all directory files under Src/main
- Create the Groovy directory under the main directory, create the package name Com.home.plugin, and create the Myplugin.groovy content as follows
package com.home.pluginimport org.gradle.api.Pluginimport org.gradle.api.Projectpublic class MyPlugin implements Plugin<Project> { void apply(Project project) { System.out.println("========================"); System.out.println("这是个插件!"); System.out.println("========================"); }}
Create the Resources directory under the main directory and then new Meta-inf directory in the Resources directory
Then create a new Gradle-plugins directory inside the Meta-inf. Finally create a new properties file in the Gradle-plugins directory
Notice the name of this file, you can name it at will, but use it later
For example, if you are named Com.home.plugin.properties, and you use a custom plugin in other build.gradle files, you need to write: Apply plugin: ' Com.home.plugin '
Com.home.plugin.properties file content is: Implementation-class=com.home.plugin.myplugin
- The project directory structure is as follows:
3, in the app Build.gradle Add reference to apply plugin: ' Com.home.plugin '
Demo Source: Https://github.com/xuguohongai/android/tree/master/GradlePlugin
Reference connection: http://kvh.io/cn/tags/EmbraceAndroidStudio/
Android Custom Gradle plugin for current project use