Android面向切面編程框架OkAspectj

### 有人想要Android面向切面編程,今天他來了!??,輕松完成各種騷操作!登錄狀態(tài)攔截,日志攔截,權(quán)限攔截,輕松搞定!
https://github.com/TanZhiL/OkAspectj

更新日志:

v1.02 2019-10.17
  • 第一次發(fā)布

快速對指定函數(shù)進(jìn)行切面攔截:

  • 注解完全自定義
  • 攔截規(guī)則自定義
  • 無需手動編寫切面代碼,APT自動生成切面文件
  • 支持組件化

Installation:

1.project.gradle 添加(同步完成后再進(jìn)行下一步!!!)

    buildscript {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath 'org.aspectj:aspectjtools:1.8.9'
        classpath 'org.aspectj:aspectjweaver:1.8.9'
    }
}

2.app.gradle 添加(注意每個(gè)需要生成切面的文件的組件都需要添加annotationProcessor)

dependencies {
     implementation 'org.aspectj:aspectjrt:1.8.14'
     implementation 'com.github.TanZhiL:OkAspectjAnnotation:1.0.4'
     annotationProcessor 'com.github.TanZhiL:OkAspectjCompiler:1.0.4'
}
/*******************獨(dú)立運(yùn)行時(shí)**********************************/
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main

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.8",
                         "-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;
            }
        }
    }
}
/***********************END****************************/
/*******************作為組件時(shí)**********************************/
import com.android.build.gradle.LibraryPlugin
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main

android.libraryVariants.all { variant ->
    LibraryPlugin plugin = project.plugins.getPlugin(LibraryPlugin)
    JavaCompile javaCompile = variant.javaCompile
    javaCompile.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.logger
        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:
                case IMessage.INFO:
                    log.info message.message, message.thrown
                    break;
                case IMessage.DEBUG:
                    log.debug message.message, message.thrown
                    break;
            }
        }
    }
}

/***********************END****************************/

Usage:

  1. 在自己想要攔截的注解之上添加 @OkAspectj注解
@OkAspectj
@Target(ElementType.METHOD)
public @interface NeedLogin {
    int value()default 0;
}

  1. 在想要攔截的方法加入自己的注解
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        test();
        test1();
    }
    @NeedLogin(2)
    private void test() {

    }
    @TestAnnotaion
    private void test1() {

    }
}

3.在Application設(shè)置全局切面攔截處理

public class App extends Application {
    private static final String TAG = "App";
    @Override
    public void onCreate() {
        super.onCreate();
        OkAspectjHelper.setmHandler(new PointHandler() {
            @Override
            public void handlePoint(Class clazz, ProceedingJoinPoint joinPoint) {
                     Log.d(TAG, "handlePoint() called with: clazz = [" + clazz + "]");
                if(clazz==NeedLogin.class){
                    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
                    NeedLogin annotation = methodSignature.getMethod().getAnnotation(NeedLogin.class);
                    Log.d(TAG, "handlePoint() called with: joinPoint = [" + annotation.value() + "]");
                    try {
                        joinPoint.proceed();
                    } catch (Throwable throwable) {
                        throwable.printStackTrace();
                    }
                }
            }
        });
    }
}

4.配置完成,可以在handlePoint(Class clazz, ProceedingJoinPoint joinPoint)中自由發(fā)揮你的騷操作了!
5.也可自己編寫切面文件,然后通過調(diào)用OkAspectjHelper.notifyHandler(Class clazz,ProceedingJoinPoint joinPoint),發(fā)送切點(diǎn)信息進(jìn)行統(tǒng)一處理.

致謝

問題反饋

歡迎加星,打call https://github.com/TanZhiL/OkAspectj

關(guān)于作者

譚志龍

開源項(xiàng)目

License

Copyright (C)  tanzhilong OkAspectjFramework Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、簡述 1、AOP的概念 如果你用java做過后臺開發(fā),那么你一定知道AOP這個(gè)概念。如果不知道也無妨,套用百度...
    GitLqr閱讀 4,385評論 6 25
  • AOP,也就是面向方面編程或者說面向面編程,是一種很重要的思想。在企業(yè)級系統(tǒng)中經(jīng)常需要打印日志、事務(wù)管理這樣針對某...
    樂百川閱讀 963評論 0 8
  • AOP全稱Aspect Oriented Programming。在OOP(面向?qū)ο蟪绦蛟O(shè)計(jì))中,正是這種分...
    tuacy閱讀 383評論 0 0
  • 坐了一趟凌晨到的火車,看了一次不一樣的早晨。 早上5點(diǎn)出的站口,本來想去快餐店等著早班公交,結(jié)果沒用開門,...
    西柚x閱讀 454評論 0 0
  • 聽說經(jīng)常做一些益智類的游戲,可以讓自己的大腦不至于過早萎縮。據(jù)說經(jīng)常做一些數(shù)獨(dú),還可以提升自己的邏輯思維能力,于是...
    騎驢書生閱讀 777評論 8 51

友情鏈接更多精彩內(nèi)容