Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling String as Code during Runtime on Android

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

like image 291
sourdesi Avatar asked Jul 09 '13 01:07

sourdesi


People also ask

How does Android compile code?

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.

Can I compile Java in Android?

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.

Can I write Java code in Android Studio?

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.

What is used in Android to compile and execute Java code?

java is compiled by javac (Java compiler). Javac compiles Java source file into Java byte-code file as TestClass. class.


1 Answers

ImagePlayground is an open source Android app that does this using Dexmaker and a custom programming language.

like image 156
Jesse Wilson Avatar answered Oct 06 '22 00:10

Jesse Wilson