|
1 |
| -### 有人想要Android面向切面编程,今天他来了!😜,轻松完成各种骚操作!登录状态拦截,日志拦截,权限拦截,轻松搞定! |
| 1 | +### FragmentKey一款解决使用newInstance创建fragment定义key传值问题的apt框架 |
2 | 2 |
|
3 |
| -### !!!目前发现Gson v2.8.6版与aspectjrt库冲突,导致编译时织入失败,建议使用gson v2.8.5版本!!! |
4 |
| - |
5 |
| -[](https://jitpack.io/#TanZhiL/OkAspectj) |
| 3 | +[](https://jitpack.io/#TanZhiL/FragmentKey) |
6 | 4 | ### 更新日志:
|
7 |
| -###### v1.02 2019-10.17 |
| 5 | +###### v1.0.0 2020.1.17 |
8 | 6 | * 第一次发布
|
9 |
| -#### 快速对指定函数进行切面拦截: |
10 |
| - - 注解完全自定义 |
11 |
| - - 拦截规则自定义 |
12 |
| - - 无需手动编写切面代码,APT自动生成切面文件 |
13 |
| - - 支持组件化 |
14 |
| - |
15 |
| - |
| 7 | +#### 使用前: |
| 8 | +``` |
| 9 | + public TFragment newInstance(String username, String password, int age) { |
| 10 | + TFragment tFragment = new TFragment(); |
| 11 | +//传值 |
| 12 | + Bundle bundle = new Bundle(); |
| 13 | + bundle.putString("username", username); |
| 14 | + bundle.putString("password", password); |
| 15 | + bundle.putInt("age", age); |
| 16 | + tFragment.setArguments(bundle); |
| 17 | + return tFragment; |
| 18 | + } |
| 19 | + |
| 20 | + @Override |
| 21 | + public void onAttach(Context context) { |
| 22 | + super.onAttach(context); |
| 23 | + //使用 |
| 24 | + Bundle arguments = getArguments(); |
| 25 | + mUsername = arguments.getString("username"); |
| 26 | + mPassword = arguments.getString("password"); |
| 27 | + age = arguments.getInt("age"); |
| 28 | + } |
| 29 | +``` |
| 30 | +#### 使用后: |
| 31 | +``` |
| 32 | + //定义 |
| 33 | + @Inject |
| 34 | + public String mUsername; |
| 35 | + @Inject(name = "password1") |
| 36 | + public String mPassword; |
| 37 | + @Inject |
| 38 | + public int age; |
| 39 | + //传值 |
| 40 | + TFragment2 tFragment = new TFragment2Key().get("姓名", "密码", 10); |
| 41 | + |
| 42 | + @Nullable |
| 43 | + @Override |
| 44 | + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
| 45 | + //使用 |
| 46 | + Log.d(TAG, mUsername); |
| 47 | + Log.d(TAG, mPassword); |
| 48 | + Log.d(TAG, String.valueOf(age)); |
| 49 | + return super.onCreateView(inflater, container, savedInstanceState); |
| 50 | + } |
| 51 | +``` |
| 52 | +可以看出此框架简化了传值过程,避免了使用key来传递数据带来的麻烦. |
16 | 53 | ## Installation:
|
17 |
| -1.project.gradle 添加(同步完成后再进行下一步!!!) |
| 54 | +1.project.gradle |
18 | 55 | ```java
|
19 | 56 | buildscript {
|
20 | 57 | repositories {
|
|
24 | 61 | }
|
25 | 62 | dependencies {
|
26 | 63 | 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 | 64 | }
|
30 | 65 | }
|
31 | 66 | ```
|
32 |
| -2.app.gradle 添加(注意每个需要生成切面的文件的组件都需要添加annotationProcessor) |
| 67 | +2.app.gradle 添加 |
33 | 68 | ```java
|
34 | 69 | 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' |
| 70 | + implementation 'com.github.TanZhiL.FragmentKey:fragmentkey:1.0.0' |
| 71 | + annotationProcessor 'com.github.TanZhiL.FragmentKey:fragmentkey-compiler:1.0.0' |
38 | 72 | }
|
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 | 73 | ```
|
88 | 74 | ## Usage:
|
89 |
| -1. 在自己想要拦截的注解之上添加 @OkAspectj注解 |
| 75 | +1.在需要外部传递的字段上加上Inject注解 |
90 | 76 | ```java
|
91 |
| -@OkAspectj |
92 |
| -@Target(ElementType.METHOD) |
93 |
| -public @interface NeedLogin { |
94 |
| - int value()default 0; |
95 |
| -} |
96 |
| - |
| 77 | + @Inject |
| 78 | + public String mUsername; |
| 79 | + //name为自定义key值,默认为字段名 |
| 80 | + @Inject(name = "password1") |
| 81 | + public String mPassword; |
| 82 | + @Inject |
| 83 | + public int age; |
97 | 84 | ```
|
98 |
| - |
99 |
| -2. 在想要拦截的方法加入自己的注解 |
100 |
| - |
| 85 | +2.创建fragment实例时使用xxxKey.get(...)方法; |
101 | 86 | ```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 |
| - |
| 87 | + TFragment2 tFragment = new TFragment2Key().get("姓名", "密码", 10); |
120 | 88 | ```
|
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 |
| -} |
| 89 | +##注意: |
| 90 | +目前以支持bundle能传递的常见类型字段 |
| 91 | +``` |
| 92 | + @Inject |
| 93 | + protected String s; |
| 94 | + @Inject |
| 95 | + protected Integer i; |
| 96 | + @Inject |
| 97 | + protected boolean b; |
| 98 | + @Inject |
| 99 | + protected float f; |
| 100 | + @Inject |
| 101 | + protected double d; |
| 102 | + @Inject |
| 103 | + protected long l; |
| 104 | + @Inject |
| 105 | + protected ArrayList<String> ls; |
| 106 | + @Inject |
| 107 | + protected ArrayList<Integer> li; |
| 108 | + @Inject |
| 109 | + protected ArrayList<Parcelable> lp; |
| 110 | + @Inject |
| 111 | + protected Serializable se; |
| 112 | + @Inject |
| 113 | + protected Parcelable p; |
147 | 114 | ```
|
148 |
| -4.配置完成,可以在handlePoint(Class clazz, ProceedingJoinPoint joinPoint)中自由发挥你的骚操作了! |
149 |
| -5.也可自己编写切面文件,然后通过调用OkAspectjHelper.notifyHandler(Class clazz,ProceedingJoinPoint joinPoint),发送切点信息进行统一处理. |
150 |
| - |
151 |
| -### 配置出错的请参考 高仿喜马拉雅听Android客户端 https://github.com/TanZhiL/Zhumulangma |
152 | 115 |
|
153 | 116 | ### 致谢
|
154 | 117 | * 感谢所有开源库的大佬
|
155 | 118 | * 借鉴大佬 https://github.com/JakeWharton/butterknife
|
156 | 119 | ### 问题反馈
|
157 |
| -欢迎加星,打call https://github.com/TanZhiL/OkAspectj |
| 120 | +欢迎加星,打call https://github.com/TanZhiL/FragmentKey |
158 | 121 | * email:1071931588@qq.com
|
159 | 122 | ### 关于作者
|
160 | 123 | 谭志龙
|
161 | 124 | ### 开源项目
|
162 | 125 | * 快速切面编程开源库 https://github.com/TanZhiL/OkAspectj
|
| 126 | +* 一款解决使用newInstance创建fragment定义key传值问题的apt框架 https://github.com/TanZhiL/FragmentKey |
163 | 127 | * 高仿喜马拉雅听Android客户端 https://github.com/TanZhiL/Zhumulangma
|
| 128 | +* 基于面向对象设计的快速持久化框架 https://github.com/TanZhiL/RxPersistence |
164 | 129 | * 骨架屏弹性块 https://github.com/TanZhiL/SkeletonBlock
|
165 |
| -* RxPersistence是基于面向对象设计的快速持久化框架 https://github.com/TanZhiL/RxPersistence |
166 | 130 | ### License
|
167 | 131 | ```
|
168 |
| -Copyright (C) tanzhilong OkAspectjFramework Open Source Project |
| 132 | +Copyright (C) tanzhilong FragmentKey Open Source Project |
169 | 133 |
|
170 | 134 | Licensed under the Apache License, Version 2.0 (the "License");
|
171 | 135 | you may not use this file except in compliance with the License.
|
|
0 commit comments