Android系统。动态类加载和Java接口 [英] Android. Dynamic class loading and Java interfaces

查看:150
本文介绍了Android系统。动态类加载和Java接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经差不多完成了我的第一个真正的Andr​​oid项目,出租车订购应用程序,只有一些小的可用性问题留。对于未来的版本中,我们计划实现编程code和资源的内置自动更新,像一些好节目做的。我设法运行一个简单的例子,基于的http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html, <一href=\"http://stackoverflow.com/questions/3022454/how-to-load-a-java-class-dynamically-on-android-dalvik\">How动态地加载Java类在Android / Dalvik的?和<一个href=\"http://stackoverflow.com/questions/6857807/is-it-possible-to-dynamically-load-a-library-at-runtime-from-an-android-applicat?rq=1\">Is它可以动态地从Android应用程序中加载运行时库?。我不会张贴在这里完成code,它看起来很像在上面的链接。主要APK作为发射器,它加载从SD卡一个jar文件,从那里加载一个特殊的类,实例化一个obect,并呼吁通过Java及其方法反映的API,简单地带来了AB AlertDialog。

I've almost finished my first real Android project, a taxi ordering application, only some little usability concerns are left. For future releases we plan to implement built-in automatic updates of programming code and resources like some good programs do. I managed to run a simple example, based on http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html, How to load a Java class dynamically on android/dalvik? and Is it possible to dynamically load a library at runtime from an Android application?. I will not post here complete code, it looks much like over the links above. Main apk acts as launcher, it loads a jar-file from SD-card, loads a special class from there, instantiates an obect and calls its method via Java Reflect API, that simply brings up ab AlertDialog.

不过,我希望能够用共同的基础Java接口无论是在启动和加载在图书馆,作为API。所以,我想无论是在发射器和库申报名为问题的inteface。在code是这样的:

But I want to be able to use common base Java interfaces both in launcher and in loaded library, acting as API. So I tried to declare an inteface called Problem both in launcher and library. The code is like this:

Class<?> problemClass = cl.loadClass("ru.homez.euler.Problem1");
Problem problem = (Problem)problemClass.newInstance();
problem.solve(this);

Dalvik虚拟机不喜欢这样,我得到

Dalvik VM doesn't like this, i get

java.lang.IllegalAccessError:类参考在pre-验证类解析
  意想不到的实施

java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation

我觉得出现这种情况是由于这个问题的接口都在发射器和库定义的,我在这里看到这样的问题。如何处理呢?

I think this happens due to that Problem interface is defined both in launcher and library, I have seen issues like that here. How to deal with this?

推荐答案

我很幸运找到这样的:<一href=\"http://stackoverflow.com/questions/10698049/how-to-dynamically-load-a-jar-with-%E2%80%8C%E2%80%8Bcommon-abstract-class\">http://stackoverflow.com/questions/10698049/how-to-dynamically-load-a-jar-with-‌​common-abstract-class.我剥过接口从库罐子,并在装载机我改变了它的可用性,以公众(库的类加载器并没有看到满载APK包可用类)!问题解决了!

I was lucky to find this: http://stackoverflow.com/questions/10698049/how-to-dynamically-load-a-jar-with-‌​common-abstract-class. I stripped interface off from the library jar, and in loader I changed its availability to public (library class loader doesn't see package-available classes loaded with apk)! Problem solved!

这篇关于Android系统。动态类加载和Java接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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