Skip to content

Commit d6b7404

Browse files
committed
完善库
1 parent b85557d commit d6b7404

File tree

11 files changed

+245
-21
lines changed

11 files changed

+245
-21
lines changed

README.md

+192
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
# ModuleCommunication
2+
3+
[![Maven central](https://img.shields.io/maven-central/v/io.github.FlyJingFish.ModuleCommunication/module-communication-annotation)](https://central.sonatype.com/search?q=io.github.FlyJingFish.ModuleCommunication)
4+
[![GitHub stars](https://img.shields.io/github/stars/FlyJingFish/ModuleCommunication.svg)](https://github.com/FlyJingFish/ModuleCommunication/stargazers)
5+
[![GitHub forks](https://img.shields.io/github/forks/FlyJingFish/ModuleCommunication.svg)](https://github.com/FlyJingFish/ModuleCommunication/network/members)
6+
[![GitHub issues](https://img.shields.io/github/issues/FlyJingFish/ModuleCommunication.svg)](https://github.com/FlyJingFish/ModuleCommunication/issues)
7+
[![GitHub license](https://img.shields.io/github/license/FlyJingFish/ModuleCommunication.svg)](https://github.com/FlyJingFish/ModuleCommunication/blob/master/LICENSE)
8+
9+
10+
### ModuleCommunication 旨在解决模块间通信,可以让模块间的代码在互相访问,并且依旧保持各个模块的代码依旧存在于其自己的模块而不需要下沉到公共模块
11+
12+
13+
## 使用步骤
14+
15+
**在开始之前可以给项目一个Star吗?非常感谢,你的支持是我唯一的动力。欢迎Star和Issues!**
16+
17+
#### 一、引入插件
18+
19+
1、在 **项目根目录**```build.gradle``` 里依赖插件
20+
21+
```gradle
22+
buildscript {
23+
dependencies {
24+
//必须项 👇
25+
classpath 'io.github.FlyJingFish.ModuleCommunication:module-communication-plugin:1.0.0'
26+
}
27+
}
28+
```
29+
30+
2、在 **项目根目录**```build.gradle``` 里依赖插件
31+
32+
```gradle
33+
plugins {
34+
//非必须项 👇,如果需要自定义切面,并且使用 android-aop-ksp 这个库的话需要配置 ,下边版本号根据你项目的 Kotlin 版本决定
35+
id 'com.google.devtools.ksp' version '1.8.0-1.0.9' apply false
36+
}
37+
```
38+
39+
[Kotlin 和 KSP Github 的匹配版本号列表](https://github.com/google/ksp/releases)
40+
41+
#### 二、配置负责通信的 module
42+
43+
- 1、例如新建一个名为 ```communication``` 的module(下文将以 ```communication``` 为例介绍)
44+
45+
- 2、在项目根目录的 ```gradle.properties``` 新增如下配置
46+
47+
```
48+
CommunicationModuleName = communication
49+
```
50+
51+
#### 三、配置通信 module
52+
53+
- 1、在 **负责通信模块(```communication```)**```build.gradle``` 添加
54+
55+
老版本
56+
57+
```gradle
58+
//必须项 👇
59+
apply plugin: 'communication.module'
60+
```
61+
62+
或者新版本
63+
64+
```gradle
65+
//必须项 👇
66+
plugins {
67+
...
68+
id 'communication.module'
69+
}
70+
```
71+
72+
- 2、在需要 暴露代码的模块 的 ```build.gradle``` 添加
73+
74+
75+
老版本
76+
77+
```gradle
78+
//必须项 👇
79+
apply plugin: 'communication.export'
80+
```
81+
82+
或者新版本
83+
84+
```gradle
85+
//必须项 👇
86+
plugins {
87+
...
88+
id 'communication.export'
89+
}
90+
```
91+
92+
#### 四、开始使用
93+
94+
- 1、在需要暴露给其他module使用的逻辑代码接口上使用 ```@ExposeInterface```
95+
96+
```kotlin
97+
@ExposeInterface
98+
interface UserHelper {
99+
fun getUser():User
100+
}
101+
```
102+
103+
- 2、把```@ExposeInterface```注解的接口类涉及的数据类上使用 ```@ExposeBean```
104+
105+
```kotlin
106+
@ExposeBean
107+
data class User (val id:String)
108+
```
109+
110+
- 3、在```@ExposeInterface```注解的接口类的实现类上使用 ```@ImplementClass(UserHelper::class)```
111+
112+
```kotlin
113+
@ImplementClass(UserHelper::class)
114+
class UserHelperImpl :UserHelper {
115+
override fun getUser():User {
116+
Log.e("UserHelperImpl","getUser")
117+
return User("1111")
118+
}
119+
}
120+
```
121+
122+
- 4、在需要使用 负责通信模块(```communication```) 的 module 上引入 ```communication```
123+
124+
```gradle
125+
compileOnly(project(":communication"))
126+
```
127+
128+
**注意引入方式必须是 compileOnly ,否则会导致打包失败**
129+
130+
- 5、调用 gradle 命令
131+
132+
<img src="/screenshot/gradle.png" alt="show" />
133+
134+
- 6、调用共享的代码
135+
136+
```kotlin
137+
class LoginActivity: AppCompatActivity() {
138+
override fun onCreate(savedInstanceState: Bundle?) {
139+
super.onCreate(savedInstanceState)
140+
val userHelper = ImplementClassUtils.getSingleInstance<UserHelper>(UserHelper::class.java)
141+
val user = userHelper.getUser()
142+
Log.e("user",""+user)
143+
}
144+
}
145+
```
146+
147+
#### 五、引入依赖库(非必须)
148+
149+
```gradle
150+
dependencies {
151+
//非必须项 👇(可以直接放在公共 module)
152+
implementation 'io.github.FlyJingFish.ModuleCommunication:module-communication-annotation:1.0.0'
153+
}
154+
```
155+
156+
这个配置项理论上来说不需要加,因为在配置上边步骤三中引入插件时,已经默认引入了
157+
158+
#### 混淆规则
159+
160+
下边是涉及到本库的一些必须混淆规则
161+
162+
```
163+
# ModuleCommunication必备混淆规则 -----start-----
164+
165+
-keep class * {
166+
@androidx.annotation.Keep <fields>;
167+
}
168+
169+
-keepnames class * implements com.flyjingfish.android_aop_annotation.base.BasePointCut
170+
-keepnames class * implements com.flyjingfish.android_aop_annotation.base.MatchClassMethod
171+
-keep class * implements com.flyjingfish.android_aop_annotation.base.BasePointCut{
172+
public <init>();
173+
}
174+
-keep class * implements com.flyjingfish.android_aop_annotation.base.MatchClassMethod{
175+
public <init>();
176+
}
177+
178+
# ModuleCommunication必备混淆规则 -----end-----
179+
```
180+
181+
182+
183+
### 最后推荐我写的另外一些库
184+
185+
- [OpenImage 轻松实现在应用内点击小图查看大图的动画放大效果](https://github.com/FlyJingFish/OpenImage)
186+
187+
- [ShapeImageView 支持显示任意图形,只有你想不到没有它做不到](https://github.com/FlyJingFish/ShapeImageView)
188+
189+
- [FormatTextViewLib 支持部分文本设置加粗、斜体、大小、下划线、删除线,下划线支持自定义距离、颜色、线的宽度;支持添加网络或本地图片](https://github.com/FlyJingFish/FormatTextViewLib)
190+
191+
- [主页查看更多开源库](https://github.com/FlyJingFish)
192+

app/build.gradle.kts

+31-9
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,7 @@ android {
2020
}
2121
}
2222

23-
buildTypes {
24-
release {
25-
isMinifyEnabled = false
26-
proguardFiles(
27-
getDefaultProguardFile("proguard-android-optimize.txt"),
28-
"proguard-rules.pro"
29-
)
30-
}
31-
}
23+
3224
compileOptions {
3325
sourceCompatibility = JavaVersion.VERSION_17
3426
targetCompatibility = JavaVersion.VERSION_17
@@ -48,6 +40,36 @@ android {
4840
excludes += "/META-INF/{AL2.0,LGPL2.1}"
4941
}
5042
}
43+
44+
signingConfigs {
45+
create("release") {
46+
storeFile = file(rootProject.file("keystore"))
47+
storePassword = "123456"
48+
keyAlias = "key0"
49+
keyPassword = "123456"
50+
}
51+
}
52+
53+
buildTypes {
54+
debug {
55+
signingConfig = signingConfigs.getByName("release")
56+
isMinifyEnabled = false
57+
isShrinkResources = false
58+
proguardFiles(
59+
getDefaultProguardFile("proguard-android-optimize.txt"),
60+
"proguard-rules.pro"
61+
)
62+
}
63+
release {
64+
signingConfig = signingConfigs.getByName("release")
65+
isMinifyEnabled = true
66+
isShrinkResources = true
67+
proguardFiles(
68+
getDefaultProguardFile("proguard-android-optimize.txt"),
69+
"proguard-rules.pro"
70+
)
71+
}
72+
}
5173
}
5274

5375
dependencies {

app/proguard-rules.pro

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@
1818

1919
# If you keep the line number information, uncomment this to
2020
# hide the original source file name.
21-
#-renamesourcefileattribute SourceFile
21+
#-renamesourcefileattribute SourceFile
22+
23+
-keepnames @com.flyjingfish.module_communication_annotation.ExposeInterface class * {*;}
24+
-keepnames @com.flyjingfish.module_communication_annotation.KeepClass class * {*;}
25+
-keep @com.flyjingfish.module_communication_annotation.KeepClass class * {
26+
public <init>();
27+
}

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ TestVersion = 1.3.0
3939
SonatypeTestCode = 1332
4040
TestType = 0
4141

42-
CommunicationModuleName = "communication"
42+
CommunicationModuleName = communication

keystore

2.57 KB
Binary file not shown.

lib-login/src/main/java/com/flyjingfish/login/LoginActivity.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package com.flyjingfish.login
33
import android.os.Bundle
44
import android.util.Log
55
import androidx.appcompat.app.AppCompatActivity
6-
import com.flyjingfish.module_communication_annotation.BeanUtils
6+
import com.flyjingfish.module_communication_annotation.ImplementClassUtils
77
import com.flyjingfish.user.UserHelper
88

99
class LoginActivity: AppCompatActivity() {
1010
override fun onCreate(savedInstanceState: Bundle?) {
1111
super.onCreate(savedInstanceState)
12-
val userHelper = BeanUtils.getSingleInstance<UserHelper>(UserHelper::class.java)
12+
val userHelper = ImplementClassUtils.getSingleInstance<UserHelper>(UserHelper::class.java)
1313
val user = userHelper.getUser()
1414
Log.e("user",""+user)
1515
}

lib-user/src/main/java/com/flyjingfish/user/UserActivity.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import android.os.Bundle
44
import android.util.Log
55
import androidx.appcompat.app.AppCompatActivity
66
import com.flyjingfish.login.LoginHelper
7-
import com.flyjingfish.module_communication_annotation.BeanUtils
7+
import com.flyjingfish.module_communication_annotation.ImplementClassUtils
88

99
class UserActivity : AppCompatActivity() {
1010
override fun onCreate(savedInstanceState: Bundle?) {
1111
super.onCreate(savedInstanceState)
12-
val loginHelper = BeanUtils.getNewInstance<LoginHelper>(LoginHelper::class.java)
12+
val loginHelper = ImplementClassUtils.getNewInstance<LoginHelper>(LoginHelper::class.java)
1313
val login = loginHelper.getLogin()
1414
Log.e("login",""+login)
1515
}

module-communication-annotation/src/main/java/com/flyjingfish/module_communication_annotation/ExposeBean.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ package com.flyjingfish.module_communication_annotation
33

44
@Retention(AnnotationRetention.SOURCE)
55
@Target(AnnotationTarget.CLASS)
6-
annotation class ExposeBean
7-
//annotation class ExposeClass(val clazzName:String = "")
6+
annotation class ExposeBean
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.flyjingfish.module_communication_annotation
22

33

4-
@Retention(AnnotationRetention.SOURCE)
4+
@Retention(AnnotationRetention.BINARY)
55
@Target(AnnotationTarget.CLASS)
66
annotation class ExposeInterface

module-communication-annotation/src/main/java/com/flyjingfish/module_communication_annotation/BeanUtils.kt module-communication-annotation/src/main/java/com/flyjingfish/module_communication_annotation/ImplementClassUtils.kt

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.flyjingfish.module_communication_annotation
22

3-
object BeanUtils {
4-
private val singleBean = mutableMapOf<Class<out Any>,Any>()
3+
import java.util.concurrent.ConcurrentHashMap
4+
5+
object ImplementClassUtils {
6+
private val singleBean = ConcurrentHashMap<Class<out Any>,Any>()
57

68
fun <T> getNewInstance(key:Class<out Any>):T{
79
val clazzName = CommunicationPackage.BIND_CLASS_PACKAGE + "." + key.simpleName +"\$\$BindClass"
@@ -19,7 +21,10 @@ object BeanUtils {
1921
var instance = singleBean[key]
2022
if (instance == null){
2123
instance = getNewInstance(key)
22-
singleBean[key] = instance as Any
24+
val oldInstance = singleBean.putIfAbsent(key,instance as Any)
25+
if (oldInstance != null){
26+
instance = oldInstance
27+
}
2328
}
2429
return instance as T
2530
}

screenshot/gradle.png

103 KB
Loading

0 commit comments

Comments
 (0)