feat: update demo

This commit is contained in:
2023-10-03 04:17:35 +08:00
parent 6e658baf17
commit 8c8cfc5498
2 changed files with 81 additions and 88 deletions

View File

@@ -41,7 +41,10 @@ import com.highcapable.yukihookapi.demo_module.data.DataConst
import com.highcapable.yukihookapi.demo_module.hook.factory.compatStyle import com.highcapable.yukihookapi.demo_module.hook.factory.compatStyle
import com.highcapable.yukihookapi.demo_module.ui.MainActivity import com.highcapable.yukihookapi.demo_module.ui.MainActivity
import com.highcapable.yukihookapi.hook.factory.applyModuleTheme import com.highcapable.yukihookapi.hook.factory.applyModuleTheme
import com.highcapable.yukihookapi.hook.factory.constructor
import com.highcapable.yukihookapi.hook.factory.method
import com.highcapable.yukihookapi.hook.factory.registerModuleAppActivities import com.highcapable.yukihookapi.hook.factory.registerModuleAppActivities
import com.highcapable.yukihookapi.hook.param.annotation.LegacyResourcesHook
import com.highcapable.yukihookapi.hook.type.android.ActivityClass import com.highcapable.yukihookapi.hook.type.android.ActivityClass
import com.highcapable.yukihookapi.hook.type.android.BundleClass import com.highcapable.yukihookapi.hook.type.android.BundleClass
import com.highcapable.yukihookapi.hook.type.java.StringArrayClass import com.highcapable.yukihookapi.hook.type.java.StringArrayClass
@@ -50,7 +53,7 @@ import com.highcapable.yukihookapi.hook.type.java.UnitType
import com.highcapable.yukihookapi.hook.xposed.bridge.event.YukiXposedEvent import com.highcapable.yukihookapi.hook.xposed.bridge.event.YukiXposedEvent
import com.highcapable.yukihookapi.hook.xposed.proxy.IYukiHookXposedInit import com.highcapable.yukihookapi.hook.xposed.proxy.IYukiHookXposedInit
@InjectYukiHookWithXposed @InjectYukiHookWithXposed(isUsingResourcesHook = true)
object HookEntry : IYukiHookXposedInit { object HookEntry : IYukiHookXposedInit {
override fun onInit() { override fun onInit() {
@@ -150,6 +153,7 @@ object HookEntry : IYukiHookXposedInit {
} }
} }
@LegacyResourcesHook
override fun onHook() { override fun onHook() {
// Start your hook // Start your hook
// Can be shortened to encase {} // Can be shortened to encase {}
@@ -161,19 +165,16 @@ object HookEntry : IYukiHookXposedInit {
loadZygote { loadZygote {
// Find Class to hook // Find Class to hook
// 得到需要 Hook 的 Class // 得到需要 Hook 的 Class
ActivityClass.hook { ActivityClass.method {
injectMember {
method {
name = "onCreate" name = "onCreate"
param(BundleClass) param(BundleClass)
} }.hook {
afterHook { after {
// Add text after the [Activity] title // Add text after the [Activity] title
// 在 [Activity] 标题后方加入文字 // 在 [Activity] 标题后方加入文字
instance<Activity>().apply { title = "$title [Active]" } instance<Activity>().apply { title = "$title [Active]" }
} }
} }
}
// Find Resources to hook // Find Resources to hook
// Requires Hook Framework to support Resources Hook to succeed // Requires Hook Framework to support Resources Hook to succeed
// Resources Hook in Zygote only needs Hook Framework support, no need to enable this feature // Resources Hook in Zygote only needs Hook Framework support, no need to enable this feature
@@ -208,29 +209,27 @@ object HookEntry : IYukiHookXposedInit {
} }
// Find Class to hook // Find Class to hook
// 得到需要 Hook 的 Class // 得到需要 Hook 的 Class
"$packageName.ui.MainActivity".toClass().hook { "$packageName.ui.MainActivity".toClass().apply {
// Inject the method to be hooked // Inject the method to be hooked
// 注入要 Hook 的方法 // 注入要 Hook 的方法
injectMember {
method { method {
name = "getFirstText" name = "getFirstText"
emptyParam() emptyParam()
returnType = StringClass returnType = StringClass
} }.hook {
// Replaced hook // Replaced hook
// 执行替换 Hook // 执行替换 Hook
replaceTo(any = "Hello YukiHookAPI!") replaceTo(any = "Hello YukiHookAPI!")
} }
// Inject the method to be hooked // Inject the method to be hooked
// 注入要 Hook 的方法 // 注入要 Hook 的方法
injectMember {
method { method {
name = "onCreate" name = "onCreate"
param(BundleClass) param(BundleClass)
} }.hook {
// Before hook the method // Before hook the method
// 在方法执行之前拦截 // 在方法执行之前拦截
beforeHook { before {
field { field {
name = "secondText" name = "secondText"
type = StringClass type = StringClass
@@ -238,7 +237,7 @@ object HookEntry : IYukiHookXposedInit {
} }
// After hook the method // After hook the method
// 在执行方法之后拦截 // 在执行方法之后拦截
afterHook { after {
if (prefs.getBoolean("show_dialog_when_demo_app_opend")) if (prefs.getBoolean("show_dialog_when_demo_app_opend"))
MaterialAlertDialogBuilder(instance<Activity>().applyModuleTheme(R.style.Theme_Default)) MaterialAlertDialogBuilder(instance<Activity>().applyModuleTheme(R.style.Theme_Default))
.setTitle("Hooked") .setTitle("Hooked")
@@ -254,15 +253,14 @@ object HookEntry : IYukiHookXposedInit {
} }
// Inject the method to be hooked // Inject the method to be hooked
// 注入要 Hook 的方法 // 注入要 Hook 的方法
injectMember {
method { method {
name = "getRegularText" name = "getRegularText"
param(StringClass) param(StringClass)
returnType = StringClass returnType = StringClass
} }.hook {
// Before hook the method // Before hook the method
// 在方法执行之前拦截 // 在方法执行之前拦截
beforeHook { before {
// Set the 0th param (recomment) // Set the 0th param (recomment)
// 设置 0 号 param (推荐) // 设置 0 号 param (推荐)
args().first().set("I am hook method param") args().first().set("I am hook method param")
@@ -273,15 +271,14 @@ object HookEntry : IYukiHookXposedInit {
} }
// Inject the method to be hooked // Inject the method to be hooked
// 注入要 Hook 的方法 // 注入要 Hook 的方法
injectMember {
method { method {
name = "getArray" name = "getArray"
param(StringArrayClass) param(StringArrayClass)
returnType = StringArrayClass returnType = StringArrayClass
} }.hook {
// Before hook the method // Before hook the method
// 在方法执行之前拦截 // 在方法执行之前拦截
beforeHook { before {
// Set the 0th param // Set the 0th param
// 设置 0 号 param // 设置 0 号 param
args().first().array<String>()[0] = "peach" args().first().array<String>()[0] = "peach"
@@ -289,12 +286,11 @@ object HookEntry : IYukiHookXposedInit {
} }
// Inject the method to be hooked // Inject the method to be hooked
// 注入要 Hook 的方法 // 注入要 Hook 的方法
injectMember {
method { method {
name = "toast" name = "toast"
emptyParam() emptyParam()
returnType = UnitType returnType = UnitType
} }.hook {
// Intercept the entire method // Intercept the entire method
// 拦截整个方法 // 拦截整个方法
replaceUnit { replaceUnit {
@@ -322,12 +318,11 @@ object HookEntry : IYukiHookXposedInit {
} }
// Inject the method to be hooked // Inject the method to be hooked
// 注入要 Hook 的方法 // 注入要 Hook 的方法
injectMember {
method { method {
name = "getDataText" name = "getDataText"
emptyParam() emptyParam()
returnType = StringClass returnType = StringClass
} }.hook {
// Replaced hook // Replaced hook
// 执行替换 Hook // 执行替换 Hook
replaceTo(prefs.get(DataConst.TEST_KV_DATA)) replaceTo(prefs.get(DataConst.TEST_KV_DATA))
@@ -335,14 +330,13 @@ object HookEntry : IYukiHookXposedInit {
} }
// Find Class to hook // Find Class to hook
// 得到需要 Hook 的 Class // 得到需要 Hook 的 Class
"$packageName.test.Main".toClass().hook { "$packageName.test.Main".toClass().apply {
// Inject the method to be hooked // Inject the method to be hooked
// 注入要 Hook 的方法 // 注入要 Hook 的方法
injectMember { constructor { param(StringClass) }.hook {
constructor { param(StringClass) }
// Before hook the method // Before hook the method
// 在方法执行之前拦截 // 在方法执行之前拦截
beforeHook { before {
// Set the 0th param // Set the 0th param
// 设置 0 号 param // 设置 0 号 param
args().first().set("I am hook constructor param") args().first().set("I am hook constructor param")
@@ -350,7 +344,6 @@ object HookEntry : IYukiHookXposedInit {
} }
// Inject the method to be hooked // Inject the method to be hooked
// 注入要 Hook 的方法 // 注入要 Hook 的方法
injectMember {
method { method {
name = "getSuperString" name = "getSuperString"
emptyParam() emptyParam()
@@ -364,23 +357,25 @@ object HookEntry : IYukiHookXposedInit {
// 由于演示的方法只会在父类存在 - 所以可以设置仅查找父类 isOnlySuperClass = true 节省时间 // 由于演示的方法只会在父类存在 - 所以可以设置仅查找父类 isOnlySuperClass = true 节省时间
// 如果想继续尝试查找当前 Class - 请删除 isOnlySuperClass = true // 如果想继续尝试查找当前 Class - 请删除 isOnlySuperClass = true
superClass(isOnlySuperClass = true) superClass(isOnlySuperClass = true)
} }.hook {
// Replaced hook // Replaced hook
// 执行替换 Hook // 执行替换 Hook
replaceTo(any = "I am hook super class method") replaceTo(any = "I am hook super class method")
} }
// Inject the method to be hooked // Inject the method to be hooked
// 注入要 Hook 的方法 // 注入要 Hook 的方法
injectMember { method {
method { name = "getTestResultFirst" }.all() name = "getTestResultFirst"
}.hookAll {
// Replaced hook // Replaced hook
// 执行替换 Hook // 执行替换 Hook
replaceTo(any = "I am hook all methods first") replaceTo(any = "I am hook all methods first")
} }
// Inject the method to be hooked // Inject the method to be hooked
// 注入要 Hook 的方法 // 注入要 Hook 的方法
injectMember { method {
method { name = "getTestResultLast" }.all() name = "getTestResultLast"
}.hookAll {
// Replaced hook // Replaced hook
// 执行替换 Hook // 执行替换 Hook
replaceTo(any = "I am hook all methods last") replaceTo(any = "I am hook all methods last")

View File

@@ -31,6 +31,7 @@ import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import com.highcapable.yukihookapi.YukiHookAPI; import com.highcapable.yukihookapi.YukiHookAPI;
import com.highcapable.yukihookapi.hook.factory.ReflectionFactoryKt;
import com.highcapable.yukihookapi.hook.log.YLog; import com.highcapable.yukihookapi.hook.log.YLog;
import com.highcapable.yukihookapi.hook.xposed.bridge.event.YukiXposedEvent; import com.highcapable.yukihookapi.hook.xposed.bridge.event.YukiXposedEvent;
import com.highcapable.yukihookapi.hook.xposed.proxy.IYukiHookXposedInit; import com.highcapable.yukihookapi.hook.xposed.proxy.IYukiHookXposedInit;
@@ -47,7 +48,7 @@ import kotlin.Unit;
// 建议还是使用 Kotlin 来完成 Hook 部分的编写 // 建议还是使用 Kotlin 来完成 Hook 部分的编写
// 请删除下方的注释 "//" 以使用此 Demo - 但要确保注释掉 Kotlin 一边的 HookEntry 的注解 // 请删除下方的注释 "//" 以使用此 Demo - 但要确保注释掉 Kotlin 一边的 HookEntry 的注解
// ======== // ========
// @InjectYukiHookWithXposed // @InjectYukiHookWithXposed(isUsingResourcesHook = true)
public class HookEntry implements IYukiHookXposedInit { public class HookEntry implements IYukiHookXposedInit {
@Override @Override
@@ -77,18 +78,15 @@ public class HookEntry implements IYukiHookXposedInit {
// 在 Java 中调用 Kotlin 的 lambda 在 Unit 情况下也需要 return Unit.INSTANCE // 在 Java 中调用 Kotlin 的 lambda 在 Unit 情况下也需要 return Unit.INSTANCE
YukiHookAPI.INSTANCE.encase(e -> { YukiHookAPI.INSTANCE.encase(e -> {
e.loadZygote(l -> { e.loadZygote(l -> {
l.hook(Activity.class, false, h -> { var result = ReflectionFactoryKt.method(Activity.class, m -> {
h.injectMember(h.getPRIORITY_DEFAULT(), "Default", i -> {
i.method(m -> {
m.setName("onCreate"); m.setName("onCreate");
m.param(Bundle.class); m.param(Bundle.class);
return null;
});
i.afterHook(a -> {
Activity instance = ((Activity) a.getInstance());
instance.setTitle(instance.getTitle() + " [Active]");
return Unit.INSTANCE; return Unit.INSTANCE;
}); });
l.hook(result, h -> {
h.after(a -> {
Activity instance = ((Activity) a.getInstance());
instance.setTitle(instance.getTitle() + " [Active]");
return Unit.INSTANCE; return Unit.INSTANCE;
}); });
return Unit.INSTANCE; return Unit.INSTANCE;