gpt4 book ai didi

java - 安卓| DexClassLoader,ClassNotFound 异常

转载 作者:行者123 更新时间:2023-11-29 04:47:36 30 4
gpt4 key购买 nike

所以我正在尝试加载我从我的应用程序编写的类,但我收到 ClassNotFound 异常。

类(class):

public class myclass{
public String doSomething()
{
return "Hello from myclass";
}
}

然后我使用以下命令创建一个 jar 文件:

javac myclass.java
jar cvf myclass.jar myclass.class

我的下一步是为我的 jar 文件创建一个 classes.dex,并将其添加到 jar 中:

dx --dex --output=classes.dex myclass.jar
aapt add myclass.jar classes.dex

到目前为止一切正常,没有错误。

这一步结束后,我的jar文件如下:

myclass.jar
-> classes.dex
-> myclass.class
-> META-INF
-> MANIFEST.MF

我的 list 文件包含以下行:

Manifest-Version: 1.0
Created-By: 1.7.0_79 (Oracle Corporation)

现在在我的应用程序中我正在这样做:

private void activate()
{
try {
URL url;
final String libPath = Environment.getExternalStorageDirectory() + "/myclass.jar";
final File tmpDir = getDir("dex", 0);

final DexClassLoader classloader = new DexClassLoader(libPath, tmpDir.getAbsolutePath(), null, this.getClass().getClassLoader());
final Class<Object> classToLoad = (Class<Object>) classloader.loadClass("myclass");

final Object myInstance = classToLoad.newInstance();
final Method doSomething = classToLoad.getMethod("doSomething");

String result = (String)doSomething.invoke(myInstance);
Toast.makeText(MainActivity.this, String.valueOf(result), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}

注意事项:

  1. 我把myclass.jar文件复制到

Environment.getExternalStorageDirectory()

目录,我确认应用程序正在正确读取文件。

  1. 我已经在 list 文件中设置了所需的权限(读取和写入外部存储)

给我错误的行是:

final Class<Object> classToLoad = (Class<Object>) classloader.loadClass("myclass");

错误:

Didn't find class "myclass" on path: DexPathList[[zip file "/storage/emulated/0/myclass.jar"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]

这是被抑制的异常:

java.io.IOException: No original dex files found for dex location /storage/emulated/0/myclass.jar

java.lang.ClassNotFoundException: Didn't find class "myclass" on path: DexPathList[[dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-support-annotations-23.3.0_d42c1a3ea55c8ce3f82fc6c8adde2e2271b97d01-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-internal_impl-23.3.0_75eb76bdd9c015b48a4280667bfd60eddc59734a-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-com.android.support-support-vector-drawable-23.3.0_0a5b88d45587f58c7ea931638fd9c9d6c4641bc7-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-com.android.support-support-v4-23.3.0_a0a32aaad2874bf2c15ffa67543dfe5af5b91a95-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-com.android.support-appcompat-v7-23.3.0_eae70f9c3956161dfb324670cb40cb42e6e1b16a-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-com.android.support-animated-vector-drawable-23.3.0_d4513d6bda757fa567bc4d7ea3e4bb7cbd09c077-classes.dex"],nativeLibraryDirectories=[/data/app/hayzohar.testingclassloaders-2/lib/x86, /vendor/lib, /system/lib]]

我做错了什么?

最佳答案

在进一步研究这段代码后,我发现问题出在权限上。模拟器上运行的是Android 6,gradle文件中的目标sdk设置为23,这意味着我们必须请求运行时权限。

归根结底,问题是没有读取文件的权限。

如果有人遇到这个问题并且正在运行带有 android 6 的模拟器,确保您请求运行时权限( list 是不够的)或者您可以将目标 sdk 设置为版本 22 及以下。

关于java - 安卓| DexClassLoader,ClassNotFound 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36565032/

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