|
| 1 | +# ModuleCommunication |
| 2 | + |
| 3 | +[](https://central.sonatype.com/search?q=io.github.FlyJingFish.ModuleCommunication) |
| 4 | +[](https://github.com/FlyJingFish/ModuleCommunication/stargazers) |
| 5 | +[](https://github.com/FlyJingFish/ModuleCommunication/network/members) |
| 6 | +[](https://github.com/FlyJingFish/ModuleCommunication/issues) |
| 7 | +[](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 | + |
0 commit comments