Getting Started with the Scripting Layer for Android (SL4A, Formerly Android Scripting Environment)
You want to write your application in one of several popular scripting languages, or, you want to program interactively on your phone.
Here's how to get started:
- Download the Scripting Layer for Android (formerly Android Scripting Environment) from http://code.google.com/p/android-scripting/
- Add the interpreter(s) you want to use
- Type in your program
- Run it immediately - no compilation or packaging steps needed!
The SL4A application is not (yet) in the Android Market, so you have to visit the web site and download it (there is a QRCode for downloading, so start in your laptop or desktop browser). And since it's not in the market, before you can download it you'll have to go into Settings->Applications->Unknown Sources and enable unknown-sourced applications. Also note that since this is not downloaded via the Market, you will not be notified when the Google project releases a new binary.
Once you have the SL4A binary installed, you must start it and download the particular interpreter you want to use. The following are available as of this writing:
- Unix shell
Some of the interpreters (like JRuby) run in the Dalvik VM, while others (like Python) run the "native" versions of the language under Linux on your phone. Communication happens via a little server that is started automatically when needed or can be started from the Interpreters menu bar.
The technique for downloading new interpreters is a bit sub-obvious. When you start the SL4A application it shows a list of scripts, if you have any. Press the menu button, then go to the View menu, and select Interpreters (while here notice that you can also view the Logcat, the system exception log file). From the Interpreters list, pressing Menu again will get you a menu bar with an Add button, and this lets you add another interpreter.
Pick a language (Python)
Suppose you think Python is a great language (which it is).
Once your interpreter is installed, go back to the SL4A main page and click the Menu button, then Add (in this context, Add creates a new file, not another interpreter). Select the installed interpreter and you'll be in Edit Mode. We're trying Python, so type in this canonical "hello world" example:
droid = android.Android()
Press the Menu button, and "Save and Run" if enabled, or "Save and Exit" otherwise. The former will run your new app; the latter will return you to the list of scripts, in which case you want to tap your script's name. In the resulting pop-up, the choices are (left to right):
- Run ("DOS box" icon)
- Edit ("pencil" icon)
- Save ("1980 floppy disk icon")
- Delete (trash can icon).
If you long-press a file name, a pop-up gives you the choice of Rename or Delete.
When you run this trivial application, you will see the Toast near the bottom of your screen.
If you want to keep your scripts in a source repository, and/or if you prefer to edit them on a laptop or desktop with a traditional keyboard, just copy the files back and forth (if you phone is rooted, you can probably run your repository directly on the phone). Scripts are stored in "sl4a/scripts" on the SD card. If you have your phone mounted on your laptop's /mnt folder, for example, you might see this (On Windows it might be E: or F: instead of /mnt):
laptop$ ls /mnt/sl4a/
Shell.log hello_world.py.log phonepicker.py.log say_weather.py.log speak.py.log
demo.sh.log ifconfig.py.log say_chat.py.log scripts take_picture.py.log
dialer.py.log notify_weather.py.log say_time.py.log sms.py.log test.py.log
laptop$ ls /mnt/sl4a/scripts
bluetooth_chat.py dialer.py hello_world.py notify_weather.py say_chat.py say_weather.py speak.py test.py weather.pyc
demo.sh foo.sh ifconfig.py phonepicker.py say_time.py sms.py take_picture.py weather.py
The official web site is ; there is a QRCode there to download the latest binary.