sourdesi
sourdesi

Reputation: 380

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

Upvotes: 11

Views: 5800

Answers (3)

Durandal
Durandal

Reputation: 5663

You could take a look at dexmaker: https://github.com/crittercism/dexmaker

It seems to be an Android friendly equivalent to ASM or cglib; it generates .dex files instead of .class files.

Upvotes: 7

Jesse Wilson
Jesse Wilson

Reputation: 40595

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

Upvotes: 9

Kai
Kai

Reputation: 15476

Basically you want a Java/Dalvik compiler that you can invoke programmically, similar to Java's javax.tools. The short answer is that it's not possible current.

Upvotes: 2

Related Questions