分享

Problems using Rhino on Android

 quasiceo 2015-07-03

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

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

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!

asked Oct 4 '10 at 21:33
Liquid_Fire
2,86611320

2 Answers

up vote 28 down vote accepted

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.

answered Oct 4 '10 at 22:02
Liquid_Fire
2,86611320

12  
By default, Rhino tries to generate JVM bytecode on the fly, as a sort of Javascript JIT. That's cool and all, but Android does not run JVM bytecode -- it runs Dalvik bytecode. Hence, you have to disable optimization, via the code snippet you showed, to allow Rhino to run. –  CommonsWare Oct 4 '10 at 22:06
    
I confirm this helps :-) –  loki2302 Jul 24 '12 at 10:33
1  
@Listill m face same problem after add this line.. –  Parag Chauhan Oct 27 '12 at 12:39
    
You can use V8 instead of rhino –  Crossle Song May 20 '13 at 4:15
    
Awesome, helped me a lot :) –  Ragunath Jawahar Jun 20 '13 at 18:03

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.

answered Oct 4 '10 at 21:42
CommonsWare
480k5010781136

    
Thanks, I didn't notice that jar file because it wasn't listed in the downloads section. It still didn't solve my problem (I've posted my actual solution), but you definitely deserve an upvote for showing me where the jar file is and the sample project that I can use as an example. –  Liquid_Fire Oct 4 '10 at 22:06
1  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多