Convert String to Java Source Code in Android

Does anyone know if it is possible to convert a String / file to a java source that can be compiled at runtime using something like JavaCompiler. This seems to be possible with Java 6, but I have not seen anyone say that JavaCompiler is available on Android.

Basically, my main goal is to turn the text of a String or a file into Android source code. Does anyone know how to do this?

Thanks!

+4
source share
3 answers

Android runs Dalvik, not Java 6. JavaCompiler is not part of the standard Dalvik distribution, so you cannot use it. Dalvik's uptime is for the embedded system because it is less dynamic, compiling code on the fly is one of the things that it shouldn't do.

Try what Xangelo or Google suggested for other script libraries. Clojure for example .;)

+2
source

You can do something advanced and set up a web service to compile the source for you. This service will take a java source, compile it into a class compiled by dalvik, and return it as binary.

This binary can then be added to the custom classloader, as described here: http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html

Although you do not compile on the phone, it will be compiled at runtime and, being inserted into your classloader, is available for execution.

0
source

Technically possible, but not easy. If you look at the Terminal IDE , they pack all the tools to compile Android byte code from the source on the device. You can use a similar approach by writing a line to a file on disk, compiling it, and then use DexClassLoader to load classes from a compiled JAR or APK file.

0
source

Source: https://habr.com/ru/post/1433463/


All Articles