最後編輯於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
待續。。。