Java has as many third-party libraries as any other modern programming language, if not more. These third-party libraries are the innumerable JARs that you can include in a server or desktop Java application—the things that the Java SDKs themselves do not provide.
In the case of Android, the Dalvik virtual machine (VM) at its heart is not precisely Java, and what it provides in its SDK is not precisely the same as what any traditional Java SDK provides. That being said, many Java third-party libraries provide capabilities that Android lacks natively, and therefore may be of use to you in your projects, if you can get them to work with Android's flavor of Java.
This chapter explains what it will take for you to leverage such libraries and describes the limitations of Android's support for arbitrary third-party code.
You have two choices for integrating third-party code into your project: use the source code or use prepackaged JARs.
If you choose to use the source code, all you need to do is copy it into your own source tree (under src/
in your project), so it can sit alongside your existing code, and then let the compiler perform its magic.
If you choose to use an existing JAR, perhaps one for which you do not have the source code, you will need to teach your build chain how to use the JAR. First, place the JAR in the libs/
directory in your Android project. Then, if you are using an IDE, you probably need to add the JAR to your build path (Ant will automatically pick up all JARs found in libs/
). This is definitely required for Eclipse, where you will need to place a reference to your JARs under the Libraries tab of the Java Build Path page.
And that's it. Adding third-party code to your Android application is fairly easy. Getting it to actually work may be somewhat more complicated, however.
Not all available Java code will work well with Android. There are a number of factors to consider, including the following:
http://json.org
) that are built into Android, but expect a different version of those libraries?One trick for addressing some of these concerns is to use open source Java code and actually work with the code to make it more Android-friendly. For example, if you are using only 10 percent of the third-party library, maybe it's worthwhile to recompile the subset of the project to be only what you need, or at least to remove the unnecessary classes from the JAR. The former approach is safer, in that you get compiler help to make sure you are not discarding some essential piece of code, although it may be more tedious to do.
Unlike other mobile device operating systems, Android has no restrictions on what you can run on it, as 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 (www.beanshell.org/
). BeanShell gives you Java-compatible syntax with implicit typing and no compilation required.
To add BeanShell scripting, you need to put the BeanShell interpreter's JAR file in your libs/
directory. Unfortunately, the 2.0b4 JAR available for download from the BeanShell site 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 Apache Subversion and execute ant jarcore
to build it, and 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.
From there, using BeanShell on Android is no different from using BeanShell in any other Java environment:
Interpreter
class.Interpreter#set()
.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:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/eval"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Go!"
android:onClick="go"
/>
<EditText
android:id="@+id/script"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:singleLine="false"
android:gravity="top"
/>
</LinearLayout>
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.EditText;
importandroid.widget.Toast;
importbsh.Interpreter;
public class MainActivity extends Activity {
private Interpreter i=new Interpreter();
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
}
public void go(View v) {
EditText script=(EditText)findViewById(R.id.script);
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 previously mentioned), 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 to execute it, as shown in Figure 33–1.
importandroid.widget.Toast;
Toast.makeText(context, "Hello, world!", Toast.LENGTH_LONG).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, as long as the set()
call and the script code use the same name.
Click the Go! button, and you get the result shown in Figure 33–2.
READ_CONTACTS
permission, expect any BeanShell scripts your application runs to have the same permission.Since this chapter covers scripting in Android, you may be interested to know that you have options beyond embedding BeanShell directly in your project.
Some experiments have been conducted with other JVM-based programming languages, such as JRuby and Jython. At present, their support for Android is not always 100 percent smooth, but progress is continually being made. Those interested in JRuby on Android, for instance, should investigate the Ruboto open source project, at http://ruboto.org
.
Additionally, Scripting Layer for Android (SL4A), described at http://code.google.com/p/android-scripting/
, allows you to write scripts in a wide range of scripting languages, beyond BeanShell, such as the following:
These scripts are not full-fledged applications, though the SL4A team is working on allowing you to turn them into APK files complete with basic UIs. For on-device development, SL4A is a fine choice. Notable projects developed with SL4A include the Nexus One sensor logging payload. If you're interested in further SL4A reading and development, an excellent book on the topic is Pro Android Python with SL4A, by Paul Ferrill (Apress, 2011).