Since I want to write applications for Android in a dynamic language, I decided to experiment with embedding Rhino in an Android app. I created a new Android Java project, grabbed the Rhino jar-file from the ASE web site and dropped it in the libs directory. By browsing the documentation at the Rhino web site, it was quite straightforward to write a working demo.
Here is the code:
public class JSApp extends Activity
public void onCreate(Bundle savedInstanceState)
"var widgets = Packages.android.widget;\n" +
"var view = new widgets.TextView(TheActivity);\n" +
void doit(String code)
// Create an execution environment.
Context cx = Context.enter();
// Turn compilation off.
// Initialize a variable scope with bindnings for
// standard objects (Object, Function, etc.)
Scriptable scope = cx.initStandardObjects();
// Set a global variable that holds the activity instance.
scope, "TheActivity", Context.javaToJS(this, scope));
// Evaluate the script.
cx.evaluateString(scope, code, "doit:", 1, null);