Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android dexclassloader get list of all classes

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

like image 860
M Kosztolowicz Avatar asked Aug 30 '12 11:08

M Kosztolowicz


1 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);
}
like image 57
Jens Avatar answered Oct 21 '22 23:10

Jens