Skip to content

Commit 4136c1e

Browse files
committed
README
1 parent 2b6cd6e commit 4136c1e

File tree

6 files changed

+125
-132
lines changed

6 files changed

+125
-132
lines changed

README.md

+92-128
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,57 @@
1-
### 有人想要Android面向切面编程,今天他来了!😜,轻松完成各种骚操作!登录状态拦截,日志拦截,权限拦截,轻松搞定!
1+
### FragmentKey一款解决使用newInstance创建fragment定义key传值问题的apt框架
22

3-
### !!!目前发现Gson v2.8.6版与aspectjrt库冲突,导致编译时织入失败,建议使用gson v2.8.5版本!!!
4-
5-
[![](https://jitpack.io/v/TanZhiL/OkAspectj.svg)](https://jitpack.io/#TanZhiL/OkAspectj)
3+
[![](https://jitpack.io/v/TanZhiL/FragmentKey.svg)](https://jitpack.io/#TanZhiL/FragmentKey)
64
### 更新日志:
7-
###### v1.02 2019-10.17
5+
###### v1.0.0 2020.1.17
86
* 第一次发布
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来传递数据带来的麻烦.
1653
## Installation:
17-
1.project.gradle 添加(同步完成后再进行下一步!!!)
54+
1.project.gradle
1855
```java
1956
buildscript {
2057
repositories {
@@ -24,148 +61,75 @@
2461
}
2562
dependencies {
2663
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'
2964
}
3065
}
3166
```
32-
2.app.gradle 添加(注意每个需要生成切面的文件的组件都需要添加annotationProcessor)
67+
2.app.gradle 添加
3368
```java
3469
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'
3872
}
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****************************/
8773
```
8874
## Usage:
89-
1. 在自己想要拦截的注解之上添加 @OkAspectj注解
75+
1.在需要外部传递的字段上加上Inject注解
9076
```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;
9784
```
98-
99-
2. 在想要拦截的方法加入自己的注解
100-
85+
2.创建fragment实例时使用xxxKey.get(...)方法;
10186
```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);
12088
```
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;
147114
```
148-
4.配置完成,可以在handlePoint(Class clazz, ProceedingJoinPoint joinPoint)中自由发挥你的骚操作了!
149-
5.也可自己编写切面文件,然后通过调用OkAspectjHelper.notifyHandler(Class clazz,ProceedingJoinPoint joinPoint),发送切点信息进行统一处理.
150-
151-
### 配置出错的请参考 高仿喜马拉雅听Android客户端 https://github.com/TanZhiL/Zhumulangma
152115

153116
### 致谢
154117
* 感谢所有开源库的大佬
155118
* 借鉴大佬 https://github.com/JakeWharton/butterknife
156119
### 问题反馈
157-
欢迎加星,打call https://github.com/TanZhiL/OkAspectj
120+
欢迎加星,打call https://github.com/TanZhiL/FragmentKey
158121
* email:1071931588@qq.com
159122
### 关于作者
160123
谭志龙
161124
### 开源项目
162125
* 快速切面编程开源库 https://github.com/TanZhiL/OkAspectj
126+
* 一款解决使用newInstance创建fragment定义key传值问题的apt框架 https://github.com/TanZhiL/FragmentKey
163127
* 高仿喜马拉雅听Android客户端 https://github.com/TanZhiL/Zhumulangma
128+
* 基于面向对象设计的快速持久化框架 https://github.com/TanZhiL/RxPersistence
164129
* 骨架屏弹性块 https://github.com/TanZhiL/SkeletonBlock
165-
* RxPersistence是基于面向对象设计的快速持久化框架 https://github.com/TanZhiL/RxPersistence
166130
### License
167131
```
168-
Copyright (C) tanzhilong OkAspectjFramework Open Source Project
132+
Copyright (C) tanzhilong FragmentKey Open Source Project
169133
170134
Licensed under the Apache License, Version 2.0 (the "License");
171135
you may not use this file except in compliance with the License.

app/src/main/java/com/thomas/simple/MainActivity.java

+3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ protected void onCreate(Bundle savedInstanceState) {
1717
super.onCreate(savedInstanceState);
1818
setContentView(R.layout.activity_main);
1919

20+
TFragment2 tFragment = new TFragment2Key().get("姓名", "密码", 10);
21+
2022
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
23+
2124
fragmentTransaction.add(android.R.id.content,new TFragment2Key().get("姓名","密码",10)).commit();
2225

2326
}

app/src/main/java/com/thomas/simple/TFragment.java

+6
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,10 @@ public class TFragment extends Fragment {
4242
@Inject
4343
protected Parcelable p;
4444

45+
public TFragment newInstance(){
46+
TFragment tFragment=new TFragment();
47+
48+
49+
return tFragment;
50+
}
4551
}

app/src/main/java/com/thomas/simple/TFragment2.java

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.thomas.simple;
22

3+
import android.content.Context;
34
import android.os.Bundle;
45
import android.util.Log;
56
import android.view.LayoutInflater;
@@ -37,4 +38,23 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
3738
Log.d(TAG, String.valueOf(age));
3839
return super.onCreateView(inflater, container, savedInstanceState);
3940
}
41+
42+
public TFragment newInstance(String username, String password, int age) {
43+
TFragment tFragment = new TFragment();
44+
Bundle bundle = new Bundle();
45+
bundle.putString("username", username);
46+
bundle.putString("password", password);
47+
bundle.putInt("age", age);
48+
tFragment.setArguments(bundle);
49+
return tFragment;
50+
}
51+
52+
@Override
53+
public void onAttach(Context context) {
54+
super.onAttach(context);
55+
Bundle arguments = getArguments();
56+
mUsername = arguments.getString("username");
57+
mPassword = arguments.getString("password");
58+
age = arguments.getInt("age");
59+
}
4060
}
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
POM_NAME=OkAspectj Compiler
2-
POM_ARTIFACT_ID=okaspectj-compiler
1+
POM_NAME=FragmentKey Compiler
2+
POM_ARTIFACT_ID=fragmentkey-compiler
33
POM_PACKAGING=jar

fragmentkey/gradle.properties

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
POM_NAME=OkAspectj
2-
POM_ARTIFACT_ID=okaspectj
1+
POM_NAME=FragmentKey
2+
POM_ARTIFACT_ID=fragmentkey
33
POM_PACKAGING=jar

0 commit comments

Comments
 (0)