Reputation: 470
Does anyone know if it's possible to convert a String/file into java source which can be compiled at run time using something like JavaCompiler. It looks like this is possible with Java 6, but I haven't seen anyone say that JavaCompiler is available in Android.
Basically my main goal is to turn a String or file text into source code in Android. Does anyone know how that can be done?
Thanks!
Upvotes: 4
Views: 1799
Reputation: 36302
Technically possible, but not easy. If you look at Terminal IDE they package in all the tools to compile Android byte code from source on the device. You could take a similar approach by writing out the string to a file on disk, compiling it, and then use DexClassLoader
to load the classes from the compiled JAR file or APK.
Upvotes: 0
Reputation: 11113
You could do something advanced and setup a web service to compile source for you. This service would accept java source, compile it into a dalvik compiled class and return it as a binary.
This binary could then be added to a custom class loader as described here: http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html
Although you are not compiling on the phone, this would be compiled during runtime and, once inserted into your classloader, available for execution.
Upvotes: 0
Reputation: 4315
Android runs Dalvik not Java 6. JavaCompiler is not included in standard Dalvik distribution, so you cannot use it. Dalvik runtime is designed for embedded system as such it is less dynamic, compiling code on the fly is one of the things that it is not supposed to do.
Try what Hyangelo suggested, or Google for other scripting libraries. Clojure for example. ;)
Upvotes: 2