app
|
+-modules // 业务层
| |
| +- module1 // 业务模块1
| |
| +- module2 // 业务模块2
|
+-injectservice
| |
| +-injectservice_plugin // 插件实现
| |
| +-injectservice_runtime // 插件runtime
|
+-base
|
+-lib_interface // 定义接口
主要功能就是 module2
不通过依赖 module1
来实现访问 mdoule1
的接口实现,模块间交互的接口都放在 lib_interface
中
添加jitpack依赖
maven { url 'https://jitpack.io' }
project的build.gralde中依赖插件版本
buildscript {
dependencies {
classpath "com.github.aidaole:InjectService:0.0.5"
}
}
app的build.gradle中应用插件
plugins {
// ...
id 'com.aidaole.injectservice'
}
modules中同级模块依赖runtime
dependencies {
implementation "com.github.aidaole.InjectService:injectservice_runtime:0.0.5"
}
首先到 InjectService clone下来代码 sync
项目通过
然后找到发布插件的 gradle task: publishMavenPublicationToMavenRepository
发布插件
然后在 project
的 repo
文件夹下可以找到插件
settings.gradle
添加
pluginManagement {
repositories {
// ...
maven { url './repo' }
}
}`
project的 build.gradle
添加
buildscript {
dependencies {
classpath "com.aidaole.plugin:injectservice:1.0-SNAPSHOT"
}
}
app 的 build.gradle
应用插件
plugins {
// ...
id 'com.aidaole.injectservice'
}
base
层 lib_interface
,创建接口类。 不依赖任何库
interface DialogInterface { // module1 接口
fun dialog(): String
}
public interface WindowInterface { // module2 接口
String hello();
}
业务层中, :module1
定义实现类,并添加注解实现的是DialogInterface
接口;
:module2
定义实现类, 并添加实现类注解 WindowInterface
@InjectService(DialogInterface::class)
class DialogInterfaceImpl : DialogInterface {
override fun dialog(): String {
return "inject dialog"
}
}
@InjectService(WindowInterface::class)
class WindowInterfaceImpl : WindowInterface {
override fun hello(): String {
val dialog = InjectManager.get(DialogInterface::class.java)
return "hello " + dialog.dialog()
}
}
:module1
,:module2
无项目依赖,但是都要依赖底层接口和runtime
dependencies {
// ...
implementation project(":base:lib_interface")
implementation project(":injectservice:injectservice_runtime")
}
app 层初始化和调用
class MyApplication : Application() {
override fun attachBaseContext(base: Context?) {
super.attachBaseContext(base)
InjectManager.init() // 初始化注入管理器
}
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 获取并调用接口
val windowImpl = InjectManager.get(WindowInterface::class.java)
findViewById<TextView>(R.id.test_text).text = windowImpl.hello()
}
}