Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请教个问题,怎么hook Activity的onCreate函数? #120

Open
jambooid opened this issue Sep 4, 2024 · 0 comments
Open

请教个问题,怎么hook Activity的onCreate函数? #120

jambooid opened this issue Sep 4, 2024 · 0 comments

Comments

@jambooid
Copy link

jambooid commented Sep 4, 2024

我参照这ServiceHookTransform写了一个ActivityHookTransform,但是没有hook到,反编译activity发现也没有插入自定义的代码
test

class ActivityHookTransform: BaseHookTransform() {
    override fun ignoreClass(context: TransformContext, klass: ClassNode): Boolean {
        var ignore = super.ignoreClass(context, klass)
        if (klass.isInterface) {
            return true
        }
        if (!ignore) {
            ignore = !PrivacyPluginUtil.privacyPluginUtil.isActivity(klass.formatName(), klass.formatSuperName())
        }
        return ignore

    }

    override fun transform(
        project: Project,
        privacyExtension: PrivacyExtension,
        context: TransformContext,
        klass: ClassNode
    ): ClassNode {
        println("onCreate tranform"+klass.formatName())
        HookedDataManger.MANAGER.addHookService(klass.formatName())
        val onStartCommandMethod = klass.methods.find { it.name == "onCreate" }
        if(onStartCommandMethod!=null){
            println("onCreate tranform"+klass.formatName())
        }

        onStartCommandMethod?.instructions?.iterator()?.asSequence()?.forEach {
            if (it is InsnNode) {
                if (it.opcode == Opcodes.RETURN || it.opcode == Opcodes.IRETURN) {

                    val newInstructions = InsnList()
                    newInstructions.add(LdcInsnNode("xxxxx") )
                    newInstructions.add(LdcInsnNode("onCreate end======") )
                    newInstructions.add(MethodInsnNode(Opcodes.INVOKESTATIC, "android/util/Log", "i", "(Ljava/lang/String;Ljava/lang/String;)I", false) )
                    newInstructions.add(InsnNode(Opcodes.POP))

                    onStartCommandMethod.instructions.insertBefore(it, newInstructions)
                }
            }
        }
        return klass
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant