Home » Android » classloader – android dexclassloader get list of all classes

classloader – android dexclassloader get list of all classes

Posted by: admin June 15, 2020 Leave a comment

Questions:

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

How to&Answers:

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