Dmitri Novikov
Dmitri Novikov

Reputation: 123

Android. Dynamic class loading and Java interfaces

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.

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 VM doesn't like this, i get

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?

Upvotes: 0

Views: 3323

Answers (1)

Dmitri Novikov
Dmitri Novikov

Reputation: 123

I was lucky to find this: https://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!

Upvotes: 2

Related Questions