classpath ‘org.aspectj:aspectjtools:1.9.1’
classpath ‘org.aspectj:aspectjweaver:1.9.1’
}
}
allprojects {
repositories {
jcenter()
maven { url ‘https://jitpack.io’ }
mavenCentral()
}
}
- 在mudle的build.gradle中配置,注意一定是mudle中,lib中报错
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main
这里的javaCompile.doLast配置和aspectj对应,我这里是1.9.1版本
final def log = project.logger
final def variants = project.android.applicationVariants
variants.all { variant ->
if (!variant.buildType.isDebuggable()) {
log.debug(“Skipping non-debuggable build type ‘${variant.buildType.name}’.”)
return
}
JavaCompile javaCompile = variant.javaCompile
javaCompile.doLast {
String[] args = [“-showWeaveInfo”,
“-1.9”,
“-inpath”, javaCompile.destinationDir.toString(),
“-aspectpath”, javaCompile.classpath.asPath,
“-d”, javaCompile.destinationDir.toString(),
“-classpath”, javaCompile.classpath.asPath,
“-bootclasspath”, project.android.bootClasspath.join(File.pathSeparator)]
log.debug "ajc args: " + Arrays.toString(args)
MessageHandler handler = new MessageHandler(true)
new Main().run(args, handler)
for (IMessage message : handler.getMessages(null, true)) {
switch (message.getKind()) {
case IMessage.ABORT:
case IMessage.ERROR:
case IMessage.FAIL:
log.error message.message, message.thrown
break
case IMessage.WARNING:
log.warn message.message, message.thrown
break
case IMessage.INFO:
log.info message.message, message.thrown
break
case IMessage.DEBUG:
log.debug message.message, message.thrown
break
}
}
}
}
我的整个mudle配置页面如下:
apply plugin: ‘com.android.application’
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main
android {
compileSdkVersion 26
buildToolsVersion “26.0.2”
defaultConfig {
applicationId “tsou.cn.zidinyidemo”
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName “1.0”
testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner”
}
sourceSets {
main {
jniLibs.srcDirs = [‘libs’]
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’
}
}
}
repositories {
flatDir {
dirs ‘libs’
}
}
dependencies {
compile fileTree(include: [‘*.jar’], dir: ‘libs’)
androidTestCompile(‘com.android.support.test.espresso:espresso-core:2.2.2’, {
exclude group: ‘com.android.support’, module: ‘support-annotations’
})
//noinspection GradleCompatible
compile(name: ‘hxgioc’, ext: ‘aar’)
// compile project(‘:lib_hxgioc’)
compile project(‘:lib_recycleview’)
compile ‘com.android.support:appcompat-v7:27.0.2’
compile ‘com.android.support.constraint:constraint-layout:1.0.2’
testCompile ‘junit:junit:4.12’
compile project(‘:lib_hxgaop’)
compile ‘com.squareup.okhttp3:okhttp:3.8.0’
}
final def log = project.logger
final def variants = project.android.applicationVariants
variants.all { variant ->
if (!variant.buildType.isDebuggable()) {
log.debug(“Skipping non-debuggable build type ‘${variant.buildType.name}’.”)
return
}
JavaCompile javaCompile = variant.javaCompile
javaCompile.doLast {
String[] args = [“-showWeaveInfo”,
“-1.9”,
“-inpath”, javaCompile.destinationDir.toString(),
“-aspectpath”, javaCompile.classpath.asPath,
“-d”, javaCompile.destinationDir.toString(),
“-classpath”, javaCompile.classpath.asPath,
“-bootclasspath”, project.android.bootClasspath.join(File.pathSeparator)]
log.debug "ajc args: " + Arrays.toString(args)
MessageHandler handler = new MessageHandler(true)
new Main().run(args, handler)
for (IMessage message : handler.getMessages(null, true)) {
switch (message.getKind()) {
case IMessage.ABORT:
case IMessage.ERROR:
case IMessage.FAIL:
log.error message.message, message.thrown
break
case IMessage.WARNING:
log.warn message.message, message.thrown
break
case IMessage.INFO:
log.info message.message, message.thrown
break
case IMessage.DEBUG:
log.debug message.message, message.thrown
break
}
}
}
}
- 引入aspectjrt
第一种方式
compile ‘org.aspectj:aspectjrt:1.9.1’
第二种方式:
引入aspectjrt.jar
注意aspectjrt和注解放在一起
- 检查网络注解
package tsou.cn.lib_hxgaop;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
- Created by Administrator on 2018/6/25 0025.
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface HxgAopCheckNet {
int value();
}
- 处理切点
package tsou.cn.zidinyidemo.aop;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.View;
import android.widget.Toast;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import tsou.cn.lib_hxgaop.HxgAopCheckNet;
/**
-
Created by Administrator on 2018/6/25 0025.
-
处理切点
*/
@Aspect
public class CheckNetSectionAspect {
/**
-
找到处理的切点
-
-
- *(…))处理所有的方法
-
execution:切点的位置(包名+方法名)
*/
@Pointcut(“execution(@tsou.cn.lib_hxgaop.HxgAopCheckNet * *(…))”)
public void checkNetBehavior() {
}