8000 GitHub - aidaole/InjectService: 组件化项目中,提供同级模块访问能力,避免循环依赖。记录transform+asm怎么扫描生成代码
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

组件化项目中,提供同级模块访问能力,避免循环依赖。记录transform+asm怎么扫描生成代码

Notifications You must be signed in to change notification settings

aidaole/InjectService

Repository files navigation

1. 介绍项目结构

app
  |
  +-modules         // 业务层
  |   |
  |   +- module1    // 业务模块1
  |   |
  |   +- module2    // 业务模块2
  |
  +-injectservice
  |   |
  |   +-injectservice_plugin  // 插件实现
  |   |
  |   +-injectservice_runtime // 插件runtime
  |  
  +-base
      |
      +-lib_interface         // 定义接口 

主要功能就是 module2 不通过依赖 module1 来实现访问 mdoule1 的接口实现,模块间交互的接口都放在 lib_interface

2. 使用方法

2.1 依赖jitpack产物

添加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"
}

2.2 或者自己编译发布插件

首先到 InjectService clone下来代码 sync 项目通过

然后找到发布插件的 gradle task: publishMavenPublicationToMavenRepository 发布插件

然后在 projectrepo 文件夹下可以找到插件

2.2.1 本地发布

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'
}

2.3 使用插件注入

baselib_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()
    }
}

About

组件化项目中,提供同级模块访问能力,避免循环依赖。记录transform+asm怎么扫描生成代码

Resources

Stars

Watchers

Forks

Packages

No packages published
0