我正在尝试从dex文件动态加载类。每次调用loadClass时,都会收到ClassNotFoundException。该apk文件存在且有效(Android Studio可以读取该文件并查看所有类)。它在本地存储中,因此它不应该是权限问题(但我要求外部存储权限以防万一)。如果我尝试加载java.lang或android。*中的类,则该类可以正常工作,但对于我的包而言,它似乎只会失败。知道出了什么问题吗?
代码:
private void loadApk(File libFile, String classname) {
try {
DexClassLoader classloader = new DexClassLoader(
libFile.getAbsolutePath(), getCodeCacheDir().getAbsolutePath(),
null,
getClassLoader());
Class<?> classToLoad = (Class<?>) classloader
.loadClass("com.gabesechansoftware.testloadedapk.LoadedClass");
Constructor<?> constructor = classToLoad.getConstructor();
Object object = constructor.newInstance();
Log.d("Gabe", "object is " + object);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
...
loadApk(new File(getFilesDir(), "app-debug.apk"), "com.gabesechansoftware.testloadedapk.LoadedClass");
例外:
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: Didn't find class "com.gabesechansoftware.testloadedapk.LoadedClass" on path: DexPathList[[zip file "/data/user/0/com.gabesechansoftware.apkloader/files/app-debug.apk"],nativeLibraryDirectories=[/system/lib, /vendor/lib]]
at com.gabesechansoftware.apkloader.MainActivity.loadApk(MainActivity.java:58)
at com.gabesechansoftware.apkloader.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.gabesechansoftware.testloadedapk.LoadedClass" on path: DexPathList[[zip file "/data/user/0/com.gabesechansoftware.apkloader/files/app-debug.apk"],nativeLibraryDirectories=[/system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.gabesechansoftware.apkloader.MainActivity.loadApk(MainActivity.java:51)
at com.gabesechansoftware.apkloader.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Suppressed: java.io.IOException: No original dex files found for dex location /data/user/0/com.gabesechansoftware.apkloader/files/app-debug.apk
at dalvik.system.DexFile.openDexFileNative(Native Method)
at dalvik.system.DexFile.openDexFile(DexFile.java:353)
at dalvik.system.DexFile.<init>(DexFile.java:100)
at dalvik.system.DexFile.<init>(DexFile.java:74)
at dalvik.system.DexPathList.loadDexFile(DexPathList.java:374)
at dalvik.system.DexPathList.makeDexElements(DexPathList.java:337)
at dalvik.system.DexPathList.<init>(DexPathList.java:157)
at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:65)
at dalvik.system.DexClassLoader.<init>(DexClassLoader.java:57)
at com.gabesechansoftware.apkloader.MainActivity.loadApk(MainActivity.java:48)
... 13 more
它还创建了一个app-debug.apk.prof文件,因此它肯定会找到apk