Skip to content

How to use actors for interaction between modules on the android side without adding injection code to the main module (:app)

Notifications You must be signed in to change notification settings

xuehuiniaoyu/actor-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

actor-demo

How to use actor-kotlin for interaction between modules on the android side without adding injection code to the main module (:app)

https://github.com/xuehuiniaoyu/actor-kotlin

implementation 'io.github.xuehuiniaoyu:actor-kotlin:2.1.0'

Java动态伪装工具,能够用伪装接口代理真实对象。

纯Kotlin开发,使用简单但功能强大,可用于组件化开发或插件化项目开发。

1. ActorBean

ActorBean:对象反射工具,不仅有get/set方法,还能把对象抽象到接口,方便集成。

data class Student(val name: String, var studentStatus: String? = null)

1.1. 直接获取属性值

val student = Student("张三")
val actorStudent = ActorBean(student)
val name = actorStudent("name")

1.2. 通过接口获取属性值

Interface StudentProxy {
    @GET("name") fun getName()
}

val actorStudent = ActorBean(student).agent(StudentProxy::class.java)
val name = actorStudent.getName()

1.3. 直接赋值

val actorStudent = ActorBean(student)
actorStudent.set("studentStatus", "学籍")

1.4. 通过接口给属性赋值


Interface StudentProxy {
    @SET("studentStatus") fun setStudentStatus(status: String)
}

val actorStudent = ActorBean(student).agent(StudentProxy::class.java)
actorStudent.setStudentStatus("学籍")

2. ActorInterface

ActorInterface 接口动态伪装工具,通过Interface2伪装成Interface1

interface Interface1 {
    fun log(log: String)
}

interface Interface2 {
    fun log(log: Any)
}
val actorInterface = ActorInterface(object: Interface2 {
    override fun log(log: Any) {
        println("log: $log")
    }
})

actorInterface.getImplement<Interface1> { impl ->
    impl?.log("hello world!")
}
actorInterface.bindInterface(Interface1::class.java)
actorInterface.recovery()

3. @DynamicImplementation

表示:伪装类需要动态实现

完整代码:
interface Interface1 {
    fun log(log: String)
}

interface Interface2 {
    fun log(log: Any)
}
class Kit {
    fun call(key: String, interface1: Interface1) {
        interface1.log("$key -------> hello world from call...")
    }
}
interface KitProxy {
    fun call(key: String, @DynamicImplementation data: Any)
}

利用Actor让KitProxy伪装成Kit对象

val callback = object: Interface2 {
    override fun log(log: Any) {
        println("log: $log")
    }
}

val proxy = Actor(Kit()).imitator(KitProxy::class.java)
proxy.call("hello", callback)

About

How to use actors for interaction between modules on the android side without adding injection code to the main module (:app)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages