zoukankan      html  css  js  c++  java
  • Android Hook Instrumentation

    要替换的Instrumentation类:

    public class MyInstrumentation extends Instrumentation {
    
        private static final String TAG = MyInstrumentation.class.getSimpleName();
    
        private Instrumentation base;
    
        public MyInstrumentation(Instrumentation base) {
            super();
            this.base = base;
        }
    
       // 返回值改成true, 重写异常处理方法,在遇到异常时,程序不崩溃, @Override
    public boolean onException(Object obj, Throwable e){ e.printStackTrace(); return true; } @Override public Activity newActivity(ClassLoader cl, String className, Intent intent) throws InstantiationException, IllegalAccessException, ClassNotFoundException { try { intent.setExtrasClassLoader(cl); intent.getBooleanExtra("a",false); }catch (Exception e){ e.printStackTrace(); } Log.e(TAG,"className " + className);return super.newActivity(cl,className, intent); } }

    Hook工具类:

    public class Hooker {
        public static final String TAG = "Hooker";
    
        public static void hookInstrmentation(){
            Class<?> activityThread;
            try{
                activityThread = Class.forName("android.app.ActivityThread");
                Method sCurrentActivityThread = activityThread.getDeclaredMethod("currentActivityThread");
                sCurrentActivityThread.setAccessible(true);
                //获取ActivityThread 对象
                Object activityThreadObject = sCurrentActivityThread.invoke(activityThread);
    
                //获取 Instrumentation 对象
                Field mInstrumentation = activityThread.getDeclaredField("mInstrumentation");
                mInstrumentation.setAccessible(true);
                Instrumentation instrumentation = (Instrumentation) mInstrumentation.get(activityThreadObject);
                MyInstrumentation customInstrumentation = new MyInstrumentation(instrumentation);
                //将我们的 customInstrumentation 设置进去
                mInstrumentation.set(activityThreadObject, customInstrumentation);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    替换:

    public class GlobalApplication extends android.app.Application {
    
        @Override
        protected void attachBaseContext(Context base) {
            Hooker.hookInstrmentation();   //替换操作
            super.attachBaseContext(base);
        }
    
    }
  • 相关阅读:
    Spring Security demo
    applicationContext-XXX.xml和XXX-servlet.xml的区别
    搜索意图识别浅析
    如何配置使用Dnsmasq
    机器学习十大算法之EM算法
    如何利用OpenSSL生成证书
    11月13日晚直播预告 | 关于数据可视化,网易大数据资深专家将在这里告诉你
    漫话中文分词
    10分钟快速构建汽车零售看板
    聊一聊整车厂的那些事——售后配件业务
  • 原文地址:https://www.cnblogs.com/roger-jc/p/12667282.html
Copyright © 2011-2022 走看看