RoboAspectJ is a Gradle plugin to introduce AspectJ (Aspect-Orient Programming) to Android project.
It compiles aspects(if needed) and weave them all together in FULL-PROJECT scope. This means project
(or subproject) sources, external libraries and local dependencies will all be dealt with by default.
Note: This plugin may change due to the modification of transform-api.
So you may keep track of RoboAspectJ to make sure you’re using the most recent version.
There are basically 2 ways to write your aspects and weave them into production code:
As Source
write aspects in @AspectJ syntax under your project’s java source directory. e.g. {$projectDir}/src/main/java/
As Library
compile and bundle your aspects independently using ajc, then make it dependency in build script. For example:
compile 'com.example.myaspects:library:1.0'
This way may be a little bit complicated. But it’s suitable for those who want to maintain their aspects as an independent project.
Variant-Specific Concern
While RoboAspectJ is registered globally, we still can do our crosscutting concern under specific variant.
Actually, this is already done by Android plugin.
As Source
put variant-specific aspects under corresponding folder.
For example, I want to do some performance monitoring in myflavor, so I will write aspects under ${projectDir}/src/myflavor/java/.
As Library
add variant-specific aspects dependency to corresponding configuration scope.
Though weaving is disabled, Aspects and AspectJ compile dependencies are all still there. It’s only
the weaving step doesn’t happen.
Note: Corresponding property has precedence over config in build script in both of these 2 cases.
For instance, weaving will not take effect when your roboaspectj.enable property is false,
no matter what you config in build script.
RoboAspectJ
RoboAspectJ is a Gradle plugin to introduce AspectJ (Aspect-Orient Programming) to Android project. It compiles aspects(if needed) and weave them all together in FULL-PROJECT scope. This means project (or subproject) sources, external libraries and local dependencies will all be dealt with by default.
latest version: v0.9.1
Prerequisite
Android Plugin (application) 2.1.0
Apply
Add plugin dependency in buildscript classpath:
Apply plugin:
Coding
There are basically 2 ways to write your aspects and weave them into production code:
As Source
write aspects in @AspectJ syntax under your project’s java source directory. e.g.
{$projectDir}/src/main/java/As Library
compile and bundle your aspects independently using ajc, then make it dependency in build script. For example:
Variant-Specific Concern
While RoboAspectJ is registered globally, we still can do our crosscutting concern under specific variant. Actually, this is already done by Android plugin.
As Source
put variant-specific aspects under corresponding folder.
For example, I want to do some performance monitoring in
myflavor, so I will write aspects under${projectDir}/src/myflavor/java/.As Library
add variant-specific aspects dependency to corresponding configuration scope.
Configuration
There is an extension
aspectjfor you to do some tweaking.Exclude
If you want to leave some artifact untouched from AspectJ, using:
Java runtime
When applying
rxjavaorretrolambda, you may needjrt.jaras classpath. Configure it by:Disable
For debug or performance use, you can disable weaving:
alternatively, set
roboaspectj.enablepropertyfalsewhen run gradle.Maybe you want it to be smarter to disable it when it’s a debug flavor, then add this to your build script:
or, specify
roboaspectj.disableWhenDebugproperty.Though weaving is disabled, Aspects and AspectJ compile dependencies are all still there. It’s only the weaving step doesn’t happen.
License
Code is under the Apache Licence v2.
Feedback
This plugin is currently a prototype, and it still has much to improve. Feel free to contact: xuxizhi@meituan.com