动态编译技术:编译期间动态的在class文件中注入代码或者或修改,大多插件化热修复都会采用此技术。
自定义Gradle插件
①
常规操作创建Project,新建一个module,此处新建了一个lib1新模块用于编写gradle插件.
②
如图将lib1中的所有文件删除,保留main,build,lib1.iml
③
- 创建groovy文件
- main--->groovy--->包名(com.xxx.xxx)--->XXX.groovy(注意添加.groovy后缀)
④
插件模块build添加groovy和maven
apply plugin: 'groovy'
apply plugin: 'maven'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//gradle sdk
compile gradleApi()
//groovy sdk
compile localGroovy()
}
repositories {
mavenCentral()
}
⑤ 创建resouces定义插件使用名
在mian--->resources--->META-INF--->gradle-plugins--->com.allure.plugin.
properties
此处注意com.allure.plugin是定义的插件名,后缀properties,其他地方使用:
apply plugin: 'com.allure.plugin'
在此文件指定加载的具体插件类
implementation-class=com.allure.plugin.MyPlugin
⑥ 书写插件类执行的任务
public class MyPlugin implements Plugin<Project> {
void apply(Project project) {
println('========================');
println('test plugin!');
println('========================');
}
}
到此插件制作完毕
(二)打包到本地或者maven
在插件类的build
group='com.allure.plugin'
version='1.0.0'
uploadArchives {
repositories {
mavenDeployer {
//提交到远程服务器:
// repository(url: "http://www.xxx.com/repos") {
// authentication(userName: "admin", password: "admin")
// }
//本地的Maven地址设置为/Users/mac/Downloads/ttt
repository(url: uri('/Users/mac/Downloads/ttt'))
}
}
}
- 执行上传task
执行后生成文件
(三)使用自定义的Gradle插件
在任意module(如app)里的build引入自定义的插件
apply plugin :'com.allure.plugin'
buildscript {
repositories {
maven {//本地Maven仓库地址
url uri('/Users/mac/Downloads/ttt')
}
}
dependencies {
//格式为-->group:module:version
classpath 'com.allure.plugin:lib1:1.0.0'
}
}
执行效果如图
Configuration on demand is an incubating feature.
========================
test plugin!
========================
Configuration 'compile' in project ':app' is deprecated. Use 'implementation' instead.
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:checkDebugManifest UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:prepareLintJar UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:createDebugCompatibleScreenManifests UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:splitsDiscoveryTaskDebug UP-TO-DATE
....