Skip to content

Instantly share code, notes, and snippets.

@aveuiller
Created September 21, 2017 08:50
Show Gist options
  • Save aveuiller/9cc1a56ea8f75db8732521d8c250e27a to your computer and use it in GitHub Desktop.
Save aveuiller/9cc1a56ea8f75db8732521d8c250e27a to your computer and use it in GitHub Desktop.
Configuration example to make script engine work with Android
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "test.app"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'io.apisense:rhino-android:1.0' // Includes ready to use rhino implementation with JSR223
}
package test.app;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Test extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String result = scriptMethod();
Log.i("TEST", result);
}
private String scriptMethod() {
return execute("3;");
}
public String execute(String s) {
Object result = null;
String engineName = "js";
ScriptEngineManager manager = new ScriptEngineManager();
Log.i("TEST", "using Script engine name: " + engineName);
ScriptEngine engine = manager.getEngineByName(engineName);
if (engine == null) {
throw new UnsupportedOperationException("Engine not found: " + engineName);
}
Log.i("TEST", s);
try {
result = engine.eval(s);
return result.toString();
} catch (Exception e) {
Log.e("e", e.toString());
throw new RuntimeException(e);
}
}
}
@Soft0Tech
Copy link

Not working

@Normankita
Copy link

Ok, I am still having a hard time with ScriptEngine, it tells me that it can not be resolved, any clue on how to solve that ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment