HighCapable / YukiHookAPI

⛱️ An efficient Hook API and Xposed Module solution built in Kotlin.
https://highcapable.github.io/YukiHookAPI/
Apache License 2.0
1.42k stars 105 forks source link

New Hook Entry Class #48

Open fankes opened 1 year ago

fankes commented 1 year ago

New Hook Entry Class

The entry class currently used by the API is designed with reference to the working mode and principle of the native Xposed API.

The biggest problem encountered so far is the logical confusion of the entry class.

For example, we cannot clearly get a Host App's lifecycle from the beginning.

The current API solution is to use onAppLifecycle to monitor the Host App's lifecycle method.

So we may adopt a new entry class design pattern in later versions to bring better experience to Xposed Module developers.

The following example

@YukiXposedModule
class YourModule(hookContext: HookContext) : YukiBaseModule(hookContext: HookContext) {

    init {
        YukiHookAPI.configs {
            isDebug = false
        }
        // HookContext means the global system context
        hookContext.packageName // "android"
        // Listen to the loading events of the native Xposed API
        YukiXposedEvent.events {
            onInitZygote {
                // The it object is [StartupParam]
            }
            onHandleLoadPackage {
                // The it object is [LoadPackageParam]
            }
            onHandleInitPackageResources {
                // The it object is [InitPackageResourcesParam]
            }
        }
    }

    override fun onHook() {
        loadApp(name = "com.example.demo") {
            // Do something...
        }
    }
}

Below are the current entry class schemes.

The following example

@InjectYukiHookWithXposed
object HookEntry : IYukiHookXposedInit {

    override fun onInit() = configs {
        isDebug = false
    }

    override fun onHook() = encase {
        loadApp(name = "com.example.demo") {
            // Do something...
        }
    }

    override fun onXposedEvent() {
        // Listen to the loading events of the native Xposed API
        YukiXposedEvent.events {
            onInitZygote {
                // The it object is [StartupParam]
            }
            onHandleLoadPackage {
                // The it object is [LoadPackageParam]
            }
            onHandleInitPackageResources {
                // The it object is [InitPackageResourcesParam]
            }
        }
    }
}

新的入口类方案

API 目前采用的入口类参考了原生 Xposed API 的工作模式与原理而设计,目前遇到的最大的问题就是入口类逻辑上的混乱问题。

例如我们无法从开始就明确地得到一个宿主的生命周期,目前 API 给出的解决方案是使用 onAppLifecycle 来监听宿主的生命周期方法。

所以我们可能会在后期的版本采用一种新的入口类设计模式来给 Xposed 模块开发者带来更好的体验。

示例如下

@YukiXposedModule
class YourModule(hookContext: HookContext) : YukiBaseModule(hookContext: HookContext) {

    init {
        YukiHookAPI.configs {
            isDebug = false
        }
        // HookContext 即全局系统上下文
        hookContext.packageName // "android"
        // 监听原生 Xposed API 的装载事件
        YukiXposedEvent.events {
            onInitZygote {
                // it 对象即 [StartupParam]
            }
            onHandleLoadPackage {
                // it 对象即 [LoadPackageParam]
            }
            onHandleInitPackageResources {
                // it 对象即 [InitPackageResourcesParam]
            }
        }
    }

    override fun onHook() {
        loadApp(name = "com.example.demo") {
            // Do something...
        }
    }
}

以下是目前采取的入口类方案。

示例如下

@InjectYukiHookWithXposed
object HookEntry : IYukiHookXposedInit {

    override fun onInit() = configs {
        isDebug = false
    }

    override fun onHook() = encase {
        loadApp(name = "com.example.demo") {
            // Do something...
        }
    }

    override fun onXposedEvent() {
        // 监听原生 Xposed API 的装载事件
        YukiXposedEvent.events {
            onInitZygote {
                // it 对象即 [StartupParam]
            }
            onHandleLoadPackage {
                // it 对象即 [LoadPackageParam]
            }
            onHandleInitPackageResources {
                // it 对象即 [InitPackageResourcesParam]
            }
        }
    }
}