在Android上使用Rhino的问题

我正在尝试在Android的Java应用程序中使用Mozilla

Rhino评估一些JavaScript。我正在使用Eclipse + ADT插件。

首先,我尝试仅从Mozilla的网站下载Rhino

.jar文件,并将其作为Eclipse中的库添加到项目中。Eclipse很好地识别了它并编译了该应用程序。但是,在运行它时,调用时会出现异常Context.evaluateReader()(有关堆栈跟踪,请参见下文)。

然后,我尝试将Rhino源代码添加为Eclipse中的一个单独的Android项目,将其标记为一个库并在我的项目中对其进行引用,这足以使Eclipse对其进行编译,但导致了相同的错误。

这是我得到的堆栈跟踪(java.lang.UnsupportedOperationException: can't load this type of

class file

Thread [<7> Thread-8] (Suspended (exception UnsupportedOperationException)) 

DefiningClassLoader(ClassLoader).defineClass(String, byte[], int, int, ProtectionDomain) line: 338

DefiningClassLoader.defineClass(String, byte[]) line: 62

Codegen.defineClass(Object, Object) line: 159

Codegen.createScriptObject(Object, Object) line: 114

Context.compileImpl(Scriptable, Reader, String, String, int, Object, boolean, Evaluator, ErrorReporter) line: 2440

Context.compileReader(Reader, String, int, Object) line: 1326

Context.compileReader(Scriptable, Reader, String, int, Object) line: 1298

Context.evaluateReader(Scriptable, Reader, String, int, Object) line: 1137

TimetableProcessor.evaluate(InputStream, String, String[]) line: 31

TimetableProcessor.processBasicData(InputStream, String) line: 58

TimetableProcessor.process(InputStream, String) line: 52

TimetableUpdater.update() line: 53

Main$1$1.run() line: 22

我的代码中出现异常的部分看起来像这样:

        Context cx = Context.enter();

cx.setLanguageVersion(Context.VERSION_1_7);

Scriptable scope = cx.initStandardObjects();

try {

Object result = cx.evaluateReader(scope, new InputStreamReader(data), /* <<< exception here */

filename, 0, null);

} catch (IOException e) {

// ...

}

我还发现了此博客文章,其中包含类似的代码,并说它可行。作者说他使用了来自Android脚本站点的jar文件。我找到的唯一jar文件位于rhino_extras_r3.zip。但是,它不包含.class文件,而是一个classes.dex文件。当我在Eclipse中将其添加为库时,它无法识别其中包含的类,因此由于缺少对Rhino类的引用而无法编译我的项目。

感谢您提供任何有关如何使其正常工作的帮助!

回答:

我终于得到它的工作。我应该更加关注我链接的那篇博客文章。

如果我添加线

cx.setOptimizationLevel(-1);

要禁用优化,一切都会正常进行。

以上是 在Android上使用Rhino的问题 的全部内容, 来源链接: utcz.com/qa/426994.html

回到顶部