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, messag
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
e.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;
@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;
@Aspect
public class CheckNetSectionAspect {
@Pointcut(“execution(@tsou.cn.lib_hxgaop.HxgAopCheckNet * *(…))”)
public void checkNetBehavior() {
}
@Around(“checkNetBehavior()”)
public Object checkNet(ProceedingJoinPoint joinPoint) {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
HxgAopCheckNet checkNet = methodSignature.getMethod().getAnnotation(HxgAopCheckNet.class);
if (checkNet != null) {
Object object = joinPoint.getThis();
Context context = getContext(object);
if (context != null) {
if (!isNetworkConnected(context)) {
int resId = checkNet.value();
Toast.makeText(context, resId, Toast.LENGTH_LONG).show();
return null;
}
}
}
try {
return joinPoint.proceed();//这里是真正的方法执行过程
//如果在方法执行完成后继续 *** 作,可以在这里对joinPoint.proceed(),进行接收,然后再次返回
//Object object= joinPoint.proceed();
//这里执行后续 *** 作***************************************
// return object;
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}
@SuppressLint(“NewApi”)
private Context getContext(Object object) {
if (object instanceof Activity) {
return (Activity) object;
} else if (object instanceof Fragment) {
Fragment fragment = (Fragment) object;
return fragment.getContext();
} else if (object instanceof View) {
View view = (View) object;
return view.getContext();
}
return null;
}
private static boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)