I have tried using things on Janino on Android and have concluded that they simply do not work within the Dalvik VM.
So Im just going to ask a simple question: On Android,Is it possible to compile a string containing code during runtime for use within the app. If so, are there any libraries that let me do so and/or can you share a code example of how to do it?
For (a very simple) example, If I had a String object containing the following:
public class Adder{
int x;
int y;
public Adder(int x,int y) {
this.x = x;
this.y = y;
}
public int add() { return x+y;}
}
As one giant line of string. Is there a way I can process it to create an instance of an Adder object so I can call the add()
method, say, via the Reflection API?
Edit I've tried beanshell interpretation but it proved to be too slow. Im looking for something a little faster, just like Janino
The compilers convert your source code into DEX (Dalvik Executable) files, which include the bytecode that runs on Android devices, and everything else into compiled resources. The packager combines the DEX files and compiled resources into an APK or AAB, depending on the chosen build target.
Tested on Android Studio 0.8.6 - 1.2.2 Using this method you can have Java modules and Android modules in the same project and also have the ability to compile and run Java modules as stand alone Java projects. Open your Android project in Android Studio. If you do not have one, create one.
Android Studio helps you to create new Java classes; enumeration and singleton classes; and interface and annotation types based on file templates. To create a new Java class or type, follow these steps: In the Project window, right-click a Java file or folder, and select New > Java Class.
java is compiled by javac (Java compiler). Javac compiles Java source file into Java byte-code file as TestClass. class.
ImagePlayground is an open source Android app that does this using Dexmaker and a custom programming language.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With