動態(tài)加載dex(Part1)
幾年前我寫過一篇博客(Click here),當(dāng)時是看了《Android軟件安全與靜態(tài)分析》這本書寫的。里面有介紹dex的生成和反編譯。現(xiàn)在我們不需要了解那么詳細(xì),大概說一下流程。
- javac生成.class
- jar命令把.class變成.jar
- dx命令把.jar變成.dex
jar可以轉(zhuǎn)換成dex,apk中也可以解壓出dex。
之前說過,抽象類ClassLoader的子類DexClassLoader或者PathClassLoader可以實(shí)現(xiàn)loadClass,區(qū)別是DexClassLoader多了一個參數(shù)optimizedDirectory,這個參數(shù)的意思是dexPath「優(yōu)化」后的存儲路徑,也就是說DexClassLoader是可以讀取外部存儲上的apk/dex/jar的,然后把它們優(yōu)化后拷貝到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這個參數(shù)代表動態(tài)鏈接庫的路徑,這個路徑也必須是內(nèi)部存儲的,不能是外部存儲。這里暫時不討論動態(tài)庫的加載。
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參數(shù)傳的值,是自己在應(yīng)用程序內(nèi)部存儲目錄下創(chuàng)建的路徑。
有點(diǎn)累。一年一度的ingress雙倍經(jīng)驗(yàn)開始了,周末兩天要出去沖一下 15級,估計(jì)要在寒風(fēng)中肝十幾個小時。今天先到這里。
-NOV 25