在jar上从jar加载类 [英] Loading classes from jar on android

查看:123
本文介绍了在jar上从jar加载类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很努力,但总是得到一个Class Not Found异常,从阅读这个答案
https://stackoverflow.com / a / 3024261

I tried really hard, but always get a Class Not Found exception, from reading this answer https://stackoverflow.com/a/3024261

我带着我的罐子运行

dx --dex - output = C:\classes.dex C:\ MyYndroidLib.jar

dx --dex --output=C:\classes.dex C:\MyAndroidLib.jar

并得到一个dex文件

然后我运行

apt添加C:\ MyLib.jar C:\ class.dex

apt add C:\MyLib.jar C:\classes.dex

使用dex文件创建一个jar。

to create a jar with the dex file.

然后我编写了以下代码。

then I wrote the following code.

DexClassLoader classLoader = new DexClassLoader(
            destPath, dirPath +"/" , null, getClass().getClassLoader());
    Class<?> classToLoad = classLoader.loadClass("ClassImpl");

关于调试我可以看到dex在classLoader中(在mDexs成员下)
和ClassImpl是我进入的唯一一个类。

on debug I can see that the dex is inside the classLoader(under the mDexs member) and the ClassImpl is the only class I got inside.

但我一直得到类找不到异常。

but I keep getting the class not found exception.

有人从外部jar获得了动态类加载的工作示例吗?
有人知道我的问题是什么吗?

Anyone got a working sample of dynamic class loading from external jar ? Someone knows whats my problem?

推荐答案

我没有使用aapt ...只有dex。然后我加载了我的类,调用了我的方法。

I did not use aapt... Only dex. And I got my class loaded, and my methods called.

使用此代码查看dex文件内部:

Use this code for see inside the dex file:

DexFile dexfile = DexFile.loadDex(url_jar_path, 
        File.createTempFile("opt", "dex", context.getCacheDir()).getPath(), 0);

// Print all classes in the DexFile

Enumeration<String> classNames = dexfile.entries();

String classname = "";

while (true) {
    if (! classNames.hasMoreElements()) {
        break;
    }
    classname = classNames.nextElement();
}

使用此代码查看课程内部:

Use this code for see inside the class:

Class class = dexclassloader.loadClass(ruta_clase_en_jar);

String name;

Method[] method_array = clase.getMethods();

for (Method i: method_array) {
    name = i.getName ();
}

这篇关于在jar上从jar加载类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆