在android中动态加载.dex

时间:2017-08-11 09:38:48

标签: android classnotfoundexception

有没有办法在android中运行时动态加载.dex文件。 我尝试如下。

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

收到以下错误。

 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)

0 个答案:

没有答案