gpt4 book ai didi

Android dex 类加载器 - 找不到类

转载 作者:太空狗 更新时间:2023-10-29 14:45:01 25 4
gpt4 key购买 nike

我想使用 dex 类加载器将代码动态加载到我的 Android 应用程序中。我遵循了本教程:http://www.marioalmeida.eu/2015/01/27/how-to-easy-way-load-apk-classes-using-dexclassloader/ .但是我得到以下错误堆栈跟踪:

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

我创建了一个没有 Activity 的 Android 项目和我要加载的以下类:

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!");
}
}

我使用 gradle assemble 生成了 APK 文件,并将其放入模拟器上的/storage/emulated/0/testjars/injector.apk 中。

这是我用来加载 DEX 文件的代码:

    String dexFile = "/testjars/injector.apk";

// Get the path to the SD card
File f = new File(Environment.getExternalStorageDirectory().toString() + dexFile);
final File optimizedDexOutputPath = getDir("outdex", 0);
DexClassLoader classLoader = new DexClassLoader(f.getAbsolutePath(), optimizedDexOutputPath.getAbsolutePath(),null, getClassLoader());

// The classpath is created for the new class
String completeClassName = "com.ph.injection.Injection";
String methodToInvoke = "inject";

try {
Class<?> myClass = classLoader.loadClass(completeClassName);
Object obj = (Object) myClass.newInstance();
Method m = myClass.getMethod(methodToInvoke);
m.invoke(obj);
}
catch (Exception e) {
e.printStackTrace();
}

我不知道为什么没有正确加载 DEX 文件/找不到类。有人可以帮助我吗?如果重要的话,我会使用 Genymotion Android 6.0.0 模拟器。

谢谢!

最佳答案

假设您没有遇到与 DexClassLoader 无关的问题,例如没有读取和写入外部存储的权限,问题很可能是您没有将您的应用配置为启用 multidex。在 gradle 中,您可以这样配置您的应用:

defaultConfig {

multiDexEnabled true
}

如果您的目标应用程序低于 api 26,那么您还必须添加对 mulitdex 支持库的依赖。对于 api 26 及更高版本,这不是必需的,因为它已经嵌入到 android 中。

如果您考虑一下,如果您尝试加载外部代码,该代码存在于另一个 dex 文件中,因此您需要为您的应用程序启用 multidex 才能使其正常工作。大多数人只有在达到单个 dex 应用程序的 65K 方法限制时,才会将他们的应用程序配置为启用 multidex。在运行时访问外部代码只是让您的应用程序启用多索引的另一个原因。注意:您引入的外部代码不必构建为启用 multidex。

如果您的应用程序已经启用了 multidex,那么您会收到无法找到您的类的消息的另一个原因(假设您指定了包含所需类和正确类名的 jar)是您要加载的类,需要一个找不到的类。追踪起来可能有点棘手。您收到的错误消息是您找不到您的类,而实际上问题是它找不到从属类。

根据我的经验,“关闭实例运行”和“禁用 dex 优化”之类的解决方案通常偏离正轨,通常只与某些特定的旧版本相关,可能是测试版,并且已经得到纠正。它们被称为阿司匹林解决方案,因为在某些情况下它们确实有效,但潜在的问题仍然是个谜。

关于Android dex 类加载器 - 找不到类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41535711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com