Maintainers
Pricing
Enterprise
More
More
Documentation
Try Live
Rankings
Install
Blog
Docs7
Ask
New
Add Docs
YukiHookAPI
https://github.com/highcapable/yukihookapi
Admin
⛱️ An efficient Hook API and Xposed Module solution built in Kotlin.
Tokens:
171,595
Snippets:
1,184
Trust Score:
7.1
License:
Apache-2.0
Update:
3 weeks ago
Context
Chat
Benchmark
66.48
Latest
Show doc for...
Code
Info
Show Results
Tokens:
Raw
Copy
Link
Tokens:
Raw
Copy
Link
### Hook a method with loadApp and hook Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/index.md Shows the core usage of Yuki Hook API: load an app by package name, resolve a method (e.g., Activity.onCreate), and hook it with before/after callbacks. This is the primary example for getting started with method hooking. ```kotlin loadApp(name = "com.android.browser") { Activity::class.resolve().firstMethod { name = "onCreate" parameters(Bundle::class) }.hook { before { // Your code here. } after { // Your code here. } } } ``` -------------------------------- ### Start HostTestActivity from Context Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/host-inject.md Starts the registered Module Activity from any Context in the Host environment. ```kotlin val context: Context = ... // Assume this is your Context context.startActivity(context, HostTestActivity::class.java) ``` -------------------------------- ### build.gradle.kts namespace example Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/xposed-using.md Example of defining the namespace in Kotlin DSL for automatic module package name generation. ```kotlin android { namespace = "com.example.demo" } ``` -------------------------------- ### Find all methods by name Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Uses YukiHookAPI's method finder with name = "b" and .all(instance) to get and execute all methods named 'b'. The example matches two methods: b() and b(String). ```kotlin // Assume this is an instance of this Class val instance = Test() // Call and execute using YukiHookAPI Test::class.java.method { name = "b" }.all(instance).forEach { instance -> // Call and execute each method instance.call(...) } ``` -------------------------------- ### build.gradle namespace example Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/xposed-using.md Example of defining the namespace in Groovy DSL for automatic module package name generation. ```groovy android { namespace 'com.example.demo' } ``` -------------------------------- ### Find all methods with paramCount range 1..3 Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Uses YukiHookAPI's method finder with paramCount(1..3) and .all(instance) to get and execute all methods whose parameter count is between 1 and 3 inclusive. The example matches three methods: doTask(String), release(String, Function<boolean, String>, boolean), and b(String). ```kotlin // Assume this is an instance of this Class val instance = Test() // Call and execute using YukiHookAPI Test::class.java.method { paramCount(1..3) }.all(instance).forEach { instance -> // Call and execute each method instance.call(...) } ``` -------------------------------- ### Example log output format Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/logger.md Shows the printed result format: [TAG][D][host package name] message. ```text [YukiHookAPI][D][host package name] This is a log ``` -------------------------------- ### AndroidManifest.xml Activity tag example Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/host-inject.md Shows an example Activity tag from the Host App's AndroidManifest.xml that can be used as a proxy. ```xml <activity android:name="com.demo.test.activity.TestActivity" ...> ``` -------------------------------- ### Find all methods with custom paramCount condition Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Uses YukiHookAPI's method finder with paramCount { it < 3 } and .all(instance) to get and execute all methods whose parameter count is less than 3. The example matches six methods: init(), doTask(String), stop(String), getName(String), b(), and b(String). ```kotlin // Assume this is an instance of this Class val instance = Test() // Call and execute using YukiHookAPI Test::class.java.method { paramCount { it < 3 } }.all(instance).forEach { instance -> // Call and execute each method instance.call(...) } ``` -------------------------------- ### ModuleApplication.appContext call for IllegalStateException Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-exception.md This example shows calling ModuleApplication.appContext when the app may have been destroyed or not started correctly, leading to IllegalStateException. ```kotlin // This variable is called but the app may have been destroyed or not started correctly ModuleApplication.appContext... ``` -------------------------------- ### replaceTo Example for Resources Hook Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-exception.md Example of using replaceTo to replace a string resource value. This is shown in the context of the 'Failed to got Host Resources' exception. ```kotlin conditions { name = "test" string() } replaceTo { "${it}_some_text" } ``` -------------------------------- ### String.toClass() example for NoClassDefFoundError Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-exception.md This example shows how to use String.toClass() which may throw NoClassDefFoundError if the class is not found in the current ClassLoader. ```kotlin "com.demo.Test".toClass() ``` -------------------------------- ### Loading Hooker with loadApp in special case Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-example.md Example of loading a child Hooker with loadApp when dealing with multiple package names. ```kotlin encase { // Assume this is the app package name and child Hooker you need to load loadApp("com.example.demo", YourCustomHooker) } ``` -------------------------------- ### Resources Hook Example Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/guide/example.md Shows how to hook resources using resources().hook with injectResource, conditions, and replaceTo. Note: this will be discontinued in version 2.0.0. ```kotlin // Resources Hook (2.0.0 will be discontinued) resources().hook { injectResource { conditions { // Your code here. } replaceTo(...) } } ``` -------------------------------- ### AndroidManifest.xml namespace example Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/xposed-using.md Example of defining the package in AndroidManifest.xml for automatic module package name generation. ```xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.demo"> ``` -------------------------------- ### Incorrect Usage: Direct Hook in encase Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-example.md Error example showing that you cannot start a Hook directly inside encase without using loadApp, loadSystem, or loadZygote. This will cause multiple executions and errors. ```kotlin encase { // Wrong usage, can't start Hook directly "com.example.demo.DemoClass".toClass() .resolve() .firstMethod { // Your code here. }.hook { // Your code here. } // Wrong usage, can't start Hook directly resources().hook { // ... } } ``` -------------------------------- ### Rovo89 Xposed API Hook Example Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/guide/home.md This snippet shows the traditional Xposed API approach to achieve the same hooking functionality as the YukiHookAPI example. It implements 'IXposedHookZygoteInit' and 'IXposedHookLoadPackage' and uses 'XposedHelpers.findAndHookMethod' to hook the 'onCreate' method. ```kotlin class HookEntry : IXposedHookZygoteInit, IXposedHookLoadPackage { private lateinit var moduleResources: XModuleResources override fun initZygote(sparam: IXposedHookZygoteInit.StartupParam) { moduleResources = XModuleResources.createInstance(sparam.modulePath, null) XposedHelpers.findAndHookMethod( Activity::class.java.name, null, "onCreate", Bundle::class.java, object : XC_MethodHook() { override fun beforeHookedMethod(param: MethodHookParam?) { // Your code here. } override fun afterHookedMethod(param: MethodHookParam?) { // Your code here. } }) } override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam) { if (lpparam.packageName == "com.android.browser") XposedHelpers.findAndHookMethod( Activity::class.java.name, lpparam.classLoader, "onCreate", Bundle::class.java, object : XC_MethodHook() { override fun beforeHookedMethod(param: MethodHookParam?) { // Your code here. } override fun afterHookedMethod(param: MethodHookParam?) { // Your code here. } }) } } ``` -------------------------------- ### YukiHookAPI constructor finder with parameter Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Shows how to use YukiHookAPI's constructor finder to get a constructor with a Boolean parameter and create a new instance. ```kotlin Test::class.java.constructor { param(BooleanType) }.get().call(true) // Can create a new instance ``` -------------------------------- ### No Find Conditions: Get All Members Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Shows how to get all members using all(...) or giveAll() when no find conditions are set. ```kotlin Test::class.java.field().all(...) Test::class.java.method().giveAll() ``` -------------------------------- ### YukiHookAPI constructor finder without parameters Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Shows how to use YukiHookAPI's constructor finder to get the no-argument constructor and create a new instance. ```kotlin Test::class.java.constructor().get().call() // Create a new instance ``` -------------------------------- ### New API hook example Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/move-to-api-1-2-x.md New API example for hooking the 'test' method in 'com.example.Test' class. Uses toClass() and method().hook with before and after blocks. The Hook object is migrated from Class to Member. ```kotlin "com.example.Test".toClass() .method { name = "test" }.hook { before { // Your code here. } after { // Your code here. } } ``` -------------------------------- ### Version A Class Definition Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Example class ATest with a static doTask method, representing one version of the host app. ```java public class ATest { public static void doTask() { // ... } } ``` -------------------------------- ### KavaRef Member Hook Example Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/guide/example.md Shows the current recommended way to hook a member using KavaRef: resolve the target class, select a method, and define before/after hooks. ```kotlin // KavaRef TargetClass.resolve().firstMethod { // Your code here. }.hook { before { // Your code here. } after { // Your code here. } } ``` -------------------------------- ### YukiHookAPI field finder to get isTaskRunning field Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Shows how to use YukiHookAPI's field finder to retrieve the isTaskRunning field and get its value as any type. ```kotlin // Assume this is an instance of this Class val instance = Test() // Call and execute using YukiHookAPI Test::class.java.field { name = "isTaskRunning" type = BooleanType }.get(instance).any() // Any instantiates an object of any type of Field ``` -------------------------------- ### HostTestActivity extending BaseActivity Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/host-inject.md Example Activity that inherits from BaseActivity and loads a layout using the Module App's resources. ```kotlin class HostTestActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Module App's Resources have been injected automatically // You can directly use xml to load the layout setContentView(R.layout.activity_main) } } ``` -------------------------------- ### Custom Hooker singleton example Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-example.md Recommended way to create a child Hooker using a singleton object. Override onHook to write hook code directly. ```kotlin object CustomHooker : YukiBaseHooker() { override fun onHook() { // Your code here. } } ``` -------------------------------- ### Example exception stack output from YLog.error Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/logger.md Shows the exception stack trace that YLog.error prints when an exception is provided. ```text java.lang.Throwable at com.demo.Test.<init>(...) at com.demo.Test.doTask(...) at com.demo.Test.stop(...) at com.demo.Test.init(...) at a.a.a(...) ... 3 more ``` -------------------------------- ### Invalid proxy parameter example for registerModuleAppActivities Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-exception.md Shows an example of filling in an invalid proxy parameter (false) that is not allowed. The proxy parameter only accepts String, CharSequence, or Class types. ```kotlin // The content filled in here is just an example // And the proxy is filled with invalid parameters that cannot be understood registerModuleAppActivities(proxy = false) ``` -------------------------------- ### Legacy API hook example Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/move-to-api-1-2-x.md Legacy API example for hooking the 'test' method in 'com.example.Test' class. Uses findClass and injectMember with beforeHook and afterHook. Marked as LegacyHookApi, can suppress warning with @OptIn(LegacyHookApi::class). ```kotlin findClass("com.example.Test").hook { injectMember { method { name = "test" } beforeHook { // Your code here. } afterHook { // Your code here. } } } ``` -------------------------------- ### Resources Hook Conditions Example Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-exception.md Example showing two cases of setting conditions for a Resources hook: one with no conditions and one missing the type condition. This is shown in the context of the 'Resources Hook condition name/type cannot be empty' exception. ```kotlin // Case 1 conditions { // No conditions are set here } // Case 2 conditions { name = "test" // The type condition is missing here } ``` -------------------------------- ### Get instance from Field and call methods Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Use field { ... }.current { ... } or field { ... }.current()?.method to get the instance where the field is located and call methods on it. Two plans are shown. ```kotlin // Assume this is an instance of this Class val instance = Test() // Assume this Class is not directly available instance.current { // <Plan 1> field { name = "baseInstance" }.current { method { name = "doBaseTask" param(StringClass) }.call("task_name") } // <Plan 2> field { name = "baseInstance" }.current() ?.method { name = "doBaseTask" param(StringClass) }?.call("task_name") } ``` -------------------------------- ### Correct Usage: Scope Loading in encase Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-example.md Correct example showing how to use loadZygote and loadApp to load Hookers and create Hooks within the proper scope. This prevents multiple executions. ```kotlin encase { // ✅ Correct usage, load in Zygote loadZygote(CustomHooker) // ✅ Correct usage, load in Zygote loadZygote { // ✅ Correct usage, Hook in Zygote resources().hook { // ... } } // ✅ The correct way to use it, use the app scope to load loadApp(/** name parameter optional */, hooker = CustomHooker) // ✅ The correct way to use it, load the Hooker after judging the scope of the app loadApp(/** name parameter optional */) { loadHooker(CustomHooker) // ✅ Correct usage, Hook in app scope "com.example.demo.DemoClass".toClass() .resolve() .firstMethod { // Your code here. }.hook { // Your code here. } // ✅ Correct usage, Hook in app scope resources().hook { // ... } } } ``` -------------------------------- ### Example output of YLog.error Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/logger.md Shows the console output format for an error log: [YukiHookAPI][E][host package name] followed by the message. ```text [YukiHookAPI][E][host package name] This is an error ``` -------------------------------- ### Host App: dataChannel wait and put with value Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/xposed-channel.md Example for the Host App to receive data from the Module App using wait and send data using put. No package name is needed. ```kotlin // Get from the Module App dataChannel.wait<String>(key = "key_from_module") { value -> // Your code here. } // Send to the Module App dataChannel.put(key = "key_from_host", value = "I am host") ``` -------------------------------- ### moduleAppResources variable call Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-exception.md Example of calling moduleAppResources in PackageParam.encase, which can throw IllegalStateException if the instance object cannot be obtained. ```kotlin encase { // This variable is called moduleAppResources... } ``` -------------------------------- ### YukiResForwarder resources call Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-exception.md Example of calling moduleAppResources.fwd(...).resources, which can throw IllegalStateException if the resources instance is invalid. ```kotlin // This variable is called moduleAppResources.fwd(...).resources ``` -------------------------------- ### YukiHookAPI: Hook Entry Point with PackageParam Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/guide/move-to-new-api.md Shows the YukiHookAPI way to set up a hook entry point using the `encase` lambda. It provides access to `packageName`, `appInfo`, `systemContext`, `appContext`, and allows hooking a specific app with `loadApp`. Includes examples of member hooking and resource hooking. ```kotlin override fun onHook() = encase { // Get the package name of the current Hook packageName // Get the ApplicationInfo of the current Hook appInfo // Get the system context object systemContext // Get the host Application lifecycle appContext // Hook specified app loadApp(name = "com.demo.test") { // Member Hook "com.demo.test.TestClass".toClass() .resolve() .firstMethod { name = "test" parameters(Boolean::class) }.hook { after { // ... } } // Resources Hook (fixed usage) resources().hook { injectResource { conditions { name = "ic_launcher" mipmap() } replaceToModuleResource(R.mipmap.ic_launcher) } } } } ``` -------------------------------- ### Sample Code 1: YukiHookAPI.encase usage Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-example.md Shows how to create a Hooker using YukiHookAPI.encase with a fully qualified call. ```kotlin YukiHookAPI.encase { loadApp(name = "com.example.demo") { "$packageName.DemoClass".toClass() .resolve() .firstMethod { // Your code here. }.hook { // Your code here. } } } ``` -------------------------------- ### ModuleApplication.appContext call in encase for IllegalStateException Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-exception.md This example shows calling ModuleApplication.appContext inside encase, which may throw IllegalStateException if the app is dead or not started correctly. ```kotlin encase { // This variable is called ModuleApplication.appContext... } ``` -------------------------------- ### VariousClass.toClass() Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Gets the Class object from the list of class names, using the ClassLoader of the (Xposed) Host environment in PackageParam. ```APIDOC ## VariousClass.toClass() ### Description Gets the Class object from the list of class names, using the ClassLoader of the (Xposed) Host environment in PackageParam. ### Method toClass ### Return Value - The matched Class object. ### Example ```kotlin VariousClass("com.demo.ATest", "com.demo.BTest").toClass().method { name = "doTask" emptyParam() }.get().call() ``` ``` -------------------------------- ### ArgsModifyer.set index out of bounds Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-exception.md Example of calling args(index = 6).set(...) when the method parameter index is out of bounds. The subscript starts from 0, so if the original parameter subscript is 5, filling in 6 causes this exception. ```kotlin injectMember { // ... after { // The subscript starts from 0 // Assuming the original parameter subscript is 5, but fill in 6 here args(index = 6).set(...) } } ``` -------------------------------- ### Necessary Find Conditions: Constructor Example Class Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Java class with a parameterless constructor, used to illustrate the need for emptyParam in constructor find conditions. ```java public class TestFoo { public TestFoo() { // ... } } ``` -------------------------------- ### YukiHookAPI method finder to get and invoke doTask method Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Shows the YukiHookAPI syntactic sugar alternative to standard reflection for finding and invoking the doTask method. ```kotlin // Assume this is an instance of this Class val instance = Test() // Call and execute using YukiHookAPI Test::class.java.method { name = "doTask" param(StringClass) }.get(instance).call("task_name") ``` -------------------------------- ### Incorrect Usage: Direct Hook in CustomHooker Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-example.md Error example showing that you cannot start a Hook directly in a CustomHooker's onHook method without a scope judgment. The outer encase lacks a scope, so the Hook would execute multiple times. ```kotlin object HookEntry : IYukiHookXposedInit { override fun onHook() { // <Scenario 1> encase { loadHooker(CustomHooker) } // <Scenario 2> encase(CustomHooker) } } object CustomHooker : YukiBaseHooker() { override fun onHook() { // Wrong method of use // Because there is no judgment object in the outer layer, you cannot start Hook directly "com.example.demo.DemoClass".toClass() .resolve() .firstMethod { // Your code here. }.hook { // Your code here. } } } ``` -------------------------------- ### Example obfuscated class to search Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md A sample obfuscated class that might be the target of a vague search. It shows the structure of a class with obfuscated names and various members. ```java package com.demo; public class a extends Activity implements Serializable { public a(String var1) { // ... } private String a; private String b; private boolean a; protected void onCreate(Bundle var1) { // ... } private static void a(String var1) { // ... } private String a(boolean var1, String var2) { // ... } private void a() { // ... } public void a(boolean var1, a var2, b var3, String var4) { // ... } } ``` -------------------------------- ### Rovo89 Xposed API: Hook Entry Point with LoadPackageParam Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/guide/move-to-new-api.md Shows the traditional Rovo89 Xposed API approach for hooking apps. It requires implementing `initZygote` and `handleLoadPackage`, manually obtaining the system context via reflection, and using `XposedHelpers.findAndHookMethod` for method hooks and `setReplacement` for resource hooks. ```kotlin private lateinit var moduleResources: XModuleResources override fun initZygote(sparam: IXposedHookZygoteInit.StartupParam) { moduleResources = XModuleResources.createInstance(sparam.modulePath, null) } override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam) { // Get the package name of the current Hook lpparam.packageName // Get the ApplicationInfo of the current Hook lpparam.applicationInfo // Get the system context object // There is no ready-made calling method in the Rovo89 Xposed API, you need to reflect ActivityThread to achieve it // Get the host Application lifecycle AndroidAppHelper.currentApplication() // Class Hook if(lpparam.packageName == "com.demo.test") XposedHelpers.findAndHookMethod( "com.demo.test.TestClass", lpparam.classLoader, "test", Boolean::class.java, object : XC_MethodHook() { override fun afterHookedMethod(param: MethodHookParam) { // ... } } ) } override fun handleInitPackageResources(resparam: XC_InitPackageResources.InitPackageResourcesParam) { // Get the package name of the current Hook resparam.packageName // Resources Hook resparam.res.setReplacement( "com.demo.test", "mipmap", "ic_launcher", moduleResources.fwd(R.mipmap.ic_launcher) ) } ``` -------------------------------- ### Standard reflection to get and invoke doTask method Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Shows the standard Java reflection approach to find and invoke the private doTask method on a Test instance. ```kotlin // Assume this is an instance of this Class val instance = Test() // Call and execute using reflection API Test::class.java .getDeclaredMethod("doTask", String::class.java) .apply { isAccessible = true } .invoke(instance, "task_name") ``` -------------------------------- ### Hook App onCreate and onStart methods Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/guide/example.md Hooks both onCreate and onStart methods of Activity in com.android.browser using Kotlin's apply method to create a call space. Add this code inside the encase method. ```kotlin loadApp(name = "com.android.browser") { Activity::class.resolve().apply { firstMethod { name = "onCreate" parameters(Bundle::class) }.hook { after { AlertDialog.Builder(instance()) .setTitle("Hooked") .setMessage("I am hook!") .setPositiveButton("OK", null) .show() } } firstMethod { name = "onStart" emptyParameters() }.hook { after { // Your code here. } } } } ``` -------------------------------- ### Create instance with buildOf and call method Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Use buildOf to create an instance and then call methods on it with current. This is a more concise alternative to standard reflection. ```kotlin "com.demo.Test".toClass().buildOf(true) { param(BooleanType) }?.current { method { name = "doTask" param(StringClass) }.call("task_name") } ``` -------------------------------- ### Deploy dependencies in build.gradle.kts (Version Catalog) Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/guide/quick-start.md Adds YukiHookAPI, KavaRef, Xposed API, and KSP dependencies using version catalog references. Xposed API and KSP are required only for Xposed Module usage. ```kotlin dependencies { // Basic dependency implementation(libs.yukihookapi.api) // It is recommended to use KavaRef as the core reflection API implementation(libs.kavaref.core) implementation(libs.kavaref.extension) // Must be added when used as an Xposed Module, otherwise optional compileOnly(libs.xposed.api) // Must be added when used as an Xposed Module, otherwise optional ksp(libs.yukihookapi.ksp.xposed) } ``` -------------------------------- ### Create Hook Entry Class with @InjectYukiHookWithXposed Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/guide/quick-start.md Define a Kotlin object that implements IYukiHookXposedInit and annotate it with @InjectYukiHookWithXposed. The onHook method uses YukiHookAPI.encase to start writing hook code. ```kotlin @InjectYukiHookWithXposed object HookEntry : IYukiHookXposedInit { override fun onHook() = YukiHookAPI.encase { // Your code here. } } ``` -------------------------------- ### Loading Hookers via YukiHookAPI.encase Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-example.md Loads multiple Hookers from the entry class using YukiHookAPI.encase. ```kotlin object HookEntry : IYukiHookXposedInit { override fun onHook() = YukiHookAPI.encase(FirstHooker, SecondHooker, ThirdHooker ...) } ``` -------------------------------- ### Necessary Find Conditions: Example Class with Overloaded foo Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Java class with two foo methods, one with a String parameter and one without, used to illustrate the need for find conditions. ```java public class TestFoo { public void foo(String string) { // ... } public void foo() { // ... } } ``` -------------------------------- ### Sample Code 2: encase usage without prefix Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-example.md Shows how to create a Hooker using encase without the YukiHookAPI prefix, assuming it is called within the onHook method. ```kotlin encase { loadApp(name = "com.example.demo") { "$packageName.DemoClass".toClass() .resolve() .firstMethod { // Your code here. }.hook { // Your code here. } } } ``` -------------------------------- ### Find method with name, emptyParam, and returnType Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Finds the getName method in Test class with explicit returnType StringClass and gets the result using get(instance).string(). ```kotlin // Assume this is an instance of this Class val instance = Test() // Call and execute using YukiHookAPI Test::class.java.method { name = "getName" emptyParam() returnType = StringClass }.get(instance).string() // Get the result of the method ``` -------------------------------- ### YukiHookAPI classOf extension to get a Class by mapping Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Uses the classOf extension to get an existing Class object directly, optionally with a custom ClassLoader. Effective for stubs. ```kotlin // Assume this Class can be obtained directly var instance = classOf<Test>() // We can also customize the ClassLoader where the Class is located, which is very effective for stubs val customClassLoader: ClassLoader? = ... // Assume this is your ClassLoader var instance = classOf<Test>(customClassLoader) ``` -------------------------------- ### Deploy plugins in build.gradle.kts (Version Catalog) Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/guide/quick-start.md Applies the KSP plugin using the version catalog alias. Required only when using YukiHookAPI as an Xposed Module. ```kotlin plugins { // Must be added when used as an Xposed Module, otherwise optional alias(libs.plugins.ksp) } ``` -------------------------------- ### YukiHookAPI toClass extension to get a Class Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Uses the toClass extension to get a Class directly from a string, optionally with a custom ClassLoader. Throws an exception if the Class does not exist. ```kotlin // Get this Class directly // If you are currently in the PackageParam environment, then you don't need to consider ClassLoader var instance = "com.demo.Test".toClass() // ClassLoader where the custom Class is located val customClassLoader: ClassLoader? = ... // Assume this is your ClassLoader var instance = "com.demo.Test".toClass(customClassLoader) ``` -------------------------------- ### No Find Conditions: Get First Member Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Shows how using field() or method() without find conditions and then get(...) or give() returns only the first member in bytecode order. ```kotlin Test::class.java.field().get(...) Test::class.java.method().give() ``` -------------------------------- ### Loading child Hooker via loadApp Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-example.md Shows how to load a child Hooker from the entry class using loadApp with the child Hooker as parameter. ```kotlin object HookEntry : IYukiHookXposedInit { override fun onHook() = encase { loadApp(name = "com.example.demo", ChildCustomHooker) } } object ChildCustomHooker : YukiBaseHooker() { override fun onHook() { "$packageName.DemoClass".toClass() .resolve() .firstMethod { // Your code here. }.hook { // Your code here. } } } ``` -------------------------------- ### Add plugins in build.gradle.kts (Traditional Method) Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/guide/quick-start.md Applies the KSP plugin with a version placeholder. Required only when using YukiHookAPI as an Xposed Module. ```kotlin plugins { // Must be added when used as an Xposed Module, otherwise optional id("com.google.devtools.ksp") version "<ksp-version>" } ``` -------------------------------- ### Use RemedyPlan with wait to find doTask Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Uses RemedyPlan to find the doTask method across different versions, with wait to get the result. Note: cannot use get with RemedyPlan. ```kotlin // Assume this is an instance of this Class val instance = Test() // Call and execute using YukiHookAPI Test::class.java.method { name = "doTask" emptyParam() }.remedys { method { name = "doTask" param(StringClass) }.onFind { // Found logic can be implemented here } method { name = "doTask" param(StringClass, IntType) }.onFind { // Found logic can be implemented here } }.wait(instance) { // Get the result of the method } ``` -------------------------------- ### Hooked method return type mismatch example Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-exception.md Example showing incorrect return type usage in hook callbacks, leading to 'Hooked method return type match failed' error. ```java private boolean test() ``` ```kotlin method { name = "test" emptyParam() }.hook { // <Scenario 1> Set the wrong type, the original type is Boolean before { result = 0 } // <Scenario 2> Return the wrong type, the original type is Boolean replaceAny { 0 } // <Scenario 3> Use the wrong type directly, the original type is Boolean replaceTo(any = 0) } ``` -------------------------------- ### Standard reflection to create instance and call method Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Shows the standard Java reflection API to create an instance of a class and invoke a method. This is presented as a more verbose alternative to buildOf. ```kotlin "com.demo.Test".toClass() .getDeclaredConstructor(Boolean::class.java) .apply { isAccessible = true } .newInstance(true) .apply { javaClass .getDeclaredMethod("doTask", String::class.java) .apply { isAccessible = true } .invoke(this, "task_name") } ``` -------------------------------- ### YukiHookAPI toClassOrNull extension to safely get a Class Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Uses the toClassOrNull extension to get a Class without throwing an exception if it does not exist; returns null instead. Supports an optional custom ClassLoader. ```kotlin // Get this Class directly // If you are currently in the PackageParam environment, then you don't need to consider ClassLoader // If not available, the result will be null but no exception will be thrown var instance = "com.demo.Test".toClassOrNull() // ClassLoader where the custom Class is located val customClassLoader: ClassLoader? = ... // Assume this is your ClassLoader // If not available, the result will be null but no exception will be thrown var instance = "com.demo.Test".toClassOrNull(customClassLoader) ``` -------------------------------- ### Define versions and libraries in gradle/libs.versions.toml Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/guide/quick-start.md Defines version catalog entries for YukiHookAPI, KSP, KavaRef, and Xposed API. The KSP plugin and Xposed API are required only for Xposed Module usage. YukiHookAPI 1.3.0 uses KavaRef as core reflection API. ```toml [versions] yukihookapi = "<yuki-version>" ksp = "<ksp-version>" kavaref-core = "<kavaref-version>" kavaref-extension = "<kavaref-version>" [plugins] # Must be added when used as an Xposed Module, otherwise optional ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" } [libraries] yukihookapi-api = { module = "com.highcapable.yukihookapi:api", version.ref = "yukihookapi" } # Must be added when used as an Xposed Module, otherwise optional yukihookapi-ksp-xposed = { module = "com.highcapable.yukihookapi:ksp-xposed", version.ref = "yukihookapi" } # YukiHookAPI version 1.3.0 uses KavaRef as core reflection API # YukiHookAPI no longer binds its own reflection API, you can start trying to use KavaRef kavaref-core = { module = "com.highcapable.kavaref:kavaref-core", version.ref = "kavaref-core" } kavaref-extension = { module = "com.highcapable.kavaref:kavaref-extension", version.ref = "kavaref-extension" } # Must be added when used as an Xposed Module, otherwise optional xposed-api = { module = "de.robv.android.xposed:api", version = "82" } ``` -------------------------------- ### MyApplication with YukiHookAPI.encase Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-using.md Create a custom Application class and add YukiHookAPI.encase to attachBaseContext. Configure YukiHookAPI via YukiHookApi.configs and load the API with encase. Note that you cannot wrap with loadApp and must write Hook code directly. ```kotlin class MyApplication : Application() { override fun attachBaseContext(base: Context?) { // Load Hook Framework // // Your code here. // // Configure YukiHookAPI YukiHookApi.configs { // Your code here. } // Load YukiHookAPI YukiHookAPI.encase(base) { // Your code here. } super.attachBaseContext(base) } } ``` -------------------------------- ### Multiple search with all and waitAll Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Searches a set of classes with a fixed condition. Use `all` for synchronous search to get all results, either via `forEach` or an iteration block. Use `waitAll` for asynchronous search to get all results in a collection. ```kotlin // Synchronous search, use all to get all the results found by the conditions searchClass { // ... }.all().forEach { clazz -> // Get each result } // Synchronous search, using all { ... } to iterate over each result searchClass { // ... }.all { clazz -> // Get each result } // Asynchronous search, use waitAll to get all the results found by the conditions searchClass(async = true) { // ... }.waitAll { classes -> classes.forEach { // Get each result } } ``` -------------------------------- ### YukiHookAPI.Status.Executor.name Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/guide/example.md Gets the name of the current Hook Framework. ```APIDOC ## YukiHookAPI.Status.Executor.name ### Description Gets the name of the current Hook Framework. ### Method Property (String) ### Usage ```kotlin val frameworkName = YukiHookAPI.Status.Executor.name ``` ``` -------------------------------- ### YukiHookAPI.Status.Executor.apiLevel Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/guide/example.md Gets the API Level of the current Hook Framework. ```APIDOC ## YukiHookAPI.Status.Executor.apiLevel ### Description Gets the API Level of the current Hook Framework. ### Method Property (Int) ### Usage ```kotlin val frameworkApiLevel = YukiHookAPI.Status.Executor.apiLevel ``` ``` -------------------------------- ### Correct usage of encase in onHook Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-exception.md Shows the correct way to load the encase method inside the onHook method, while only the configs method can be loaded in onInit. This avoids the exception and is the recommended pattern. ```kotlin object HookEntry : IYukiHookXposedInit { override fun onInit() { // Only the configs method can be loaded here YukiHookAPI.configs { // Your code here. } } override fun onHook() { // ✅ Correct usage YukiHookAPI.encase { // Your code here. } } } ``` -------------------------------- ### classOf Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Gets an existing Class object by mapping, with an optional custom ClassLoader. ```APIDOC ## classOf ### Description Gets an existing Class object by mapping, with an optional custom ClassLoader. ### Method Top-level Function ### Signature `fun <T> classOf(loader: ClassLoader? = null): Class<T>` ### Parameters - **loader** (ClassLoader?) - Optional - The ClassLoader to use. If null, the default ClassLoader is used. ### Request Example ```kotlin // Assume this Class can be obtained directly var instance = classOf<Test>() // We can also customize the ClassLoader where the Class is located, which is very effective for stubs val customClassLoader: ClassLoader? = ... // Assume this is your ClassLoader var instance = classOf<Test>(customClassLoader) ``` ### Response #### Success Response Returns the `Class` object for the specified type. ``` -------------------------------- ### Get Hook Framework name with YukiHookAPI.Status.Executor.name Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/guide/example.md Retrieves the name of the current Hook Framework. ```kotlin val frameworkName = YukiHookAPI.Status.Executor.name ``` -------------------------------- ### encase(baseContext: Context?, initiate: PackageParam.() -> Unit) Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-example.md Entry point for Hook API usage with a baseContext parameter and an initiate lambda for configuring the hook. ```APIDOC ## encase(baseContext: Context?, initiate: PackageParam.() -> Unit) ### Description Entry point for using YukiHookAPI as a Hook API. This method requires a baseContext parameter and an initiate lambda for configuring the hook. ### Method Function call ### Signature ```kotlin fun encase(baseContext: Context?, initiate: PackageParam.() -> Unit) ``` ### Parameters - **baseContext** (Context?) - Required - The Context obtained from attachBaseContext. - **initiate** (PackageParam.() -> Unit) - Required - Lambda for configuring the hook. ### Usage Example ```kotlin encase(baseContext) { // hook configuration } ``` ### Notes - Never use the encase method without the baseContext parameter in Xposed mode, as it will cause the hook to not work. - Resources Hook feature is not supported as Hook API. ``` -------------------------------- ### VariousClass with Custom ClassLoader Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Pass a custom ClassLoader to get() when the class exists in a specific ClassLoader. ```kotlin val customClassLoader: ClassLoader? = ... // Assume this is your ClassLoader VariousClass("com.demo.ATest", "com.demo.BTest").get(customClassLoader).method { name = "doTask" emptyParam() }.get().call() ``` -------------------------------- ### Get all log file contents with YLog.contents Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/logger.md Retrieves the contents of all log files that have been printed so far. ```kotlin // Get the contents of all log files that have been printed so far val fileContent = YLog.contents ``` -------------------------------- ### Module App: dataChannel wait and put with value Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/xposed-channel.md Example for the Module App to receive data from the Host App using wait and send data using put. The package name of the Host App must be specified. ```kotlin // Get from the Host App of the specified package name dataChannel(packageName = "com.example.demo").wait<String>(key = "key_from_host") { value -> // Your code here. } // Send to the Host App with the specified package name dataChannel(packageName = "com.example.demo").put(key = "key_from_module", value = "I am module") ``` -------------------------------- ### IllegalStateException: The Host App's Context has not yet initialized successfully Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-exception.md Shows the abnormal usage of prefs.native() in the Host environment before the Host App's lifecycle is initialized. ```kotlin encase { // This method was called prefs.native() } ``` -------------------------------- ### Get Hook Framework API level with YukiHookAPI.Status.Executor.apiLevel Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/guide/example.md Retrieves the API level of the current Hook Framework. ```kotlin val frameworkApiLevel = YukiHookAPI.Status.Executor.apiLevel ``` -------------------------------- ### BaseTest class definition Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Example class used to illustrate member extensions. Contains constructors and a private method. ```java package com.demo; public class BaseTest { public BaseTest() { // ... } public BaseTest(boolean isInit) { // ... } private void doBaseTask(String taskName) { // ... } } ``` -------------------------------- ### Register Module App Activities with Default Parameters Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/config/api-exception.md Use this code to register module app activities with default parameters. This works only for apps that can be launched normally. If the app does not declare a startup entry Activity, you must specify the proxy parameter manually. ```kotlin // Register directly with default parameters registerModuleAppActivities() ``` -------------------------------- ### Add dependencies in build.gradle.kts (Traditional Method) Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/guide/quick-start.md Adds YukiHookAPI, KavaRef, Xposed API, and KSP dependencies with explicit coordinates and version placeholders. Xposed API and KSP are required only for Xposed Module usage. ```kotlin dependencies { // Basic dependency implementation("com.highcapable.yukihookapi:api:<yuki-version>") // It is recommended to use KavaRef as the core reflection API implementation("com.highcapable.kavaref:kavaref-core:<kavaref-version>") implementation("com.highcapable.kavaref:kavaref-extension:<kavaref-version>") // Must be added when used as an Xposed Module, otherwise optional compileOnly("de.robv.android.xposed:api:82") // Must be added when used as an Xposed Module, otherwise optional ksp("com.highcapable.yukihookapi:ksp-xposed:<yuki-version>") } ``` -------------------------------- ### Configure Debug Mode in onInit Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/guide/quick-start.md Override onInit to configure YukiHookAPI. Set isDebug to BuildConfig.DEBUG to enable debug mode only in debug builds. This is recommended for Xposed Module usage. ```kotlin override fun onInit() = configs { isDebug = BuildConfig.DEBUG } ``` -------------------------------- ### Use RemedyPlan with waitAll and paramCount Source: https://github.com/highcapable/yukihookapi/blob/master/docs-source/src/en/special-features/reflection.md Uses RemedyPlan with paramCount ranges and waitAll to find doTask across versions, supporting multiple find. ```kotlin // Assume this is an instance of this Class val instance = Test() // Call and execute using YukiHookAPI Test::class.java.method { name = "doTask" emptyParam() }.remedys { method { name = "doTask" paramCount(0..1) }.onFind { // Found logic can be implemented here } method { name = "doTask" paramCount(1..2) }.onFind { // Found logic can be implemented here } }.waitAll(instance) { // Get the result of the method } ```