动态加载dex(Part1)
几年前我写过一篇博客(Click here),当时是看了《Android软件安全与静态分析》这本书写的。里面有介绍dex的生成和反编译。现在我们不需要了解那么详细,大概说一下流程。
- javac生成.class
- jar命令把.class变成.jar
- dx命令把.jar变成.dex
jar可以转换成dex,apk中也可以解压出dex。
之前说过,抽象类ClassLoader的子类DexClassLoader或者PathClassLoader可以实现loadClass,区别是DexClassLoader多了一个参数optimizedDirectory,这个参数的意思是dexPath「优化」后的存储路径,也就是说DexClassLoader是可以读取外部存储上的apk/dex/jar的,然后把它们优化后拷贝到optimizedDirectory里。
public BaseDexClassLoader(String dexPath, File optimizedDirectory,
String libraryPath, ClassLoader parent) {
super(parent);
this.originalPath = dexPath;
this.pathList =
new DexPathList(this, dexPath, libraryPath, optimizedDirectory);
}
libraryPath这个参数代表动态链接库的路径,这个路径也必须是内部存储的,不能是外部存储。这里暂时不讨论动态库的加载。
File optimizedDexPath = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "test.jar"); //
File dexOutputDir = this.getDir("dex", 0); // 自己指定的dex存放目录
DexClassLoader dexClassLoader = new DexClassLoader(optimizedDexPath.getAbsolutePath(),dexOutputDir.getAbsolutePath(), null, getClassLoader());
这个optimizedDirectory参数传的值,是自己在应用程序内部存储目录下创建的路径。
有点累。一年一度的ingress双倍经验开始了,周末两天要出去冲一下 15级,估计要在寒风中肝十几个小时。今天先到这里。
-NOV 25