[上一篇文章]中讲解了如何在Android使用AOP,会发现在Gradle配置aop会比较麻烦,每个module使用了aop都需要配置。接下来看如何简化配置。

1、创建Module

首先,需要建立一个Android Library,命名为aop-plugin,如图:



2、删除文件

由于plugin是由groovy进行创建的,需要删除红色框内的文件



3、更改gradle

把module里面的build.gradle内容清空,修改内容:

apply plugin: 'groovy'
apply plugin: 'maven' dependencies {
compile gradleApi()
compile localGroovy()
compile 'com.android.tools.build:gradle:3.1.3'
compile 'org.aspectj:aspectjtools:1.9.1'
compile 'org.aspectj:aspectjrt:1.9.1'
} repositories {
mavenCentral()
jcenter()
} uploadArchives { // 这里只是更新到本地,可以上传到自定义的maven仓库
repositories {
mavenDeployer {
pom.groupId = 'com.fomin.aop.plugin'
pom.artifactId = 'aop-plugin'
pom.version = 1.0
repository(url: uri('../repo'))
}
}
}

4、创建文件

├── build.gradle
└── src
└── main
├── groovy
└── com
└── fomin
└── aop
└── plugin
└── AopPlugin.groovy
└── resources
└── META-INF
└── gradle-plugins
└── aop-plugin.properties

建立/src/main/groovy/com/fomin/aop/plugin/AopPlugin.groovy,其中/src/main/groovy是固定的,/com/fomin/aop/plugin是创建library的包名,AopPlugin.groovy是以groovy的具体类名。

AopPlugin内容:

package com.fomin.aop.plugin

import com.android.build.gradle.AppPlugin
import com.android.build.gradle.LibraryPlugin
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.compile.JavaCompile class AopPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
println("==================")
println(" Gradle插件 ")
println("==================") if (!project.android) {
throw new IllegalStateException('Must apply \'com.android.application\' or \'com.android.library\' first!')
} def isApp = project.plugins.withType(AppPlugin)
def isLib = project.plugins.withType(LibraryPlugin)
println("isApp : " + isApp)
println("isLib : " + isLib)
if (!isApp && !isLib) {
throw new IllegalStateException("'android' or 'android-library' plugin required.")
} final def log = project.logger
final def variants
if (isApp) {
variants = project.android.applicationVariants
} else {
variants = project.android.libraryVariants
} project.dependencies {
implementation 'org.aspectj:aspectjrt:1.9.1'
} variants.all { variant ->
JavaCompile javaCompile = variant.javaCompile
javaCompile.doLast {
String[] args = ["-showWeaveInfo",
"-1.8",
"-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:
case IMessage.INFO:
log.info message.message, message.thrown
break;
case IMessage.DEBUG:
log.debug message.message, message.thrown
break;
}
}
}
}
}
}

建立/src/main/resources/META-INF/gradle-plugins/aop-plugin.properties,其中aop-plugin是自定义的插件名称,引用插件的时候用到,其它文件名是固定的。aop-plugin.properties文件内容为:

implementation-class=com.fomin.aop.plugin.AopPlugin

5、生成plugin

在Gradle栏目中会自动生成一个upload/uploadArchives的脚本,点击这个脚本会在生成 plugin。生成路径在build.gradle已经定义。





6、引入plugin

在相关module或者app的build.gradle引用plugin

apply plugin: 'aop-plugin'    ///插件名

大功告成,可以直接使用aop plugin,无需再像上一篇文章配置那么繁琐了。

Android自定义Aop的Gradle Plugin的更多相关文章

  1. AS 自定义 Gradle plugin 插件 案例 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  2. Gradle之Android Gradle Plugin 主要流程分析(二)

    [Android 修炼手册]Gradle 篇 -- Android Gradle Plugin 主要流程分析 预备知识 理解 gradle 的基本开发 了解 gradle task 和 plugin ...

  3. Android Studio, gradle plugin is too old or set ANDROID_DAILY_OVERRIDE

    早上打开Android Studio,忽然报了个错,说gradle plugin版本过低,让更新或者设置ANDROID_DAILY_OVERRIDE环境变量: 日志如下: INFO - ls.idea ...

  4. The Android Gradle Plugin and Gradle version-compatibility

    http://tools.android.com/tech-docs/new-build-system/version-compatibility Version Compatibility Post ...

  5. Android Gradle Plugin指南(六)——高级构建定制

    原文地址:http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Advanced-Build-Customization ...

  6. Android AS升级3.1 编译报错:The SourceSet 'instrumentTest' is not recognized by the Android Gradle Plugin.

    AndroidStudio升级到3.1后编译报错:The SourceSet ‘instrumentTest’ is not recognized by the Android Gradle Plug ...

  7. The android gradle plugin version 2.3.0-beta2 is too old, please update to the latest version.

    编译项目的时候,报如下错误: Error:(, ) A problem occurred evaluating project ':app'. > Failed to apply plugin ...

  8. Configuration on demand is not supported by the current version of the Android Gradle plugin since you are using Gradle version 4.6 or above. Suggestion: disable configuration on demand by setting org

    androidStudio打开cocos3.17.2Lua项目时,出现了 Configuration on demand is not supported by the current version ...

  9. gradle/gradle plugin/Android studio关系

    gradle - 构建工具,存储于Users/stono/.gradle/wrapper/dists Adroid Studio- IDE Gradle plugin - 在AS中使用Gradle的插 ...

随机推荐

  1. Android RecycleView 的优化

    减少条目的 View 的层级.层级越少效率越高,尤其避免使用 weight.用 ConstraintLayout 可以最大程度减少层级. 使用 ViewStub.如果某个 view 可能不需要被加载, ...

  2. 08_python_文件操作

    一.初始文件操作 打开⽂件的⽅式: r, w, a, r+, w+, a+, rb, wb, ab, r+b, w+b, a+b 默认使⽤的是r(只读)模式 f = open("少妇嫩模.t ...

  3. 在cad2008引用了错误的com接口的dll导致出现了

    请求“System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, Publi ...

  4. SQL简介及MySQL的安装目录详解

    一,SQL简介 1,数据库定义语言(DDL) ①create:用于创建数据库.表.索引.视图等: ②alter:用于修改数据库.表.索引.视图等: ③drop:用于删除数据库.表.索引.视图.用户等. ...

  5. postgresql-脏页和缓存失效

    脏页和缓存失效 https://www.cnblogs.com/flying-tiger/p/7885478.html Dirty pages and cache invalidation 我们一直在 ...

  6. 初识SQL语句

    SQL(Structured Query Language ) 即结构化查询语言 SQL语言主要用于存取数据.查询数据.更新数据和管理关系数据库系统,SQL语言由IBM开发.SQL语言分为3种类型: ...

  7. typescript-koa-postgresql 实现一个简单的rest风格服务器 —— typescript 开发环境配置

    最近需要用 nodeJS 写一个后台程序,为了能够获得 IDE 的更多代码提示,决定用 typescript 来编写,随便也学习下 ts,在这记录下实现过程. 1.新建文件夹 typescript-k ...

  8. 不能将 CHECK_POLICY 和 CHECK_EXPIRATION 选项设为 OFF (关)

    数据库用户信息死活无法修改..老是出现错误当 MUST_CHANGE 为 ON (开)时,不能将 CHECK_POLICY 和 CHECK_EXPIRATION 选项设为 OFF (关). (Micr ...

  9. centos7 完整配置openvpn详情教程

    1. 什么是OpenVpn OpenVPN 是一个用于创建虚拟专用网络加密通道的软件包,最早是由James Yonan编写的.OpenVPN允许创建的VPN使用公开密钥.电子证书.或者用户名/密码来进 ...

  10. [每天解决一问题系列 - 0002] Xcopy cannot copy file with long directory

    现象: 当xcopy的文件的全名(包括目录和文件名)的长度超过255字符时,会copy失败,得到insufficient memory错误 解决方法: 在Server 版的OS中,有robcopy命令 ...