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

sourdesi
  • 360
  • 1
  • 4
  • 21

3 Answers3

9

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

Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
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.

Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
Durandal
  • 5,575
  • 5
  • 35
  • 49
  • 1
    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. – sourdesi Jul 09 '13 at 01:44
  • 2
    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(http://mathjs.org/) but even it only would evaluate what was fed into it, not parse it. – Durandal Jul 09 '13 at 02:18
  • 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 – sourdesi Jul 09 '13 at 02:32
  • 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). – Marco Altran Feb 16 '15 at 18:55
  • @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](http://www.antlr.org/) to create an abtract syntax tree, and then [Dexmaker](https://github.com/crittercism/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. – sourdesi Feb 17 '15 at 19:26
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.

Kai
  • 15,284
  • 6
  • 51
  • 82
  • 2
    Why do you say it isn't possible? – danfuzz Jul 22 '13 at 02:11
  • 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. – Kai Jul 22 '13 at 05:45
  • 2
    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). – danfuzz Jul 22 '13 at 16:12