Problems using Rhino on Android

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

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

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).

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.

This is the stacktrace I get (java.lang.UnsupportedOperationException: can't load this type of class file)

Thread [ 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 

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), /* 

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.

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

回答1:

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

If I add the line

cx.setOptimizationLevel(-1); 

to disable optimisations, everything works perfectly.



回答2:

An Android-compatible edition of rhino1_7R2.jar is available on the SL4A site, in the version control system. Here is a sample project that wraps up Rhino and BeanShell into an Android interpreter service.



回答3:

I've written a library which allows to run Rhino on Android.

Advantages include:

  • Rhino can only run on optimization level -1. The library supports all levels.
  • The library supports the use of JavaAdapter, which is not possible with base Rhino.
  • Altough i did not benchmark this, it should give a performance increase. (Compiled code is faster than interpreted code)

https://github.com/F43nd1r/rhino-android



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!