I am using multidex solution in my project. I found several libs for multidex (https://github.com/casidiablo/multidex, https://github.com/jayway/maven-android-plugin/pull/425). They are all based on modifying ClassLoader's pathList field by reflect.
Every thing is OK. The Activity in secondary dex works well. But when I invoke method in secondary dex in Application's onCreate() method ClassDefNotFound Exception will be got, though I loaded secondary dex before.
I am very sure the pathList is modified successfully. Using Class.forName(""), class in secondary dex can be found.
Who knows why? Waiting for your answer. Thanks so much.
Call method in secondary dex like this,
public class CustomizedApplication extends Application {
@Override
public void onCreate() {
// load secondary dex here
Dexter.loadAllDexes(this);
super.onCreate();
//invoke method in secondary dex
ClassInSecondaryDex.foo();
}
}
I am not an expert in JVM class loading but from my experience I know that all classes used in class (in normal way - not by reflection) should be known when class is initializing, I think it's faster than searching all the classes by class loader when they are invoked. So you have to use reflection in this situation but in your activities all classes will be loaded before activity initialization so it would work. When you use Class.forName() class loader search for class but it does not work like that in normal non-reflection code because it's really not efficient.
try this
public class CustomizedApplication extends Application {
@Override
public void onCreate() {
// load secondary dex here
Dexter.loadAllDexes(this);
super.onCreate();
//invoke method in secondary dex
ClassInSecondaryDex.foo();
try{
Class<?> clazz= Class.forName("your.package.ClassInSecondaryDex");
clazz.getMethod("foo").invoke(clazz);
}catch(Exception e)
{
e.printStackTrace();
}
}