Rhino for Android

半腔热情 提交于 2019-12-12 02:59:39

问题


I am trying to use rhino in my android project following this. I dawnloaded Rhino and added js.jar in lib.
Here is my MainActivity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...

        Context rhino = Context.enter();
//        try{                     // I commented it to catch the error
            rhino.setLanguageVersion(Context.VERSION_1_2);
            Scriptable scope = rhino.initStandardObjects();
            Object result=rhino.evaluateString(scope, 
                           "obj={a:1,b:['x','y']}", "MySource", 1, 
                            null);       // This line cannot be compiled

            Scriptable obj = (Scriptable)scope.get("obj",scope);
            Log.i("JS","obj " + (obj == result ? "==" : "!=") +" result");

            Log.i("js","obj.a == " + obj.get("a", obj));


            Scriptable b = (Scriptable) obj.get("b", obj);
//        }catch (Exception e){
//            Log.e("js","Exception.....");
//        }
     }
}

Logcat:

FATAL EXCEPTION: main
    Process: com.behy.jsinand, PID: 7027
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.behy.jsinand/com.behy.jsinand.MainActivity}: java.lang.UnsupportedOperationException: can't load this type of class file
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
    Caused by: java.lang.UnsupportedOperationException: can't load this type of class file
    at java.lang.ClassLoader.defineClass(ClassLoader.java:300)
    at org.mozilla.javascript.DefiningClassLoader.defineClass(DefiningClassLoader.java:27)
    at org.mozilla.javascript.optimizer.Codegen.defineClass(Codegen.java:130)
    at org.mozilla.javascript.optimizer.Codegen.createScriptObject(Codegen.java:85)
    at org.mozilla.javascript.Context.compileImpl(Context.java:2394)
    at org.mozilla.javascript.Context.compileString(Context.java:1335)
    at org.mozilla.javascript.Context.compileString(Context.java:1324)
    at org.mozilla.javascript.Context.evaluateString(Context.java:1076)
    at com.behy.jsinand.MainActivity.onCreate(MainActivity.java:41)
    at android.app.Activity.performCreate(Activity.java:5990)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
    at android.app.ActivityThread.access$800(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

It is complately based on the sample project but I dont undrestand why it doesn't compile.


回答1:


I fixed it (and post the answere maybe it's helpful for you)
The project linked in the question is not for Android application.
To compile it in android, you should set the optimizationLevel of your Context to -1: rhino.setOptimizationLevel(-1);

and it works!




回答2:


I wrote a library which allows using Rhino on Android with no constraints for the optimzation level.

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



来源:https://stackoverflow.com/questions/35767263/rhino-for-android

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