Android如何创建Gradle插件开发工程及调试

背景

想写一个Gradle Transform Api,真的是怎么建一个Gradle插件,网上一水的水文,写的不明不白。代码都不全,还是得靠自己摸索。因此,在这里整理总结一下为大家提供出来。

如何创建Gradle插件工程

基于com.android.tools.build:gradle:3.4.0,下面分为几步,介绍如何配置Gradle插件开发工程。
按照以下几步即可。

新建Android项目

这个不多说了,Android Studio中新建一个Android项目。

新建Gradle插件Module

这个也不多说,新建一个Android Module。

删除不要的文件

打开module,除了build.gradle、.gitignore、*.iml都可以删掉

开发目录配置

在module目录下进行以下操作:

  1. 新建src/main/groovy目录,用于编写代码,与Android项目类似,只是java变成了groovy
  2. 新建src/main/resoureces/META-INF/gradle-plugins目录,用于存放插件入库和名字配置
  3. 在build.gradle中添加以下内容:
apply plugin: 'groovy'
apply plugin: 'maven'
dependencies {
    //gradle sdk
    compile gradleApi()
    //groovy sdk
    compile localGroovy()

    //添加依赖,于Android一样
    compile "com.android.tools.build:gradle:3.4.0"
    compile "com.android.tools.build:transform-api:1.5.0"
    compile "javassist:javassist:3.12.1.GA"
    compile "commons-io:commons-io:2.5"
}
repositories {
    mavenCentral()
}
//以上都为固定写法
//打包到本地或者远程Maven库
uploadArchives {
    repositories {
        mavenDeployer {
            pom.groupId = 'com.xxx.plugin.gradle'   //groupId
            pom.artifactId = 'xxx'  //artifactId
            pom.version = '1.0.2' //版本号
            //提交到远程服务器:
            // repository(url: "http://www.xxx.com/repos") {
            //    authentication(userName: "admin", password: "admin")
            // }
            //本地的Maven地址设置为
            repository(url: uri('/Users/lhxia/Downloads'))

        }
    }
}
  1. 在src/main/groovy目录下右键【New->Package】创建包名:com.xxx.plugin
  2. 在com.xxx.plugin包下创建插件入口文件:
package com.xxx.plugin
import org.gradle.api.Plugin
import org.gradle.api.Project
class MyPlugin implements Plugin<Project> {
    void apply(Project project) {//当插件apply时调用
        System.out.println("自定义插件!") //输出一句话
    }
}
  1. 在src/main/resoureces/META-INF/gradle-plugins目录下创建plugin_name.properties文件:,添加以下内容:
implementation-class=com.xxx.plugin.MyPlugin  #配置前面的插件入口类
  1. sync一下,开发环境就配置好了,我们可以看到Android Studio已经识别出目录了。
    在这里插入图片描述

插件发布

插件发布就简单了。前面在build.gradle中配置了上传maven的信息,直接执行upload即可。
在这里插入图片描述

插件接入

当插件发布完成后就是接入了。与其他插件一样,按照以下步奏配置:

  1. 在项目最外面的build.gradle中添加classpath:
    在这里插入图片描述
  2. 在需要插件的module的build.gradle里添加apply插件(plugin_name就是前面的properties文件名字):
    在这里插入图片描述
  3. 随便执行一个task就可以看到控制台的插件输出了
    在这里插入图片描述

如何使用kotlin编写插件呢?

其实kotlin写起来也挺简便,有些同学groovy不熟,因此用kotlin也能编写插件,这个配置也简单:

  1. src/main/groovy目录改成src/main/kotlin,然后在kotlin目录下边写kotlin代码。
  2. 修改插件module的build.gradle文件(我们可以看到对比前面的文件只是去掉了groovy,添加了kotlin的相关配置):
ext {
    kotlin_version = '1.3.40'
}
//apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: "kotlin"

sourceCompatibility = 1.8

dependencies {
    //gradle sdk
    compile gradleApi()
    //groovy sdk
//    compile localGroovy()
    //添加依赖,于Android一样
    compile "com.android.tools.build:gradle:3.4.0"
    compile "com.android.tools.build:transform-api:1.5.0"
    compile "javassist:javassist:3.12.1.GA"
    compile "commons-io:commons-io:2.5"
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.40"
}
compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
repositories {
    mavenCentral()
}
//以上都为固定写法
//打包到本地或者远程Maven库
uploadArchives {
    repositories {
        mavenDeployer {
            pom.groupId = 'com.xxx.plugin.gradle'
            pom.artifactId = 'xxx'
            pom.version = '1.0.4'
            //提交到远程服务器:
            // repository(url: "http://www.xxx.com/repos") {
            //    authentication(userName: "admin", password: "admin")
            // }
            //本地的Maven地址设置为E:/Maven
            repository(url: uri('/Users/lhxia/Downloads'))
        }
    }
}

如何调试插件

打断点

打断点不说了。和java一样,点一下就行

执行编译

在命令行中执行APP的编译命令:

 ./gradlew assembleDebug -Dorg.gradle.daemon=false -Dorg.gradle.debug=true

后面两个参数是必须的,表示等待gradle调试。这个时候会卡在这:
在这里插入图片描述
表示等待调试。

配置remote

  1. 创建新的运行配置在这里插入图片描述
  2. 创建remote类型的配置,本地调试默认配置即可直接点确定,如果远程调试请输入Host:
    在这里插入图片描述
  3. 对remote点击调试按钮:
    在这里插入图片描述
    就会看见命令行往下走了。随后就会收到断点:
    在这里插入图片描述

总结

其实过程很简单,一步一步即可完成插件的配置及调试,后面就可以开发属于自己的插件了。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值