On Android-Studio you are building your project with the Android framework. The JSR223 is not embedded in Android's implementation of Java by default.
But you can register them with a library.
For example with https://github.com/APISENSE/rhino-android which is embedding rhino
, the javascript engine from Java 7 into Android. (disclaimer: I maintain this library)
Edit ensuing your comments
I tried to reproduce your behavior, here is what I have:
When using rhino as ScriptEngine
name:
09-20 11:13:16.151 5997-5997/? I/TEST: using Script engine name: rhino
09-20 11:13:16.159 5997-5997/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: test.app, PID: 5997
java.lang.NoClassDefFoundError: com.sun.script.javascript.ExternalScriptable
at com.sun.script.javascript.RhinoScriptEngine.<init>(RhinoScriptEngine.java:43)
at com.sun.script.javascript.RhinoScriptEngineFactory.getScriptEngine(RhinoScriptEngineFactory.java:51)
at javax.script.ScriptEngineManager.getEngineByName(ScriptEngineManager.java:154)
at test.app.Test.execute(Test.java:33)
at test.app.Test.scriptMethod(Test.java:23)
at test.app.Test.onCreate(Test.java:17)
[...]
When using JavaScript as ScriptEngine
name:
09-20 11:16:16.883 8776-8776/? I/TEST: using Script engine name: JavaScript
09-20 11:16:16.883 8776-8776/test.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: test.app, PID: 8776
java.lang.RuntimeException: Unable to start activity ComponentInfo{test.app/test.app.Test}: java.lang.UnsupportedOperationException: Engine not found: JavaScript
[...]
Caused by: java.lang.UnsupportedOperationException: Engine not found: JavaScript
at test.app.Test.execute(Test.java:37)
at test.app.Test.scriptMethod(Test.java:23)
at test.app.Test.onCreate(Test.java:17)
[...]
As you can see rhino is trying to instanciate a ScriptEngine
but a class throw an error when loaded (com.sun.script.javascript.ExternalScriptable
).
The thing is that your jar dependency contains the JSR223 and some engine definition but does not contain the actual script engine.
To correct this situation you may want to add an actual rhino implementation to your dependencies.