8

我在我的 android 应用程序中使用来自 assets 或 sdcard 的外部 jar。为此,我正在使用 DexClassLoader。

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

加载一个类:

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

它工作得非常好,但现在我想在我的 DexClassLoader 中获取所有类名的列表,我发现可以在 java 中工作,但在 android 中没有这样的东西。

问题是我如何从 DexClassLoader 获取所有类名的列表

4

1 回答 1

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);
}
于 2012-08-30T12:17:38.847 回答