• Stars
    star
    3,896
  • Rank 10,593 (Top 0.3 %)
  • Language
    Groovy
  • License
    Apache License 2.0
  • Created almost 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Android gradle plugin that effects AspectJ on Android project and can hook methods in Kotlin, aar and jar file.

AspectJX

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

最近更新

v2.0.10 (2020-03-31)

  • Supports android gradle plugin 3.6.1
  • Upgrade inner aspectjrt version to 1.9.5

查看更多版本信息

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
  • 在app项目的build.gradle里应用插件
apply plugin: 'android-aspectjx'
//或者这样也可以
apply plugin: 'com.hujiang.android-aspectjx'
  • 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是否支持*.aj文件的编译?

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

  • 问:编译时会出现can't determine superclass of missing type**及其他编译错误怎么办

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

感谢

  • 开发AspectJX的初衷
  1. 目前的开源库中还没有发现可应用于Android平台的比较好的AOP框架或者工具,虽然xposeddexposed非常强大,但基于严重的碎片化现状,兼容问题永远是一座无法逾越的大山。
  2. 目前其他的AspectJ相关插件和框架都不支持AAR或者JAR切入的,对于目前在Android圈很火爆的Kotlin更加无能为力。
  • 感谢
  1. 该项目的设计参考了大神JakeWhartonHugo项目及uPhycagradle-android-aspectj-plugin项目的设计思想,并在它们的基础上扩展支持AAR, JAR及Kotlin的应用。在此感谢JakeWharton和uPhyca.
  2. 感谢热心的AspectJX粉丝及其他使用者的积极反馈,你们提供的PR以及在Issues里提出的问题和答复给大家解决了很多问题,你们都为AspectJX贡献了力量

参考

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