|
1 |
| -# FragmentKey |
| 1 | +### 有人想要Android面向切面编程,今天他来了!😜,轻松完成各种骚操作!登录状态拦截,日志拦截,权限拦截,轻松搞定! |
| 2 | + |
| 3 | +### !!!目前发现Gson v2.8.6版与aspectjrt库冲突,导致编译时织入失败,建议使用gson v2.8.5版本!!! |
| 4 | + |
| 5 | +[](https://jitpack.io/#TanZhiL/OkAspectj) |
| 6 | +### 更新日志: |
| 7 | +###### v1.02 2019-10.17 |
| 8 | +* 第一次发布 |
| 9 | +#### 快速对指定函数进行切面拦截: |
| 10 | + - 注解完全自定义 |
| 11 | + - 拦截规则自定义 |
| 12 | + - 无需手动编写切面代码,APT自动生成切面文件 |
| 13 | + - 支持组件化 |
| 14 | + |
| 15 | + |
| 16 | +## Installation: |
| 17 | +1.project.gradle 添加(同步完成后再进行下一步!!!) |
| 18 | +```java |
| 19 | + buildscript { |
| 20 | + repositories { |
| 21 | + google() |
| 22 | + jcenter() |
| 23 | + maven { url 'https://jitpack.io' } |
| 24 | + } |
| 25 | + dependencies { |
| 26 | + classpath 'com.android.tools.build:gradle:3.2.1' |
| 27 | + classpath 'org.aspectj:aspectjtools:1.8.9' |
| 28 | + classpath 'org.aspectj:aspectjweaver:1.8.9' |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | +2.app.gradle 添加(注意每个需要生成切面的文件的组件都需要添加annotationProcessor) |
| 33 | +```java |
| 34 | +dependencies { |
| 35 | + implementation 'org.aspectj:aspectjrt:1.8.14' |
| 36 | + implementation 'com.github.TanZhiL.OkAspectj:okaspectj:1.0.7' |
| 37 | + annotationProcessor 'com.github.TanZhiL.OkAspectj:okaspectj-compiler:1.0.7' |
| 38 | +} |
| 39 | +/*******************独立运行时**********************************/ |
| 40 | +import org.aspectj.bridge.IMessage |
| 41 | +import org.aspectj.bridge.MessageHandler |
| 42 | +import org.aspectj.tools.ajc.Main |
| 43 | + |
| 44 | +project.android.applicationVariants.all { variant -> |
| 45 | + if (!variant.buildType.isDebuggable()) { |
| 46 | + return; |
| 47 | + } |
| 48 | + JavaCompile javaCompile = variant.javaCompile |
| 49 | + javaCompile.doLast { |
| 50 | + String[] args = ["-showWeaveInfo", |
| 51 | + "-1.8", |
| 52 | + "-inpath", javaCompile.destinationDir.toString(), |
| 53 | + "-aspectpath", javaCompile.classpath.asPath, |
| 54 | + "-d", javaCompile.destinationDir.toString(), |
| 55 | + "-classpath", javaCompile.classpath.asPath, |
| 56 | + "-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)] |
| 57 | + MessageHandler handler = new MessageHandler(true); |
| 58 | + new Main().run(args, handler); |
| 59 | + } |
| 60 | +} |
| 61 | +/***********************END****************************/ |
| 62 | +/*******************作为组件时**********************************/ |
| 63 | +import com.android.build.gradle.LibraryPlugin |
| 64 | +import org.aspectj.bridge.IMessage |
| 65 | +import org.aspectj.bridge.MessageHandler |
| 66 | +import org.aspectj.tools.ajc.Main |
| 67 | + |
| 68 | +android.libraryVariants.all { variant -> |
| 69 | + LibraryPlugin plugin = project.plugins.getPlugin(LibraryPlugin) |
| 70 | + JavaCompile javaCompile = variant.javaCompile |
| 71 | + javaCompile.doLast { |
| 72 | + String[] args = ["-showWeaveInfo", |
| 73 | + "-1.5", |
| 74 | + "-inpath", javaCompile.destinationDir.toString(), |
| 75 | + "-aspectpath", javaCompile.classpath.asPath, |
| 76 | + "-d", javaCompile.destinationDir.toString(), |
| 77 | + "-classpath", javaCompile.classpath.asPath, |
| 78 | + "-bootclasspath", plugin.project.android.bootClasspath.join( |
| 79 | + File.pathSeparator)] |
| 80 | + |
| 81 | + MessageHandler handler = new MessageHandler(true); |
| 82 | + new Main().run(args, handler) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/***********************END****************************/ |
| 87 | +``` |
| 88 | +## Usage: |
| 89 | +1. 在自己想要拦截的注解之上添加 @OkAspectj注解 |
| 90 | +```java |
| 91 | +@OkAspectj |
| 92 | +@Target(ElementType.METHOD) |
| 93 | +public @interface NeedLogin { |
| 94 | + int value()default 0; |
| 95 | +} |
| 96 | + |
| 97 | +``` |
| 98 | + |
| 99 | +2. 在想要拦截的方法加入自己的注解 |
| 100 | + |
| 101 | +```java |
| 102 | + |
| 103 | + @Override |
| 104 | + protected void onCreate(Bundle savedInstanceState) { |
| 105 | + super.onCreate(savedInstanceState); |
| 106 | + setContentView(R.layout.activity_main); |
| 107 | + test(); |
| 108 | + test1(); |
| 109 | + } |
| 110 | + @NeedLogin(2) |
| 111 | + private void test() { |
| 112 | + |
| 113 | + } |
| 114 | + @TestAnnotaion |
| 115 | + private void test1() { |
| 116 | + |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +``` |
| 121 | +3.在Application设置全局切面拦截处理 |
| 122 | + |
| 123 | +```java |
| 124 | +public class App extends Application { |
| 125 | + private static final String TAG = "App"; |
| 126 | + @Override |
| 127 | + public void onCreate() { |
| 128 | + super.onCreate(); |
| 129 | + OkAspectjHelper.init(new PointHandler() { |
| 130 | + @Override |
| 131 | + public void handlePoint(Class clazz, ProceedingJoinPoint joinPoint) { |
| 132 | + Log.d(TAG, "handlePoint() called with: clazz = [" + clazz + "]"); |
| 133 | + if(clazz==NeedLogin.class){ |
| 134 | + MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); |
| 135 | + NeedLogin annotation = methodSignature.getMethod().getAnnotation(NeedLogin.class); |
| 136 | + Log.d(TAG, "handlePoint() called with: joinPoint = [" + annotation.value() + "]"); |
| 137 | + try { |
| 138 | + joinPoint.proceed(); |
| 139 | + } catch (Throwable throwable) { |
| 140 | + throwable.printStackTrace(); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + }); |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
| 148 | +4.配置完成,可以在handlePoint(Class clazz, ProceedingJoinPoint joinPoint)中自由发挥你的骚操作了! |
| 149 | +5.也可自己编写切面文件,然后通过调用OkAspectjHelper.notifyHandler(Class clazz,ProceedingJoinPoint joinPoint),发送切点信息进行统一处理. |
| 150 | + |
| 151 | +### 配置出错的请参考 高仿喜马拉雅听Android客户端 https://github.com/TanZhiL/Zhumulangma |
| 152 | + |
| 153 | +### 致谢 |
| 154 | +* 感谢所有开源库的大佬 |
| 155 | +* 借鉴大佬 https://github.com/JakeWharton/butterknife |
| 156 | +### 问题反馈 |
| 157 | +欢迎加星,打call https://github.com/TanZhiL/OkAspectj |
| 158 | +* email:1071931588@qq.com |
| 159 | +### 关于作者 |
| 160 | +谭志龙 |
| 161 | +### 开源项目 |
| 162 | +* 快速切面编程开源库 https://github.com/TanZhiL/OkAspectj |
| 163 | +* 高仿喜马拉雅听Android客户端 https://github.com/TanZhiL/Zhumulangma |
| 164 | +* 骨架屏弹性块 https://github.com/TanZhiL/SkeletonBlock |
| 165 | +* RxPersistence是基于面向对象设计的快速持久化框架 https://github.com/TanZhiL/RxPersistence |
| 166 | +### License |
| 167 | +``` |
| 168 | +Copyright (C) tanzhilong OkAspectjFramework Open Source Project |
| 169 | +
|
| 170 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 171 | +you may not use this file except in compliance with the License. |
| 172 | +You may obtain a copy of the License at |
| 173 | +
|
| 174 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 175 | +
|
| 176 | +Unless required by applicable law or agreed to in writing, software |
| 177 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 178 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 179 | +See the License for the specific language governing permissions and |
| 180 | +limitations under the License. |
| 181 | +``` |
0 commit comments