2012-08-30 45 views
6

我在我的android應用程序中使用資產或SD卡中的外部jar。要做到這一點,我正在使用DexClassLoader。android dexclassloader獲取所有類的列表

DexClassLoader cl = new DexClassLoader(dexInternalStoragePath.getAbsolutePath(), 
         optimizedDexOutputPath.getAbsolutePath(), 
         null, 
         getClassLoader()); 

加載類:

Class myNewClass = cl.loadClass("com.example.dex.lib.LibraryProvider"); 

它的作品真的不錯,但現在我想在我的DexClassLoader 我發現this在java中工作的所有類名稱的列表中,但沒有這樣的在android中的東西。

的問題是如何從DexClassLoader

回答

11

獲得所有類的名稱列表要列出所有類.jar文件中包含一個classes.dex文件使用DexFile,不DexClassLoader,例如像這樣:

String path = "/path/to/your/library.jar" 
try { 
    DexFile dx = DexFile.loadDex(path, File.createTempFile("opt", "dex", 
      getCacheDir()).getPath(), 0); 
    // Print all classes in the DexFile 
    for(Enumeration<String> classNames = dx.entries(); classNames.hasMoreElements();) { 
     String className = classNames.nextElement(); 
     System.out.println("class: " + className); 
    } 
} catch (IOException e) { 
    Log.w(TAG, "Error opening " + path, e); 
} 
+0

儘管http://developer.android.com/reference/dalvik/system/DexFile.html#loadDex(java.lang.String,java.lang.String中,INT註釋),其「這個函數不應該直接被應用程序調用,」DexFile.loadDex()似乎是檢查一個dex文件的唯一方法 - 一個簡單的'new DexFile(File)'拋出'IoException'。 –

+0

當我試圖從jar中讀取所有類時,我只從當前運行的應用程序獲取所有類,但不從jar文件中獲取類。我不明白髮生了什麼 –

+0

你能得到實際的課程嗎? – TheRealChx101