android dexclassloader get list of all classes

限于喜欢 提交于 2019-12-04 20:19:54

问题


I am using external jar from assets or sdcard in my android application. To do that I am using DexClassLoader.

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

to load a class :

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

it works really nice but now I want to get list of all classes names in my DexClassLoader I found this to work in java but there is no such thing in android.

Question is how i can get list of all classes names from DexClassLoader


回答1:


To list all classes in a .jar file containing a classes.dex file you use DexFile, not DexClassLoader, e.g. like this:

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);
}


来源:https://stackoverflow.com/questions/12195642/android-dexclassloader-get-list-of-all-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!