A gradle plugin that supports using AspectJ in android project. can weave third party libs and kotlin code
There is no perfect AOP tools or framework on android, even though xposed and dexposed are fantastic libs, but not compatible for all Android system versions.And some of the AspectJ plugins can not work well on libs(aar, jar) and kotlin code.
aspectjx takes some great ideas from JakeWharton's project Hugo and uPhyca's gradle-android-aspectj-plugin, and extends the ability to support aar, jar, kotlin. Thanks to JakeWharton and uPhyca.
Before using aspectjx, Android gradle plugin version must be greater than or equal
1.5.0
, and aspectjx must be used inapplication module
, notlibrary module
.
dependencies {
classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:1.0.10'
}
OR
using JAR from product. Make new directory named as plugins, and put product/gradle-android-plugin-aspectjx-1.0.10.jar in plugins, then do as below:
dependencies {
classpath fileTree(dir:'plugins', include:['*.jar'])
//don't lost dependency
classpath 'org.aspectj:aspectjtools:1.8.+'
}
ATTENTION
: compile 'org.aspectj:aspectjrt:1.8.+'
must be added to the modules that contain the AspectJ code, read the Demo
apply plugin: 'android-aspectjx'
aspectjx will scan and weave all .class file and jar, aar by the default,except for add some filter config includeJarFilter, excludeJarFilter. includeJarFilter, excludeJarFilter can support groupId filter, artifactId filter and dependency path matching.
aspectjx {
//includes the libs that you want to weave
includeJarFilter 'universal-image-loader', 'AspectJX-Demo/library'
//excludes the libs that you don't want to weave
excludeJarFilter 'universal-image-loader'
}
- excludes the lib whose groupId is
org.apache.httpcomponents
aspectjx {
excludeJarFilter 'org.apache.httpcomponents'
}
- exlucdes the lib whose artifactId is
gson
aspectjx {
excludeJarFilter 'gson'
}
- excludes the jar
alisdk-tlog-1.jar
aspectjx {
excludeJarFilter 'alisdk-tlog-1'
}
- excludes all dependency libs
aspectjx {
excludeJarFilter '.jar'
}
- IntelliJ now has no tools for AspectJ, Just Annotation Style AspectJ can work on Android studio. *.aj file can not be compiled . How to use Annotation Style AspectJ
- AspectJ may not work well on Android studio with Instant Run feature, if so, close the Instant Run feature.
- AspectJ may compile error as below, just exlcudes the associated lib to resolve it.
- The Classes and methods that you want to hooked in AOP should not be obfuscated:
package com.hujiang.test;
public class A {
public boolean funcA(String args) {
....
}
}
//if you want to hook A#funcA(String) in AOP code, keep this class and method in proguard config file
-keep class com.hujiang.test.A {*;}
email:xiaoming1109@gmail.com
Copyright 2016 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