android dexclassloader获取所有类的列表

时间:2012-08-30 11:15:45

标签: android classloader

我在我的android应用程序中使用来自资产或sdcard的外部jar。为此,我使用的是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

获取所有类名的列表

1 个答案:

答案 0 :(得分:13)

要列出包含classes.dex文件的.jar文件中的所有类,请使用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);
}
相关问题