使用犀牛在Android问题 [英] Problems using Rhino on Android

查看:413
本文介绍了使用犀牛在Android问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Mozilla的犀牛在我的Java应用程序为Android来评估一些JavaScript。我使用的Eclipse + ADT插件。

I'm trying to use Mozilla Rhino in my Java application for Android to evaluate some JavaScript. I am using Eclipse + ADT plugin.

首先我想简单地下载从Mozilla的网站上的犀牛.jar文件,并将其添加到项目作为Eclipse的一个库。 Eclipse中认出这很好,编译的应用程序。但是,在运行时,我打电话时,得到一个异常 Context.evaluateReader()(见下面的堆栈跟踪)。

First I tried simply downloading the Rhino .jar file from Mozilla's website and adding it to the project as a library in Eclipse. Eclipse recognised it fine and compiled the application. However, when running it I get an exception when calling Context.evaluateReader() (see below for stack trace).

然后我试图加入犀牛源$ C ​​$ C作为Eclipse的一个单独的Andr​​oid项目,这标志着它作为一个库,并在我的项目,这是足以让Eclipse来得到它来编译引用它,但导致了同样的错误。

Then I tried adding the Rhino source code as a separate Android project in Eclipse, marking it as a library and referencing it in my project, which was enough to get Eclipse to get it to compile, but led to the same error.

这是我得到的堆栈跟踪( java.lang.UnsupportedOperationException:无法加载此类型的类文件的

This is the stacktrace I get (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

我的code击中异常的位看起来是这样的:

The bit of my code that hits the exception looks like this:

        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) {
            // ...
        }

我还发现这篇博客文章其中包含类似code并说,它的工作原理。笔者说,他用从 Android的脚本网站jar文件。唯一的jar文件,我发现有在 rhino_extras_r3.zip 。但是,它不包含的.class 文件,而是一个 classes.dex 文件。当我说这是Eclipse的一个库,它不承认它包含的类,因而无法编译,因为缺少引用犀牛类。

I also found this blog post which contains similar code and says it works. The author says he used a jar file from the Android Scripting site. The only jar file I found there was in rhino_extras_r3.zip. However, it doesn't contain .class files but rather a classes.dex file. When I added this as a library in Eclipse, it didn't recognise the classes it contains and thus failed to compile my project because of the missing references to Rhino classes.

任何帮助都对如何得到这个工作是AP preciated!

Any help at all on how to get this to work is appreciated!

推荐答案

我终于得到它的工作。我应该更加密切关注该博客文章我联系。

I finally got it to work. I should have paid more close attention to that blog post I linked.

如果我添加了一行

cx.setOptimizationLevel(-1);

禁用的优化,一切完美的作品。

to disable optimisations, everything works perfectly.

这篇关于使用犀牛在Android问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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