11

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

0

3 Answers 3

9

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

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

Comments

7

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.

5 Comments

Ive looked through its documentation but have no clue how I would be able to use it to compile a string of code as code. From what it seems (at least in the hello world example), I wont be able to use it to actually compile Strings. I emphasize the string part because the user needs to create a mathematical expression that needs to be compiled and evaluated to a double.
Yikes, dynamically compiling user entered data sounds like a tough nut to crack. You'd have to parse that string for pieces to construct and "equation" method of sorts using dexmaker. ie "2 * 4 / 6" You could definitely use dexmaker to dynamically construct a method and then run it, but it'd be pretty error prone to try and parse on the fly like that. If you had access to a Java->JS bridge you might be able to use something like mathjs(mathjs.org) but even it only would evaluate what was fed into it, not parse it.
Yep, its been bugging me for a week now. Ive been through everything from compilers to expression evaluators to scripting languages and everything was either too slow or didn't work. I haven't lost hope yet though, Ill find a way
Have you found a solution @sourdesi? I've been facing the same challenge. I need to add code on the fly that contain some expressions and modify variables (int or double).
@MarcoAltran Yeah I just ended up using some of the code from ImagePlayground as the accepted answer suggests. I emailed the creator of the project whether it was ok to use his implementation and he was fine with it. You should check it out, its a pretty comprehensive interpreter which uses Antlr to create an abtract syntax tree, and then Dexmaker to compile it. Im not sure whether it would still be compatible with current versions of android since its been switched from the Dalvik VM to ART. Worth looking into.
2

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.

3 Comments

Why do you say it isn't possible?
Apparently I am wrong. But wouldn't the ability to generate and run code at runtime have severe security implications? Would be very interested in knowing what Android does to mitigate any potential problems.
The Android security model is based on processes as the unit of trust and isolation. As such, it doesn't matter that a process can load new code, per se. This is why native (non-VM) application code on Android does not change the security properties of the system. The security risk is if you can trick a system process (that is, a privileged process) into running bad code. However, that has nothing to do with the VM per se (since the bad code might be native code, for example).

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.