繁体   English   中英

我想通过使用cglib钩context.startActivity,但出现异常

[英]I want to hook context.startActivity by using cglib, but got an exception

private void cghookMInstrumentation() throws Exception {

    //get mInstrumentation
    Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
    Method currentActivityThread = activityThreadClass.getDeclaredMethod("currentActivityThread");
    currentActivityThread.setAccessible(true);
    Object sCurrentActivityThread = currentActivityThread.invoke(null);//ActivityThread instance

    Method getInstrumentation = activityThreadClass.getDeclaredMethod("getInstrumentation");
    getInstrumentation.setAccessible(true);
    Instrumentation instrumentation = (Instrumentation) getInstrumentation.invoke(sCurrentActivityThread);//Instrumentation

    Field mInstrumentationField = activityThreadClass.getDeclaredField("mInstrumentation");
    mInstrumentationField.setAccessible(true);

    Enhancer enHancer = new Enhancer();
    CglibProxy cglibProxy = new CglibProxy();
    enHancer.setSuperclass(instrumentation.getClass());
    enHancer.setCallback(cglibProxy);

    Instrumentation mInstrumentation = (Instrumentation) enHancer.create();

    //replace Instrumentation
    mInstrumentationField.set(instrumentation, mInstrumentation);
}

和例外:

Caused by: java.lang.UnsupportedOperationException: can't load this type of class file
 at java.lang.ClassLoader.defineClass(ClassLoader.java:300)
 at java.lang.reflect.Method.invoke(Native Method) 
 at net.sf.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:384) 
 at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:219) 
 at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145) 
 at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:117) 
 at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108) 
 at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104) 
 at net.sf.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69) 
 at cn.chx.hooklib.CgLibHookClickListener.cghookMInstrumentation(CgLibHookClickListener.java:52) 
 at cn.chx.hooklib.CgLibHookClickListener.onClick(CgLibHookClickListener.java:28) 
 at android.view.View.performClick(View.java:5198) 
 at android.view.View$PerformClick.run(View.java:21147) 
 at android.os.Handler.handleCallback(Handler.java:739) 
 at android.os.Handler.dispatchMessage(Handler.java:95) 
 at android.os.Looper.loop(Looper.java:148) 
 at android.app.ActivityThread.main(ActivityThread.java:5417) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我想钩context.startActivity

ActivityThread -> currentActivityThread() -> mInstrumentation

我编写了一个扩展Instrumentation的类并将其设置到mInstrumentationField ,它可以正常工作,但是我使用cglib遇到了这个异常;

我将不胜感激。

Cglib在Android上不起作用。 您试图做的事是不可能的。 您可以尝试使用Byte Buddy作为替代方案,其中包括适用于Android的类加载策略,从而可以在Android上创建代理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM