最后编辑于2019年5月4日
以之前插件化的例子PluginDemo做个开场。
Step1:在需要制作成库的module的build.gradle中添加
apply plugin: 'maven'
...
uploadArchives {
repositories.mavenDeployer {
//本地磁盘仓库路径,以放到项目根目录下的 repo 的文件夹为例
repository(url: uri('../repo'))
//groupId ,自行定义
pom.groupId = 'com.example.xydzjnq'
//artifactId
pom.artifactId = 'mypluginlibrary'
//插件版本号
pom.version = '1.0.0'
}
}
Step2:打开右侧Gradle窗口,执行对应task;执行完成后会在对应目录下生成对应库文件
Step3:在需要的module中指定maven仓库位置,并引用其中的库包,如:
再举一个gradle plugin的例子GradlePlugin
该例子使用了AspectJ提供的独特的ajc编译器对使用该插件的模块进行编译。ajc会在编译期将Aspect代码插入到PointCut中,从而达到AOP的目的。
Step1:新建plugin模块,修改模块的build.gradle(引入对应dependencies)为
apply plugin: 'groovy'
dependencies {
implementation gradleApi()
implementation localGroovy()
implementation "org.aspectj:aspectjtools:1.9.0"
implementation "org.aspectj:aspectjrt:1.9.0"
}
repositories {
jcenter()
}
Step2:新建plugin模块,创建groovy目录,并新建一个实现Plugin<Project>接口的类(该类apply方法中会指定执行一些task,我会指定其在项目编译时执行ajc编译织入增强(Advice)代码)。
对应AspectJPlugin代码如下:
public class AspectJPlugin implements Plugin<Project> {
void apply(Project project) {
final def log = project.logger
project.dependencies {
implementation 'org.aspectj:aspectjrt:1.8.10'
}
if (project.android.hasProperty('applicationVariants')) {
project.android.applicationVariants.all { variant ->
JavaCompile javaCompile = variant.javaCompile
javaCompile.doLast {
String[] args = [
"-showWeaveInfo",
"-1.7",
"-inpath", javaCompile.destinationDir.toString(),
"-aspectpath", javaCompile.classpath.asPath,
"-d", javaCompile.destinationDir.toString(),
"-classpath", javaCompile.classpath.asPath,
"-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)
]
MessageHandler handler = new MessageHandler(true);
new Main().run(args, handler);
for (IMessage message : handler.getMessages(null, true)) {
switch (message.getKind()) {
case IMessage.ABORT:
case IMessage.ERROR:
case IMessage.FAIL:
log.error message.message, message.thrown
break;
case IMessage.WARNING:
log.warn message.message, message.thrown
break;
case IMessage.INFO:
log.info message.message, message.thrown
break;
case IMessage.DEBUG:
log.debug message.message, message.thrown
break;
}
}
}
}
}
if (project.android.hasProperty('libraryVariants')) {
project.android.libraryVariants.all { variant ->
JavaCompile javaCompile = variant.javaCompile
javaCompile.doLast {
String[] args = [
"-showWeaveInfo",
"-1.7",
"-inpath", javaCompile.destinationDir.toString(),
"-aspectpath", javaCompile.classpath.asPath,
"-d", javaCompile.destinationDir.toString(),
"-classpath", javaCompile.classpath.asPath,
"-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)
]
MessageHandler handler = new MessageHandler(true);
new Main().run(args, handler);
for (IMessage message : handler.getMessages(null, true)) {
switch (message.getKind()) {
case IMessage.ABORT:
case IMessage.ERROR:
case IMessage.FAIL:
log.error message.message, message.thrown
break;
case IMessage.WARNING:
log.warn message.message, message.thrown
break;
case IMessage.INFO:
log.info message.message, message.thrown
break;
case IMessage.DEBUG:
log.debug message.message, message.thrown
break;
}
}
}
}
}
}
}
Step3:新建properties文件
在resources/META-INF/grdle-plugins目录下创建一个aspectjplugin.properties文件,其中aspectjplugin就是以后引用的插件名。
Step4:修改模块的build.gradle,将该插件上传到本地库
apply plugin: 'groovy'
apply plugin: 'maven'
dependencies {
implementation gradleApi()
implementation localGroovy()
implementation "org.aspectj:aspectjtools:1.9.0"
implementation "org.aspectj:aspectjrt:1.9.0"
}
repositories {
jcenter()
}
uploadArchives {
repositories.mavenDeployer {
//本地仓库路径,以放到项目根目录下的 repo 的文件夹为例
repository(url: uri('../repo'))
//groupId ,自行定义
pom.groupId = 'com.example.xydzjnq'
//artifactId
pom.artifactId = 'autotrack'
//插件版本号
pom.version = '1.0.0'
}
}
Step5:打开右侧Gradle窗口,执行对应task;执行完成后会在对应目录下生成对应库文件
Step6:在需要使用该插件的project 的build.gradle中引入该插件
Step7:在需要该插件编译的模块apply plugin:
下面是一些开源的aspect插件:
Hugo
gradle_plugin_android_aspectjx
待续。。。