在 Android 上运行时将字符串编译为代码 [英] Compiling String as Code during Runtime on Android

查看:38
本文介绍了在 Android 上运行时将字符串编译为代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试在 Android 上的 Janino 上使用一些东西,但得出的结论是它们根本无法在 Dalvik VM 中运行.

I have tried using things on Janino on Android and have concluded that they simply do not work within the Dalvik VM.

所以我只想问一个简单的问题:在 Android 上,是否可以在运行时编译包含代码的字符串以在应用程序中使用.如果是这样,是否有任何库可以让我这样做和/或您可以分享如何执行此操作的代码示例?

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?

对于(一个非常简单的) 示例,如果我有一个包含以下内容的 String 对象:

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;}

}

作为一根巨大的字符串.有没有一种方法可以处理它以创建 Adder 对象的实例,以便我可以通过反射 API 调用 add() 方法?

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?

编辑我试过 beanshell 解释,但事实证明它太慢了.我正在寻找更快的东西,就像 Janino

Edit I've tried beanshell interpretation but it proved to be too slow. Im looking for something a little faster, just like Janino

推荐答案

ImagePlayground是一款开源 Android 应用,它使用 Dexmaker 和自定义编程语言来实现这一点.

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

这篇关于在 Android 上运行时将字符串编译为代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆