我想使用dex类加载器将代码动态加载到我的Android应用程序中。我已经按照本教程:http://www.marioalmeida.eu/2015/01/27/how-to-easy-way-load-apk-classes-using-dexclassloader/。但是我得到以下错误堆栈跟踪:
h5py
我创建了一个没有Activity的Android项目以及我要加载的以下类:
01-08 12:25:38.007 15147-15147/com.ph.dexloader W/System.err: java.lang.ClassNotFoundException: Didn't find class "com.ph.injection.Injection" on path: DexPathList[[zip file "/storage/emulated/0/testjars/injector.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at com.ph.dexloader.MainActivity.test3(MainActivity.java:153)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at com.ph.dexloader.MainActivity$1.onClick(MainActivity.java:34)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at android.view.View.performClick(View.java:5198)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at android.view.View$PerformClick.run(View.java:21147)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at android.os.Looper.loop(Looper.java:148)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at java.lang.reflect.Method.invoke(Native Method)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: Suppressed: java.io.IOException: No original dex files found for dex location /storage/emulated/0/testjars/injector.apk
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at dalvik.system.DexFile.openDexFileNative(Native Method)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at dalvik.system.DexFile.openDexFile(DexFile.java:295)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at dalvik.system.DexFile.<init>(DexFile.java:111)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at dalvik.system.DexFile.loadDex(DexFile.java:151)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at dalvik.system.DexPathList.loadDexFile(DexPathList.java:282)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at dalvik.system.DexPathList.makePathElements(DexPathList.java:248)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at dalvik.system.DexPathList.<init>(DexPathList.java:120)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:48)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at dalvik.system.DexClassLoader.<init>(DexClassLoader.java:57)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at com.ph.dexloader.MainActivity.test3(MainActivity.java:146)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: ... 10 more
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: Suppressed: java.lang.ClassNotFoundException: Didn't find class "com.ph.injection.Injection" on path: DexPathList[[zip file "/data/app/com.ph.dexloader-2/base.apk"],nativeLibraryDirectories=[/data/app/com.ph.dexloader-2/lib/x86, /vendor/lib, /system/lib]]
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: ... 12 more
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: Suppressed: java.lang.ClassNotFoundException: Didn't find class "com.ph.injection.Injection" on path: DexPathList[[dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-support-annotations-24.1.0_ae5436f3e75e322599cc28c891c7bbfb3749511e-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-internal_impl-24.1.0_3cdb966acce2459aa95bcd6df4240d298e8a6aa0-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-com.android.support-support-vector-drawable-24.1.0_68483e445bfd7fe2615d6637e23aa54f7eca71ad-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-com.android.support-support-v4-24.1.0_0b01882602c790d27bb53e6d99c19f41bfa0f42f-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-com.android.support-appcompat-v7-24.1.0_ffbd2666f53be3072a55297d806cbb863283a31e-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-com.android.support-animated-vector-drawable-24.1.0_fa7ca9f78df22fc1c453fed6517df1a2457fbf73-classes.dex"],nativeLibraryDirectories=[/data/app/com.ph.dexloader-2/lib/x86, /vendor/lib, /system/lib]]
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at com.android.tools.fd.runtime.IncrementalClassLoader$DelegateClassLoader.findClass(IncrementalClassLoader.java:90)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at com.android.tools.fd.runtime.IncrementalClassLoader.findClass(IncrementalClassLoader.java:62)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: ... 13 more
01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err: Suppressed: java.lang.ClassNotFoundException: com.ph.injection.Injection
01-08 12:25:38.009 15147-15147/com.ph.dexloader W/System.err: at java.lang.Class.classForName(Native Method)
01-08 12:25:38.009 15147-15147/com.ph.dexloader W/System.err: at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
01-08 12:25:38.009 15147-15147/com.ph.dexloader W/System.err: at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
01-08 12:25:38.009 15147-15147/com.ph.dexloader W/System.err: at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
01-08 12:25:38.009 15147-15147/com.ph.dexloader W/System.err: ... 14 more
01-08 12:25:38.009 15147-15147/com.ph.dexloader W/System.err: Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
我使用gradle汇编生成了APK文件并将其放入模拟器上的/storage/emulated/0/testjars/injector.apk。
这是我用来加载DEX文件的代码:
package com.ph.injection;
import android.util.Log;
public class Injection {
public void inject() {
/* this code is getting injected */
Log.d("INJECTED", "This is injected code!");
}
}
我不知道为什么没有正确加载DEX文件/找不到类。有人能帮助我吗?如果重要的话,我会使用Genymotion Android 6.0.0模拟器。
谢谢!
答案 0 :(得分:0)
假设您没有遇到与DexClassLoader不相关的问题,例如没有读取和写入外部存储的权限,则问题很可能是您尚未将应用程序配置为启用了Multidex。在gradle中,您可以这样配置应用程序:
defaultConfig {
…
multiDexEnabled true
}
如果要针对api 26以下的应用定位,则还必须添加对mulitdex支持库的依赖。对于api 26及更高版本,这不是必需的,因为它已嵌入android中。
如果考虑一下,如果您尝试加载外部代码,则该代码存在于另一个dex文件中,因此,您的应用需要启用multidex才能正常工作。大多数人仅在达到单个dex应用程序的65K方法限制时才将其应用程序配置为启用multidex。在运行时访问外部代码只是使您的应用启用mulitidex的另一个原因。注意:您引入的外部代码不必构建为启用了Multidex。
如果您的应用程序已经启用了multidex,则导致无法找到您的类的消息的另一个原因(假设您指定的jar正确包含了您想要的类和类名)将是因为您要加载的类,需要一个找不到的类。追溯起来可能有些棘手。您收到的错误消息是您找不到类,而问题实际上是找不到依赖类。
以我的经验,诸如“关闭实例运行”和“禁用dex优化”之类的解决方案通常无法正常运行,并且通常仅与某些特定的较旧版本(可能是beta)相关,并且此后已得到纠正。它们被称为阿司匹林解决方案,可以解决某些问题,并且在某些情况下可以起作用,但是潜在的问题仍然是个谜。