Is there any way to load .dex file dynamically at run time in android. I tried as below.
try {
File dexDir = getApplicationContext().getCacheDir();
String dexpath = Environment.getExternalStorageState()+"/app-debug.jar";
DexClassLoader dexClassLoader = new DexClassLoader(dexpath,dexDir.getAbsolutePath() ,null,context.getClassLoader());
//PathClassLoader dexClassLoader = new PathClassLoader(dexpath,context.getClassLoader());
Class<Object> classToLoad = (Class<Object>) dexClassLoader.loadClass("com.example.welcome.myclass.TestClass");
Object newInstance = classToLoad.newInstance();
Method method = classToLoad.getMethod("toast");
method.invoke(newInstance);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
getting following error.
java.lang.ClassNotFoundException: Didn't find class "com.example.welcome.myclass.TestClass" on path: DexPathList[[],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
Suppressed: java.lang.ClassNotFoundException: Didn't find class "com.example.welcome.myclass.TestClass" on path: DexPathList[[zip file "/data/app/com.example.welcome.dcl-1/base.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
08-11 16:15:05.179 9197-9197/com.example.welcome.dcl W/System.err: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
dx
tool from Android sdk – Elba