For example, in a previous draft of this book, we had a MailBuzz project. MailBuzz, as the name suggests, dealt with email. It leveraged the JavaMail APIs and needed two JavaMail JARs: mail-1.4.jar
and activation-1.1.jar
. With both of those in the libs/
directory, the classpath
told javac
to link against those JARs, so any JavaMail references in the MailBuzz code could be correctly resolved. Then, those JARs were listed, along with the MailBuzz compiled classes, in the task that invokes the dex
tool to convert the Java code into Dalvik VM instructions. Without this step, even though your code may compile, it won’t find the JavaMail classes at runtime and will fail with an exception.
As it turned out, though, the Dalvik VM and compiler supplied with the Android 0.9 and newer SDKs no longer supported some Java language features used by JavaMail. And, while the JavaMail source code is available, it is under an open-source license (Common Development and Distribution License; CDDL) that… has issues.
Unlike other mobile-device operating systems, Android has no restrictions on what you can run on it, so long as you can do it in Java using the Dalvik VM. This includes incorporating your own scripting language into your application, something that is expressly prohibited on some other devices.
One possible Java scripting language is BeanShell. [26] http://beanshell.org
BeanShell gives you Java-compatible syntax with implicit typing and no compilation required.
So, to add BeanShell scripting, you need to put the BeanShell interpreter’s JAR file in your libs/
directory. The 2.0b4 JAR available for download from the BeanShell site, unfortunately, does not work out of the box with the Android 0.9 and newer SDKs, perhaps due to the compiler that was used to build it. Instead, you should probably check out the source code from Subversion [27] http://beanshell.org/developer.html
and execute ant jarcore
to build it, then copy the resulting JAR (in BeanShell’s dist/
directory) to your own project’s libs/
. Or just use the BeanShell JAR that accompanies the source code for this book, in the Java/AndShell
project available in the Source Code area at http://apress.com.
From there, using BeanShell on Android is no different from using BeanShell in any other Java environment:
1. Create an instance of the BeanShell Interpreter
class.
2. Set any globals for the script’s use via Interpreter#set()
.
3. Call Interpreter#eval()
to run the script and, optionally, get the result of the last statement.
For example, here is the XML layout for the world’s smallest BeanShell IDE:
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:id="@+id/eval"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Go!"
/>
android:id="@+id/script"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:singleLine="false"
android:gravity="top"
/>
Couple that with the following activity implementation:
packagecom.commonsware.android.andshell;
importandroid.app.Activity;
importandroid.app.AlertDialog;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.Toast;
importbsh.Interpreter;
public classMainActivity extendsActivity {
privateInterpreter i = new Interpreter();
@Override
publicvoid onCreate(Bundle icicle) {
super. onCreate(icicle);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.eval);
finalEditText script = (EditText) findViewById(R.id.script);
btn. setOnClickListener( newView. OnClickListener() {
publicvoid onClick(View view) {
String src = script. getText(). toString();
try{
i. set("context", MainActivity. this);
i. eval(src);
} catch(bsh.EvalError e) {
AlertDialog.Builder builder =
newAlertDialog. Builder(MainActivity. this);
builder
. setTitle("Exception!"). setMessage(e. toString())
. setPositiveButton("OK", null). show();
}
}
});
}
}
Compile and run it (including incorporating the BeanShell JAR as mentioned earlier), and install it on the emulator. Fire it up, and you get a trivial IDE with a large text area for your script and a big Go! button (see Figure 21-1) to execute it.
Figure 21-1. The AndShell BeanShell IDE
importandroid.widget.Toast;
Toast. makeText(context, "Hello, world!", 5000). show();
Note the use of context
to refer to the activity when making the Toast
. That is the global set by the activity to reference back to itself. You could call this global variable anything you want, so long as the set()
call and the script code use the same name.
When you click the Go! button, you get the result shown in Figure 21-2.
Figure 21-2. The AndShell BeanShell IDE, executing some code
And now, some caveats.
First, not all scripting languages will work. For example, those that implement their own form of just-in-time (JIT) compilation, generating Java bytecodes on the fly, would probably have to be augmented to generate Dalvik VM bytecodes instead of those for stock Java implementations. Simpler languages that execute from parsed scripts, calling Java reflection APIs to call back into compiled classes, will likely work better. Even there, though, not every feature of the language may work if the feature relies upon some facility in a traditional Java API that does not exist in Dalvik — for example, there could be stuff hidden inside BeanShell or the add-on JARs that does not work on today’s Android.
Читать дальше