tags:

views:

413

answers:

2

Hi all;

This question has been asked(and answered) many times about dynamically generating and loading java bytecodes at runtime into a running Dalvik VM, but is there any way to load dex files/bytecodes into an app at runtime?

Thanks

A: 

is there any way to load dex files/bytecodes into an app at runtime?

Look at DexFile and DexClassLoader.

CommonsWare
Previously on this topic: http://stackoverflow.com/questions/1001944/android-remote-code-loading/2450049#2450049
fadden
+1  A: 

The Dalvik team would like to build a first-class runtime code generation library. We're tracking the feature request as Android bug 6322. Unfortunately, we have a very long list of performance and correctness issues, so I can't give you a timeline for when we'll spend time on this issue.

There are some alternatives, but they will all take some work:

  • Run your application on a standard JVM and exercise all runtime code generation there. Dump the .class files from memory to files, and then run dx on those files. If you're quite sophisticated, you could integrate all of this work into your build.

  • Include the open source dx tool as a project library, and execute it programatically from within your application, possibly in your application's classloader. This will bloat your application's binary.

Jesse Wilson
Thanks for your answer. Is there anything preventing me from writing my own code generator right now? I've written one for .Net->Flash and .Net->.Net, and Dex is like a cross between Java .Class and Flash .ABC files.Also, thanks for the link. I starred it and added a comment(requesting it's API be similar to .Net's DLR).
Michael Kohout
You could definitely write your own code generator right now. If you give it an Apache-license, even better!
Jesse Wilson