1

Here is what I want to achieve:

I am building an Android-Application which needs to be plugin-aware. What I need to do is downloading .java files from a Web-Server and then compile them at runtime within my application and then load it into the classpath. I want to use Java because of the ease of use because I can use the plugin objects just like my stock ones.

I've seen javax.tools's way of compiling but that's not available on DalvikVM. What are the best alternatives to that (that work in a similar way)?

EDIT:

I am using .bsh-script now. This works like a charm on a JVM and should work on an Android device (which I will test next):

package de.hotware.beanshell.test;

import bsh.EvalError;
import bsh.Interpreter;


public class BeanShellTest {

    public static interface InterfaceTest {

        public void sayHello();

    }


    public static void main(String[] args) {
        try {
            Interpreter interpreter = new Interpreter();
            InterfaceTest res = (InterfaceTest) interpreter.eval("import de.hotware.beanshell.test.BeanShellTest.InterfaceTest;" +
                    "new InterfaceTest() {" +
                    "public void sayHello() { System.out.println(\"hello\");}" +
                    "}");
            res.sayHello();
        } catch(EvalError e) {
            e.printStackTrace();
        }
    }

    public void test() {

    }

}
4
  • Cannot you download compiled code from your server? Commented Jun 6, 2013 at 13:43
  • not really. This is a feature meant for non-programmers to build simple modifications to the project. Commented Jun 6, 2013 at 13:48
  • Maybe a scripting language then. Commented Jun 6, 2013 at 13:49
  • I think I will use .bsh-script then. Gives me the same features as normal Java-Code :). Commented Jun 6, 2013 at 15:36

1 Answer 1

3

You need to do two things:

  1. Everything you would do if you were running inside a JVM. Download the source, compile it into .class files with javac or equivalent.
  2. Convert the class files to DEX format. This would mean running the dx program on the device, which is doable, but it can be a bit memory-hungry, especially for larger programs.

Once you've done that, you can use DexClassLoader to load the code from the DEX file.

All things considered, I think you're better off with BeanShell.

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

Comments

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.