犀牛安卓版

Beh*_*ehy 3 android rhino

我正在尝试在我的 android 项目中使用rhino。我黎明加载了 Rhino 并添加js.jarlib.
\n这是我的MainActivity

\n\n
public class MainActivity extends AppCompatActivity {\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        ...\n\n        Context rhino = Context.enter();\n//        try{                     // I commented it to catch the error\n            rhino.setLanguageVersion(Context.VERSION_1_2);\n            Scriptable scope = rhino.initStandardObjects();\n            Object result=rhino.evaluateString(scope, \n                           "obj={a:1,b:[\'x\',\'y\']}", "MySource", 1, \n                            null);       // This line cannot be compiled\n\n            Scriptable obj = (Scriptable)scope.get("obj",scope);\n            Log.i("JS","obj " + (obj == result ? "==" : "!=") +" result");\n\n            Log.i("js","obj.a == " + obj.get("a", obj));\n\n\n            Scriptable b = (Scriptable) obj.get("b", obj);\n//        }catch (Exception e){\n//            Log.e("js","Exception.....");\n//        }\n     }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

日志猫:

\n\n
FATAL EXCEPTION: main\n    Process: com.behy.jsinand, PID: 7027\n    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\n    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)\n    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)\n    at android.app.ActivityThread.access$800(ActivityThread.java:151)\n    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)\n    at android.os.Handler.dispatchMessage(Handler.java:102)\n    at android.os.Looper.loop(Looper.java:135)\n    at android.app.ActivityThread.main(ActivityThread.java:5254)\n    at java.lang.reflect.Method.invoke(Native Method)\n    at java.lang.reflect.Method.invoke(Method.java:372)\n    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)\n    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)\n    Caused by: java.lang.UnsupportedOperationException: can\'t load this type of class file\n    at java.lang.ClassLoader.defineClass(ClassLoader.java:300)\n    at org.mozilla.javascript.DefiningClassLoader.defineClass(DefiningClassLoader.java:27)\n    at org.mozilla.javascript.optimizer.Codegen.defineClass(Codegen.java:130)\n    at org.mozilla.javascript.optimizer.Codegen.createScriptObject(Codegen.java:85)\n    at org.mozilla.javascript.Context.compileImpl(Context.java:2394)\n    at org.mozilla.javascript.Context.compileString(Context.java:1335)\n    at org.mozilla.javascript.Context.compileString(Context.java:1324)\n    at org.mozilla.javascript.Context.evaluateString(Context.java:1076)\n    at com.behy.jsinand.MainActivity.onCreate(MainActivity.java:41)\n    at android.app.Activity.performCreate(Activity.java:5990)\n    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)\n    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)\n    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)\xc2\xa0\n    at android.app.ActivityThread.access$800(ActivityThread.java:151)\xc2\xa0\n    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)\xc2\xa0\n    at android.os.Handler.dispatchMessage(Handler.java:102)\xc2\xa0\n    at android.os.Looper.loop(Looper.java:135)\xc2\xa0\n    at android.app.ActivityThread.main(ActivityThread.java:5254)\xc2\xa0\n    at java.lang.reflect.Method.invoke(Native Method)\xc2\xa0\n    at java.lang.reflect.Method.invoke(Method.java:372)\xc2\xa0\n    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)\xc2\xa0\n    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)\xc2\xa0\n
Run Code Online (Sandbox Code Playgroud)\n\n

它完全基于示例项目,但我不明白为什么它不能编译。

\n

Beh*_*ehy 5

我修复了它(并发布答案也许对您有帮助)
问题中链接的项目不适用于 Android 应用程序。
要在 android 中编译它,您应该将其设置optimizationLevelContext-1rhino.setOptimizationLevel(-1);

它有效!