• Stars
    star
    262
  • Rank 150,648 (Top 4 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created about 7 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

android的AOP框架,支持android studio、gradle最新版本

SAF-AOP

@Tony沈哲 on weibo Download License

基于 Aspecj 实现的 Android AOP 框架,并使用沪江的gradle 插件:https://github.com/HujiangTechnology/gradle_plugin_android_aspectjx

下载安装

在根目录下的build.gradle中添加

buildscript {
     repositories {
         jcenter()
     }
     dependencies {
         classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.4'
     }
 }

在app 模块目录下的build.gradle中添加

apply plugin: 'com.hujiang.android-aspectjx'

...

dependencies {
    compile 'com.safframework:saf-aop:1.3.0'
    ...
}

基于aspectj的AOP,无需使用耗费性能的反射.不过,需要在build.gradle中配置一下aspectj

注解名称 作用 备注
@Async 借助RxJava,异步地执行app中的方法
@Cacheable Spring Cache风格的Cache注解,将结果放于缓存中 只适用于android4.0以后
@LogMethod 将方法的入参和出参都打印出来,可以用于调试
@HookMethod 可以在调用某个方法之前、以及之后进行hook 比较适合埋点的场景,可以单独使用也可以跟任何自定义注解配合使用。也支持在匿名内部类中使用
@Prefs 将方法返回的结果放入AppPrefs中 只适用于android4.0以后
@Safe 可以安全地执行方法,而无需考虑是否会抛出运行时异常 支持在捕获异常的时候进行监听
@Trace 用于追踪某个方法花费的时间,可以用于性能调优的评判 支持追踪匿名内部类中的方法
@Permission 可用于运行时动态地申请权限

@Async的使用方法:

	@Async
	private void useAsync() {
		Log.e(TAG, " thread=" + Thread.currentThread().getId());
		Log.e(TAG, "ui thread=" + Looper.getMainLooper().getThread().getId());
	}

@Cacheable的使用方法:

	@Cacheable(key = "user")
	private User initData() {
		User user = new User();
		user.userName = "tony";
		user.password = "123456";
		return user;
	}

这里的@Cacheable,实际上用到Cache,要获取Cache也很简单.

@Trace的使用方法:

	@Trace
	@Async
	private void loadUser() {
		Log.e(TAG, " thread=" + Thread.currentThread().getId());
		Log.e(TAG, "ui thread=" + Looper.getMainLooper().getThread().getId());
		Cache cache = Cache.get(this);
		User user = (User) cache.getObject("user");
		Toast.makeText(MainActivity.this, SAFUtils.printObject(user), Toast.LENGTH_SHORT).show();
	}

将@Trace和@Async两个注解结合使用,可以看到调用loadUser()方法花费的时间.

05-18 14:31:31.229 21190-21190/app.magicwindow.cn.testsaf I/MainActivity: MainActivity=loadUser() take [1ms]
05-18 14:31:31.231 21190-22033/app.magicwindow.cn.testsaf E/com.test.saf.activity.MainActivity:  thread=14876
05-18 14:31:31.231 21190-22033/app.magicwindow.cn.testsaf E/com.test.saf.activity.MainActivity: ui thread=1

@Trace还支持在匿名内部类中使用

    @Trace
    private void initData() {

        Observable.create(new ObservableOnSubscribe<String>() {

            @Trace
            @Override
            public void subscribe(@NonNull ObservableEmitter<String> e) throws Exception {

                e.onNext("111");
                e.onNext("222");
                e.onNext("333");

            }
        }).subscribe(new Consumer<String>() {

            @Trace
            @Override
            public void accept(@NonNull String str) throws Exception {

            }
        });
    }

@HookMethod的使用方法:

不写beforeMethod和afterMethod,则相当于没有使用@HookMethod
beforeMethod和afterMethod对应的都是方法名,分别表示在调用doSomething()之前执行和之后执行。目前还不支持在beforeMethod和afterMethod中传递参数。

   @HookMethod(beforeMethod="dosthbeforeMethod",afterMethod="dosthafterMethod")
   void doSomething() {

   }

@HookMethod 同样支持在匿名内部类中使用

    @HookMethod(beforeMethod = "method1",afterMethod = "method2")
    private void initData() {

        L.i("initData()");
    }

    private void method1() {
        L.i("method1() is called before initData()");
    }

    private void method2() {
        L.i("method2() is called after initData()");
    }

    private void testRx() {

        Observable.just("tony")
                .subscribe(new Consumer<String>() {

                    @HookMethod(beforeMethod = "testRxBefore")
                    @Override
                    public void accept(@NonNull String s) throws Exception {
                        System.out.println("s="+s);
                    }

                    private void testRxBefore() {
                        L.i("testRxBefore() is called before accept()");
                    }
                });

Proguard

-keep class com.safframework.aop.** { *; }

联系方式

Wechat:fengzhizi715

SAF-AOP相关文章:

http://www.jianshu.com/p/9e78560cadad

http://www.jianshu.com/p/2779e3bb1f14

Java与Android技术栈:每周更新推送原创技术文章,欢迎扫描下方的公众号二维码并关注,期待与您的共同成长和进步。

ChangeLog

版本更新记录

License

Copyright (C) 2017 - present, Tony Shen.

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.

More Repositories

1

NetDiscovery

NetDiscovery 是一款基于 Vert.x、RxJava 2 等框架实现的通用爬虫框架/中间件。
Java
635
star
2

ProxyPool

给爬虫使用的代理IP池
Java
548
star
3

SAF

(Deprecated) SAF(Simple Android Framework)是一个简单的android框架,它为开发Android app提供了基础性组件。
Java
299
star
4

SAF-Kotlin-log

完全基于 Kotlin 开发的 Android 日志框架,提供极简的 API
Kotlin
269
star
5

user-agent-list

常用浏览器的user-agent列表
Java
234
star
6

SAF-Kotlin-Utils

用 Kolin 做的 Android Utils 库,包括 utils 和 extension
Kotlin
187
star
7

AnalysisApp

一个快速分析某个app使用哪些sdk的小工具
Groovy
163
star
8

AndroidServer

基于 Kotlin + Netty 开发,为 Android App 提供 Server 的功能,包括 Http、TCP、WebSocket 服务
Kotlin
140
star
9

RxCache

A local reactive cache for Java and Android. Now, it supports heap memory、off-heap memory and disk cache.
Java
136
star
10

RxJavaInAction

《RxJava2.x 实战》一书中包含的例子。
Java
120
star
11

saf-logginginterceptor

Android项目中,OKHttp的日志的拦截器
Java
87
star
12

kotlin_tutorial

掘金的小册《Android 进阶:基于 Kotlin 的 Android App 开发实践》中的相关的例子
Kotlin
59
star
13

Tess-TwoDemo

Java
56
star
14

PicCrawler

使用RxJava2 和 Java 8的特性开发的图片爬虫
Java
55
star
15

Netty4Android

Kotlin + Netty 在 Android 上实现 Socket 的服务端 demo
Kotlin
53
star
16

Lifecycle-Coroutines-Extension

AAC 的 Lifecycle 结合 Kotlin Coroutines 进行使用
Kotlin
49
star
17

SAF-Object-Delegate

基于 Kotlin 的委托机制实现对 Extra、SharedPreferences 的封装。已经适配 AndroidX
Kotlin
46
star
18

TFLite-MnistDemo

Kotlin
46
star
19

adbd-connector

Kotlin
43
star
20

bytekit

Java 字节操作的工具库(不是字节码的工具库)
Java
39
star
21

EventBus

使用 Kotlin Coroutine 开发的 EventBus
Kotlin
39
star
22

RxCache4a

RxCache(https://github.com/fengzhizi715/RxCache) for Android
Java
35
star
23

okhttp-extension

okhttp-extension 是针对 okhttp 3 增强的网络框架。使用 Kotlin 特性编写,提供便捷的 DSL 方式创建网络请求,支持协程、响应式编程等等。
Kotlin
35
star
24

blockchain_study

Java
33
star
25

SAF-Kotlin-Router

android路由框架,支持模块化架构
Java
32
star
26

NetDiagnose

Android 网络诊断工具,使用 Kotlin + LiveData + MVVM + Coroutines 开发
Kotlin
26
star
27

KStateMachine

使用 Kotlin 特性实现的有限状态机 (FSM) 框架,基于事件驱动。
Kotlin
24
star
28

kotlin-spring-demo

Kotlin 整合 Spring Boot 2的例子
Kotlin
19
star
29

kvalidation

基于 Kotlin 特性实现的验证框架
Kotlin
17
star
30

LiveDataExtension

Kotlin
16
star
31

Kotlin-Coroutines-Utils

Kotlin Coroutines 的工具类库
Kotlin
16
star
32

RxJava-Utils

在日常开发实践中,本人所积累的 RxJava 相关的工具类
Kotlin
12
star
33

RxConditions

Java
11
star
34

SAF-Kotlin-InjectView

用Kotlin打造的简化版本的ButterKnife
Kotlin
8
star
35

okhttp-logging-interceptor

支持 Android、桌面、后端项目使用的 okhttp 日志拦截器
Kotlin
7
star
36

tony-common

个人java 后台项目中总结的常用类
Java
7
star
37

MLogCat

Java
6
star
38

kcommand

kcommand 是基于 Kotlin 特性实现的执行 Linux/Windows 命令的库
Kotlin
6
star
39

Advance-Kotlin-Tutorials

Kotlin
5
star
40

PubSub

使用 Kotlin Coroutines 实现的 Local Pub/Sub、Event Bus、Message Bus
Kotlin
5
star
41

kotlin-spring-reactive-coroutine-demo

Kotlin 使用 WebFlux、RxJava2 以及 coroutines 的例子
Kotlin
4
star
42

Vertxs

对Vert.x框架的封装
Java
3
star
43

FXHParser

抓取非小号数字货币
Java
2
star
44

NetDiscovery-Admin

Java
2
star
45

multi-level-cache

A multi-level cache framework implemented by RxCache(local cache) and Redis(remote cache).
1
star
46

websocket-demo

Kotlin
1
star
47

kjdbc

Kotlin
1
star
48

retrofit-SAF

saf 的 retrofit 框架,去掉gson和appengin以及okhttp依赖,使用saf中的fastjson和restclient替代
Java
1
star