Android

List All Classes after Multidex

Intro

Google recently posted an article to solve the 65K Reference Limit problem.
It is such a relief that we will not need to worry about the following errors any more.

1
2
3
4
5
6
Conversion to Dalvik format failed:
Unable to execute dex: method ID not in [0, 0xffff]: 65536
//--or--
trouble writing output:
Too many field references: 131000; max is 65536.
You may try using --multi-dex option.

Problem

After configuring multidex in the module, some projects may encounter the loss of part of the the classes when using reflection to list all the classes in the apk.
The usual code may like this:

1
2
3
4
5
6
7
8
9
//get the apk path
String path = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0).sourceDir;
//use DexFile the unzip the apk, find the *classes.dex* in the apk
DexFile dexfile = new DexFile(path);
//get all the classes in this dex
Enumeration<String> dexEntries = dexfile.entries();
while (dexEntries.hasMoreElements()) {
classNames.add(dexEntries.nextElement());
}