Custom gradle plugin implementation step record in Android

Implement the plugin directly in build.gradle

  • Create a groovy class that uses the plug-in function, in the build.gradle of the app layer

class GreetingPluginExtension {
   //插件类中的属性
   String message = "Hello"
}
  • Implement the org.gradle.api.Plugin interface and call the plug-in class in the reapply() method

class GreetingPlugin implements Plugin<Project> {

   @Override
   void apply(Project target) {
       //生成插件类的对象,greeting 是插件类的别名,编译完成后可以用来使用闭包的形式修改插件类中的属性
       def extension = target.extensions.create("greeting", GreetingPluginExtension)
       target.task("hello") {//生产名为hello的任务,可以在命令行中使用 gradle hello 运行该任务
           doLast {
               println(extension.message)
           }
       }
   }
}
  • Introduce custom plugins in build.gradle
 apply plugin: GreetingPlugin
  • Open the build.gradle file in Terminal and run "gradle hello", or run it in the Gradle list of AS after Sync is completed, as shown below
    Insert picture description here
    Insert picture description here
  • Use the alias of the plugin to modify the attributes in the plugin class in build.gradle, just like the following code produced by default in build.gradle, you can also modify the attributes in the custom plugin in the same way, as follows
 defaultConfig {
        applicationId "com.plugin.ggg.myapplication"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }

    apply plugin: GreetingPlugin //首先要先引入插件

    //修改插件类中的属性,再次运行hello ,便可以输出修改后的值
    greeting{
    message="1111"
}

If you run gradle hello and prompt that gradle is not an internal command, please add gradle/gradle-5.1.1 (this 5.1.1 is the version number of gradle)/bin in the AndroidStudio installation directory to the path of the environment variable, and restart the computer. can

Project independent plugin

  • Create a new Android Library Module in the project
  • Delete all the files in src/main, delete all the contents of build.gradle in the module
  • Add the following code to build.gradle in the module and sync
apply plugin: 'groovy'//引入 groovy支持的插件
apply plugin: 'maven' //引入maven支持的插件
dependencies {
    //引入 groovy支持的插件
    compile gradleApi()
    compile localGroovy()
}
//使用maven仓库
repositories {
    mavenCentral()
}
//定义插件的在本地maven中的id
 group = 'test.plugins'
//定义插件的在本地maven中的版本号
 version = '1.0.0'
//将插件打包上传到本地maven
uploadArchives {
    repositories {
        mavenDeployer {
            //指定本地maven的路径
            repository(url: uri('./repos'))
        }
    }
}

  • Create a new groovy folder in src/main, create a new .groovy file in it, create a new resources folder in src/main, create a new META-INF folder in it, create a new gradle-plugins folder in META-INF, this The names of the two folders are fixed, and then create a xxx.properties file in gradle-plugins, xxx can be customized, this will be used as the value of the plugin group in the local maven. The directory structure is as shown below
    Insert picture description here

  • The implementation of the plug-in class is as follows

package gradle

import org.gradle.api.Plugin
import org.gradle.api.Project
//实现Plugin接口,重写apply 方法
class PluginImpl implements Plugin<Project> {

    @Override
    void apply(Project target) {
        //在这里实现插件功能
        System.out.println("========================")
        System.out.println("hello gradle plugin!")
        System.out.println("========================")
    }
}
  • The contents of the xxx.properties file are as follows
# 指定插件类的入口 ,包名.类名
implementation-class=gradle.PluginImpl
  • After sync now, open the build.gradle file in the module on the command line and enter the "gradle uploadArchives" command to publish the plug-in to local maven, or publish it in Gradle in AS, as shown below
    Insert picture description here

  • After the release, the corresponding warehouse will be produced in the local maven. The local maven address specified by "./repos" is in the next-level folder in the module, so the repos warehouse produced by maven is as shown below
    Insert picture description here

  • The content in maven-metadata.xml is as follows

<?xml version="1.0" encoding="UTF-8"?>
<metadata>
  <!--插件在project 下面的build.gradle引入时的形式为[groupId]:[artifactId]:[version]-->
  <groupId>test.plugins</groupId>
  <artifactId>plugin</artifactId>
  <versioning>
    <release>1.0.0</release>
    <versions>
      <version>1.0.0</version>
    </versions>
    <lastUpdated>20190803052405</lastUpdated>
  </versioning>
</metadata>

  • Use the plug-in in the project and introduce the local maven address in the build.gradle of the project. The code is as follows
buildscript {
    ext.kotlin_version = '1.3.31'
    repositories {//声明了工程所需要的依赖库,供下面dependencies下载
        google()
        jcenter()
        maven {
            url uri("./plugin/repos") //本地maven中插件的地址
        }


    }
    dependencies {//声明了具体的依赖,使用[group]:[name]:[version]形式
        classpath 'com.android.tools.build:gradle:3.4.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        //引入插件的依赖
        classpath 'test.plugins:plugin:1.0.0'
    }
}
  • Introduce plugins in modules that need to use plugins
apply plugin: 'test.plugins'

After modifying the plugin code, it needs to be rebuilt and republished to the local maven

Guess you like

Origin blog.csdn.net/genmenu/article/details/98337935