日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

AspectJ在Android中使用(AOP)

發布時間:2023/12/15 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AspectJ在Android中使用(AOP) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

AOP(Aspect Oriented Programming)切面編程在處理一些與業務邏輯無關,但在很多地方又不得不添加相關邏輯代碼,可以很好的解決相關問題,比如在Android中有些地方需要打LOG日志,或者在某些地方需要檢查系統權限等,可以很好的解決這類問題。

而AOP中主要使用的就是AspectJ,AspectJ有自己相關的一套語法,大致和JAVA類似。官方簡介

那么在AndroidStudio 中要怎么使用AOP呢?

首先需要新建一個module,方便引入相關的AspectJ配置,便于在其中編寫與業務邏輯無關緊要,并且在代碼中經常出現的常用代碼, 之后在module下的build.gradle中 導入AspectJ的dependences

compile ‘org.aspectj:aspectjrt:1.8.9’ 復制代碼

并在build.gradle根下配置

repositories {mavenCentral() }buildscript {repositories {mavenCentral()}dependencies {classpath 'com.android.tools.build:gradle:2.1.3'classpath 'org.aspectj:aspectjtools:1.8.9'classpath 'org.aspectj:aspectjweaver:1.8.9'} }android.libraryVariants.all { variant ->LibraryPlugin plugin = project.plugins.getPlugin(LibraryPlugin)JavaCompile javaCompile = variant.javaCompilejavaCompile.doLast {String[] args = ["-showWeaveInfo","-1.5","-inpath", javaCompile.destinationDir.toString(),"-aspectpath", javaCompile.classpath.asPath,"-d", javaCompile.destinationDir.toString(),"-classpath", javaCompile.classpath.asPath,"-bootclasspath", plugin.project.android.bootClasspath.join(File.pathSeparator)]MessageHandler handler = new MessageHandler(true);new Main().run(args, handler)def log = project.loggerfor (IMessage message : handler.getMessages(null, true)) {switch (message.getKind()) {case IMessage.ABORT:case IMessage.ERROR:case IMessage.FAIL:log.error message.message, message.thrownbreak;case IMessage.WARNING:case IMessage.INFO:log.info message.message, message.thrownbreak;case IMessage.DEBUG:log.debug message.message, message.thrownbreak;}}} } 復制代碼

以上module的build.gradle 配置主要是搭建AspectJ運行環境

之后在app下的build.gradle添加module的依賴,并且添加如下配置

repositories {mavenCentral() } buildscript {repositories {mavenCentral()}dependencies {classpath 'org.aspectj:aspectjtools:1.8.9'} }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.javaCompilejavaCompile.doLast {String[] args = ["-showWeaveInfo","-1.5","-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.thrownbreak;case IMessage.WARNING:log.warn message.message, message.thrownbreak;case IMessage.INFO:log.info message.message, message.thrownbreak;case IMessage.DEBUG:log.debug message.message, message.thrownbreak;}}} } 復制代碼

以上配置主要是將app和module關聯起來。

然后在module中創建TestAspectJ類, 具體代碼如下:

package com.clayx.org.aspectj;import android.annotation.TargetApi; import android.os.Build; import android.util.Log;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut;/*** Created by Administrator on 2016/9/2.*/ @Aspect public class TestAspectJ {private static final String METHOD_EXECUTION = "execution(* *..MainActivity+.onCreate(..))";private static final String METHOD_CALL = "call(* *..MainActivity+.test(..)) && args(name)";private String TAG = "Clayx";@Pointcut(METHOD_EXECUTION)public void methodExecution() {}@Pointcut(METHOD_CALL)public void methodCall(String name) {}@Around("methodExecution()")public void aroundMethodExecution(ProceedingJoinPoint joinPoint) throws Throwable {joinPoint.proceed();String result = "-----------------------------MethodExecution";Log.e(TAG, result);}@Around("methodCall(String)")public Object arouneMethodCall(ProceedingJoinPoint joinPoint){String name = (String) joinPoint.getArgs()[0];Log.e(TAG,name);return name;}} 復制代碼

運行APP,打印出Log如下:

對于注解的使用,可以更好的和AspectJ結合使用,

注解類如下:

package com.clayx.org.aspectj.anno;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;/*** Created by Administrator on 2016/9/2.*/ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface TestAspectJ {} 復制代碼

注解相關的AspectJ的實現類

package com.clayx.org.aspectj.anno;import android.content.Context; 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;/*** Created by Administrator on 2016/9/2.*/@Aspect public class TestAspect {@Pointcut("execution(@TestAspectJ private * *..*.*(..))")public void testAspect() {}@Around("testAspect()")public void testTestAspect(ProceedingJoinPoint joinPoint) {Toast.makeText((Context) joinPoint.getTarget(), "OK", Toast.LENGTH_SHORT).show();}} 復制代碼

注解運行如下:

轉載于:https://juejin.im/post/5a6181476fb9a01c9064f18c

總結

以上是生活随笔為你收集整理的AspectJ在Android中使用(AOP)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。