来一发DEX加载器,可以兼容Activity类的。。

    xiaoxiao2021-03-25  95

    单个反射类 package com.google.android.gms.example.bannerexample; import android.content.Context; public class InvokeParam { private Context ctx; private String dexpath; private String dexoutputpath; private String className; private String functionName; private Object[] paramObj; private Class[] paramReq; public InvokeParam(Context c) { ctx = c; } public Context getContext() { return this.ctx; } public String getDexoutPath() { return this.dexoutputpath; } public void setDexoutPath(String para) { this.dexoutputpath = para; } public String getDexpath() { return this.dexpath; } public void setDexpath(String para) { this.dexpath=para; } public void setFunctionName(String para) { this.functionName=para; } public String getFunctionName() { return this.functionName; } public void setClassName(String para) { this.className=para; } public String getClassName() { return this.className; } public Object[] getParamObj() { return paramObj; } public void setParamObj(Object[] paramObj) { this.paramObj = paramObj; } public Class[] getParamReq() { return paramReq; } public void setParamReq(Class[] paramReq) { this.paramReq = paramReq; } } 支持类

    package com.google.android.gms.example.bannerexample; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * Created by LeonWang on 2017/3/10. */ public class RefInvoke { public static Object invokeStaticMethod(String class_name, String method_name, Class[] pareTyple, Object[] pareVaules) { try { Class obj_class = Class.forName(class_name); Method method = obj_class.getMethod(method_name, pareTyple); return method.invoke(null, pareVaules); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static Object invokeMethod(String class_name, String method_name, Object obj, Class[] pareTyple, Object[] pareVaules) { try { Class obj_class = Class.forName(class_name); Method method = obj_class.getMethod(method_name, pareTyple); return method.invoke(obj, pareVaules); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static Object getFieldOjbect(String class_name, Object obj, String filedName) { try { Class obj_class = Class.forName(class_name); Field field = obj_class.getDeclaredField(filedName); field.setAccessible(true); return field.get(obj); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static Object getStaticFieldOjbect(String class_name, String filedName) { try { Class obj_class = Class.forName(class_name); Field field = obj_class.getDeclaredField(filedName); field.setAccessible(true); return field.get(null); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static void setFieldOjbect(String classname, String filedName, Object obj, Object filedVaule) { try { Class obj_class = Class.forName(classname); Field field = obj_class.getDeclaredField(filedName); field.setAccessible(true); field.set(obj, filedVaule); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void setStaticOjbect(String class_name, String filedName, Object filedVaule) { try { Class obj_class = Class.forName(class_name); Field field = obj_class.getDeclaredField(filedName); field.setAccessible(true); field.set(null, filedVaule); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

    classloader替换

    @SuppressLint("NewApi") public static void loadApkClassLoader(DexClassLoader dLoader){ try{ String filesDir = mContext.getCacheDir().getAbsolutePath(); // 配置动态加载环境 Object currentActivityThread = RefInvoke.invokeStaticMethod("android.app.ActivityThread", "currentActivityThread", new Class[] {}, new Object[] {});//获取主线程对象 String packageName = mContext.getPackageName();//当前apk的包名 ArrayMap mPackages = (ArrayMap) RefInvoke.getFieldOjbect("android.app.ActivityThread", currentActivityThread, "mPackages"); WeakReference wr = (WeakReference) mPackages.get(packageName); RefInvoke.setFieldOjbect("android.app.LoadedApk", "mClassLoader", wr.get(), dLoader); }catch(Exception e){ Log.i("demo", "load apk classloader error:"+Log.getStackTraceString(e)); } } private static InputStream getAssetStream(Context context,String asset) { try { AssetManager assetManager = context.getResources().getAssets(); InputStream stream = assetManager.open(asset); return stream; } catch (IOException e) { return null; } } private static void copyAssetFiles(InputStream in, OutputStream out) { int BUFFER_SIZE = 1024; try { byte[] buffer = new byte[BUFFER_SIZE]; int read; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } in.close(); in = null; out.flush(); out.close(); out = null; } catch (Exception e) { e.printStackTrace(); } } 使用

    String basePath = Environment.getExternalStorageDirectory().toString()+"/"+"ad_dex.jar";//context.getFilesDir().getAbsolutePath(); InputStream in = getAssetStream(this,"ad_dex.jar"); FileOutputStream out = null; try { out = new FileOutputStream(basePath); } catch (FileNotFoundException e) { e.printStackTrace(); } copyAssetFiles(in,out); String dexPath = basePath; String dexOutputPath = this.getDir("dex", Context.MODE_PRIVATE).toString(); InvokeParam invokeParam=new InvokeParam(this); invokeParam.setDexpath(dexPath); invokeParam.setDexoutPath(dexOutputPath); invokeParam.setClassName("com.rt.RtBack"); invokeParam.setFunctionName("promote"); invokeParam.setParamObj(new Object[]{this}); invokeParam.setParamReq(new Class[]{Context.class}); runFunction(invokeParam); public static Object runFunction(InvokeParam param) { DexClassLoader localDexClassLoader = new DexClassLoader(param.getDexpath(),param.getDexoutPath(), null,param.getContext().getClassLoader()); loadApkClassLoader(localDexClassLoader); try { String className = param.getClassName(); Class localClass = localDexClassLoader.loadClass(className); Method methodReq = localClass.getDeclaredMethod(param.getFunctionName(),param.getParamReq()); return methodReq.invoke(null,param.getParamObj()); } catch (Exception ex) { ex.printStackTrace(); return null; } }

    转载请注明原文地址: https://ju.6miu.com/read-21688.html

    最新回复(0)