Android Dex in Memory 内存中加载dex

 try {
            FileInputStream fis = new FileInputStream(CustomConstant.BASE_PATH + "so.dex");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int bytesRead;
            byte[] buffer = new byte[1024];
            while ((bytesRead = fis.read(buffer, 0, buffer.length)) != -1) {
                baos.write(buffer, 0, bytesRead);
            }
            baos.flush();
            byte[] dex = baos.toByteArray();
            ByteBuffer bb = ByteBuffer.allocate(dex.length);
            bb.put(dex);
            bb.position(0);

            InMemoryDexClassLoader inMemoryDexClassLoader =
                new InMemoryDexClassLoader(bb, TheApplication.getAppContext().getClassLoader());

            Class classDex = inMemoryDexClassLoader.loadClass("com.csizg.security.DexUtils3");
            Method method = classDex.getMethod("encrypt");
            String ss = (String) method.invoke(classDex.newInstance());
            String sss = "";
            LogUtil.d("DDDDDD", ss);
        } catch (IOException | ClassNotFoundException | NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

附赠dex 生成方式: https://www.jianshu.com/p/2cc4f5665cbb

你可能感兴趣的:(Android Dex in Memory 内存中加载dex)