4

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!

2
  • 1
    Probably not although you depending on what you are actually trying to accomplish you might be better off pursuing scripting instead. See code.google.com/p/android-scripting Commented Sep 10, 2012 at 20:39
  • It seems like I'd have to download the apk to be able to do this...is there a way to use a script like this that doesn't require downloading of an extra apk that I can use in my code? Commented Sep 10, 2012 at 21:20

3 Answers 3

2

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. ;)

Sign up to request clarification or add additional context in comments.

3 Comments

Source code to this library is available, you should be able to include it in you application. Though I cannot find any explanation on how to do that...
By the way, similar question was asked before: stackoverflow.com/questions/5862569/…
@user1419579 It looks like it is possible to run BeanShell (Java interpreter) on Android, here's blog post talking about it.
0

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.

1 Comment

this is a really good idea, i thought of it too, but unfortunately communicating with a server won't be an option. Thanks anyway though.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.