XTrace is a simple implementation of AOP on Android, treading through a series of pits. I hope to help you apply AOP to Android. Welcome to Star, Fork, and look forward to your valuable comments.
Here are my notes on trampling
AOP overview
Those of you who have done J2EE development must be very familiar with AOP. In J2EE, the implementation of section-oriented programming generally includes AspectJ, Spring AOP, etc. Sorry, I have only used these two. The main terms of AOP are: Aspect, Pointcut, Advise, etc. Here, I have chosen AspectJ as AOP for the Android platform.
AspectJ manipulates bytecodes at compile time, so it needs its separate compiler, AJC. In Android Studio, the AJC compiler can be enabled through Gradle scripts. To facilitate AOP development, I have created a small plug-in (very simple) that will be introduced into any library or Application where AOP is needed.
Dependencies buildscript {repositories {jcenter ()} {the classpath 'cn. Droidlover: AspectJAs: 1.0.2'}}
Copy the code
Use the plug-in in the corresponding module:
apply plugin: 'cn.droidlover.aspectjas'Copy the code
• Plugin_name. Properties = resources/ meta-INF /gradle-plugins = resources/ meta-inf /gradle-plugins = resources/ meta-inf /gradle-plugins = resources/ meta-inf /gradle-plugins = resources/ meta-inf /gradle-plugins = resources/ meta-inf /gradle-plugins = resources/ meta-inf /gradle-plugins = resources/ meta-inf /gradle-plugins As the plug-in name for cn. Droidlover. Aspectjas, then the file name should be: cn. Droidlover. Aspectjas. Properties * file contents as follows:
implementation-class=Plug-in entry PATHCopy the code
If the above plug-in is:
Copy the code
implementation-class=cn.droidlover.aspectjas.AspectJAsCopy the code
A Plugin entry is a class that implements Plugin<Project>, and there is only one entry per plug-in.
Copy the code
- You can upload your own plugins to your local Maven repository
apply plugin: 'groovy'
apply plugin: "maven"
dependencies {
compile gradleApi()
compile localGroovy()
compile 'Com. Android. Tools. Build: gradle: 2.1.0'
}
repositories {
jcenter()
}
//Upload to the local code base
uploadArchives{
repositories{
mavenDeployer{
repository(url:uri('../repo'))
pom.groupId = 'cn.droidlover' //Group name
pom.artifactId = 'AspectJAs' //The plugin name
pom.version = '1.0.2' //The version number}}}Copy the code
To use maven, add the maven address:
maven{
url uri('/repo')}Copy the code
The -xtrace instructions
XTrace targets methods with @Trace annotation. The main function of XTrace is to Log the execution method identifier, execution method name, execution parameter transmission value, execution time, and return value.
Run a screenshot
Usage:
@Trace("chat")
public void onChat(View view) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace(); }}@Trace("sum")
private int sum(int a, int b) {
return a + b;
}Copy the code
You can use this to extend, for example, statistics on user behavior.