import android.widget.Toast;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public void bEqual(View v) throws ScriptException {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String value = inputText.getText().toString();
Toast.makeText(this,value,Toast.LENGTH_LONG).show();
try{
result = (double)engine.eval(value);
Toast.makeText(this,String.format("%f",result),Toast.LENGTH_SHORT).show();
}catch(Exception e){
Toast.makeText(this,"Exception Raised",Toast.LENGTH_SHORT).show();
}
}
What is wrong in it? App is exiting when perform this action. It is not showing any errors but app is closing
javax.script not available in android that's why you have to use third party library
I am using "rhino-android" and it serve the purpose
https://github.com/APISENSE/rhino-android
implementation in gradle file
java code
Easiest way is to use Mozilla Rhino in Android instead[1] because ScriptEngine and it's dependencies would require time consuming setup and headache.
Import the ff:
Code you put somewhere in your method:
Add it in your gradle dependencies.
Reference:
first add this: compile 'io.apisense:rhino-android:1.0' to your app gradle file. Then type this snippet code
This is a real example using Rhino Script Engine in Android in Android
Use this imports:
and this is an example:
Output:
The
javax.script
package is not available on Android.If you want to use a Javascript engine, you must implement those behaviours by yourself or use one of the dependencies that already exist like rhino-android.