HujiangTechnology / gradle_plugin_android_aspectjx

A Android gradle plugin that effects AspectJ on Android project and can hook methods in Kotlin, aar and jar file.
Apache License 2.0
3.93k stars 568 forks source link

AspectJX

一个基于AspectJ并在此基础上扩展出来可应用于Android开发平台的AOP框架,可作用于java源码,class文件及jar包,同时支持kotlin的应用。

最近更新

v2.0.10 (2020-03-31)

查看更多版本信息

AspectJX 2.0.0版本与旧版本之间编译性能对比数据

下面的数据来自于同一个项目不同环境下的编译情况 由于旧版本不支持Instant Run增量编译,故没有这块数据

gradle version android plugin version full build(2.0.0/1.1.1 ms) instant run(2.0.0/1.1.1 ms) 性能提升
2.14.1 2.2.0 9761/13213 2596/- +35%
3.3 2.3.0 8133/15306 890/- +88%
4.1 3.0.1 6681/15306 713/- 129%
4.4 3.1.4

如何使用

AspectJX是基于 gradle android插件1.5及以上版本设计使用的,如果你还在用1.3或者更低版本,请把版本升上去。

本使用说明是基于重构后的2.0.0版本编写的,如需要查阅旧版本的README,请切换到对应的Tag。

在项目根目录的build.gradle里依赖AspectJX

 dependencies {
        classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.8'
        }

或者使用product目录下的jar包,在你的项目根目录下新建目录plugins,把product/gradle-android-plugin-aspectjx-2.0.0.jar拷贝到plugins,依赖jar包

dependencies {
        classpath fileTree(dir:'plugins', include:['*.jar'])
        }

注意:

  1. 区别于旧版本,离线新版本不再需要依赖org.aspectj:aspectjtools:1.8.+
  2. compile 'org.aspectj:aspectjrt:1.8.+' 必须添加到包含有AspectJ代码的module. 可以参考Demo
apply plugin: 'android-aspectjx'
//或者这样也可以
apply plugin: 'com.hujiang.android-aspectjx'

AspectJX默认会处理所有的二进制代码文件和库,为了提升编译效率及规避部分第三方库出现的编译兼容性问题,AspectJX提供include,exclude命令来过滤需要处理的文件及排除某些文件(包括class文件及jar文件)。

注意:2.0.0版本之后旧版本的includeJarFilterexcludeJarFilter命令废弃,不再支持使用

2.0.0版本的 include,exclude通过package路径匹配class文件及jar文件,不再支持通过jar物理文件路径匹配的方式,比如:

支持

aspectjx {
//排除所有package路径中包含`android.support`的class文件及库(jar文件)
    exclude 'android.support'
}

不支持

aspectjx {
    excludeJarFilter 'universal-image-loader'
}

//或者
aspectjx {
    exclude 'universal-image-loader'
}

*支持``匹配

aspectjx {
//忽略所有的class文件及jar文件,相当于AspectJX不生效
    exclude '*'
}

提供enabled 开关

enabled默认为true,即默认AspectJX生效

aspectjx {
//关闭AspectJX功能
    enabled false
}

常见问题

答: 不支持。目前AspectJX仅支持annotation的方式,具体可以参考支持kotlin代码织入的AspectJ Demo

答:大部分情况下把出现问题相关的class文件或者库(jar文件)过滤掉就可以搞定了

感谢

参考

License

Copyright 2018 firefly1126, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.gradle_plugin_android_aspectjx