vendredi 6 mars 2015

Getting a class bytcode on android

I want to send on the wire a class file (.class) for execution on a remote server. I can not use serialization, because the class does not exist on server side, and I want to add new classes dynamically. I don't want to use RMI.


On Dekstop Java, it's easy to do with:



  • Loading the class with Class<?> clazz = Class.forName("mypackage.MyClass").

  • Getting access to the file (or jar container) with URL url = clazz.getResource("MyClass.class")


On Android, I succeeded loading a class instance with this hack butclazz.getResource() always returns null. (And this is logical as Dalvik storage format is different).


I have tried Apache BCEL, but it does not work. Following code throws a ClassNotFoundException:



JavaClass clazz = Repository .lookupClass("mypackage.mMyClass");
byte[] b = clazz.getBytes();


Is there a way to translate a Dalvik byte code to regular class byte code on Android so that I can rebuild a .class file from it and then send it on the wire?


Thanks.






Aucun commentaire:

Enregistrer un commentaire