Scripting Android by Mike Riley

by Mike on 06-30-2009 11:23 AM - last edited on 06-30-2009 11:23 AM

When I wrote my first Android development article for Dr. Dobbs Journal earlier this year, one of Android's missing pieces I identified was the operating system's inability to execute well-established scripting languages such as Python or Ruby. While an open source attempt was underway to convert the pure Java version of Python, known as Jython, to the Android platform, that effort had stalled. I was dismayed to think that due to Android's architectural design and the lock-out that non-rooted phones would never have Python scripts run on the platform.

All that changed earlier this month with Google employee Damon Kohler's release of his Android Scripting Environment (ASE). Damon converted a popular scripting shell written by Java developer Pat Niemeyer called Beanshell. This environment was selected for several reasons, many of which have already been advertised on the Beanshell website. To quote from the summary of features:



  • Dynamic execution of the full Java syntax, Java code fragments, as well as loosely typed Java and additional scripting conveniences.
  • Transparent access to all Java objects and APIs.
  • Runs in four modes: Command Line, Console, Applet, Remote Session Server.
  • Can work in security constrained environments without a classloader or bytecode generation for most features.
  • The interpreter is small ~150K jar file.
  • Pure Java.
  • It's Free!!


In addition to running Beanshell (.bsh) scripts, DamoDamonn also bundled Android-compiled versions of Python v2.6 and Lua v5.1.4 with the package, effectively delivering Python and Lua interpreters for the Android platform. Damon also included special libraries for accessing Android-specific features, such as the ability to call Intents, activate phone features like placing a call and even access other applications like the eyes-free Text-to-Speech (TTS) package and 1D/2D barcode libraries. Beanshell, Lua and Python scripts are included in the .apk installation bundle demonstrating how the 'android' object can be easily instantiated and call upon such functions in script form.



While Damon has promised that the ASE will be available for download from the Android Market, it was exclusively available from the ASE website as of this writing. The version tested for this article was the 0.7 release.


Running a Python Script on Android

Obtaining and installing ASE is straightforward - download the .apk file from the Google Code ASE website, or if you're not in too much of a hurry, wait until ASE shows up in the Android Market and obtain it via the usual means. If installing the apk file, use the Android SDK and run the 'apk install' command from your computer's terminal. Be prepared, however, for a bit of a storage shock. The version discussed in this article consumed a whopping 12 MB on the device - ouch! I had to install it twice, due to immediately receiving a low storage alert with the initial install. It hurt to have to uninstall some treasured Android applications to free up space (including arcade favorite Pac Man), but until ASE has an option to the SD card (or at least store the Python and Lua runtimes and dependent libraries there), ASE users will have to bow to ASE's steep storage requirements.

 


appusage

Figure 1. The ASE consumes a whopping 12 MB of device storage space.

 

Once installed, activating the ASE is as simple as touching the ASE icon. The opening screen lists demo/starter scripts installed with the setup along with any custom scripts you may have added. From there, scripts can be selected for editing, deleting or, if properly constructed, run as a service (very cool!).


homescreen

Figure 2. The ASE program icon is shown in the left column, middle row.


While one would hope adding your own scripts to this list would be as intuitive as selecting .bsh, .lua or .py files from a designated directory on the SD card, the current version does not support such easy access. Instead, scripts have to be copied, pasted and saved within the ASE. The ASE saves these scripts in the device's /data/data/com.google.ase/scripts directory. I suppose for root-level access developer phone models, a symbolic link could point to a designated directory on the SD card for easier script storage and transfer between the phone and the computer it's tethered to, but this is cumbersome and might create a security risk. So until a new version that supports easier script access is made available, the following steps are the approach I use to install, run and test scripts within the ASE:


Install ASE via the 'apk install' Android SDK option (or via the Android Market once ASE is posted there).


Install any of the available Android Text editors available from the Android Market (I prefer Paul Mach's TextEdit program).


  1. Due to the larger screen real estate and much easier input modality, initially author the desired Python script on a computer tethered to the Android phone.
  2. Mount the Android phone's SD card to the tethered computer's desktop.
  3. Copy the Python script to Android's SD card.
  4. Eject the mounted SD card from the computer so that the phone can access the card's contents.
  5. Locate and open the copied script using an Android text editor.
  6. Select All and Copy the script's text into Android's cut & paste buffer.
  7. Launch ASE, press the 'Menu' hardware button and select 'Add' to create a new ASE script.
  8. Paste the contents of the cut & paste buffer into the new script page.
  9. Name the file (remember to keep the .py extension), click the 'Menu' hardware button and 'Save' the file.
  10. Execute the script by selecting the newly saved filename from the list of available scripts.


I have tested several of my own Python scripts and was surprised how quickly the executed on Android, as well as how little effort it took to programmatically wire up Android-specific function calls in my Python scripts. I can easily see how ASE will help me visualize and demonstrate simple GUI app and service ideas before constructing and refining them via the Android SDK.


editscript

Figure 3. Editing a 'talking clock' Python script.
selectscript

Figure 4. Selecting scripts from the main ASE menu.


prompt

Figure 5. Executing the 'talking clock' script with user input.
talkingclock

Figure 6. Running a script within ASE's Python interpretter's terminal window.

Limitations

While the current incarnation of ASE provides a very functional developer playground, there are a few annoyances. The most serious constraint is ASE's current inability to easily install and call upon additional library dependencies that may require a more traditional setup.py install (ex: MySQL libraries, etc.). It suspect it might be possible to copy other .py modules like ID3 and HTML/XML tag parser libraries (such as the awesome Beautiful Soup library), though doing so would require additional root-level cat'ing around to copy the file(s) into ASE's /data/data/com.google.ase/python/lib directory. And naturally, while the Beanshell technology delivers a very good shell environment to run Python, Lua and perhaps soon even Ruby and Perl scripting languages, these executables are still abstracted from the Linux OS lying beneath the application process layer that all Android applications such as ASE are sand-boxed into - great for security and stability, not so great for the curious tinkerer and low-level hacker who wants to control every aspect of the system with relatively simple scripts.


Conclusion

The ASE represents a remarkable attempt to bring the ease of popular scripting languages like Python to the Android OS in a very Android-friendly and relatively secure way. While script performance lags somewhat behind native Android applications and the ASE weighs in heavily while trying to multitask other Android programs, the possibilities for prototyping and simple Android utilities are nearly endless. ASE also makes rapid development on Android a reality, along with near real-time code, execute, debug on a self-conntained smartphone platform. For example, when I wrote a talking clock program using Android's SDK, the time it took to learn the nuances of the SDK, the various Android libraries and the various files that make up an Android apk compared to writing the same functionality in a Python script took minutes instead of hours. For Python scripters on the Android platform, the ASE is a no-brainer. For everyone else, the ASE demonstrates yet another reason why smartphone OS's like Android can compete with more comprehensive, heavier weight operating systems. Check it out!


Mike Riley, an advanced computing professional, can be reached via email at mike@mikeriley.com or followed on Identi.ca or Twitter @mriley.

Comments
by Longlong(anon) on 07-07-2009 10:21 PM
I am looking forward this new system~~
by Roberto C Serrano (ohrock) on 07-09-2009 08:33 AM

Two comments.  If you use the Local app (free in the market) and have ASE installed, it will give you an option to trigger ASE scripts when certaint circumstances happen  (you get home, or to work, etc).

 

Also, if you have the zyxing barcode scanner library installed in your phone (free in the market), you can invoke the barcode scanning from a script, this python code shows how.  Type the following few lines and save them as a .py file:

 

_______________

 

import android

droid = android.Android()

code = droid.scanBarcode()

barcode = int(code['result']['SCAN_RESULT'])

url = "http://books.google.com?q=%d" % barcode

droid.startActivity('android.intent.action.VIEW',url)

 

__________________________________

 

That code will scan a barcode and open your browser and look for the "book" (assuming what you read is a ISBN barcode).

 

If you want to do the same with a movie, you can replace the line that starts with 'url' with the following line:

url = "http://www.dvdaf.com/search.html?upc=%d" % barcode

 

This line will open the browser and point you to the entry for a movie's barcode. From there you canclick in the imd or amazon links.  :smileyhappy:

 

Enjoy!

by Mike on 07-24-2009 04:21 PM
Just a quick follow-up to this article - the Android Scripting Environment has hit 0.10-2-alpha, bringing several notable enhancements. Most importantly, supported language interpreters can be selectively downloaded and installed. This modification significantly reduces the installation footprint. However, due to technical and security issues, the interpreter(s) and their respective libraries still need to be installed in the device's local storage /data/data/com.google.ase/... path. However, scripts themselves are now stored on the SD card, located at /sdcard/ase/scripts. The two main benefits of this change are 1) significantly easier script file access for host computer editing, and 2) custom scripts are no longer deleted when the ASE is removed or upgraded.

While not yet available in this release, plans are afoot to include interpreters for Javascript, Perl, Ruby and perhaps even PHP, making Android fans of those languages a happy bunch as well. With these improvements, ASE is well on its way to becoming *the* RAD development environment for Android tinkerers.
Post a Comment
Be sure to enter a unique name. You can't reuse a name that's already in use.
Be sure to enter a unique email address. You can't reuse an email address that's already in use.
Type the characters you see in the picture above.Type the words you hear.
About MOTODEV Blog
The MOTODEV Blog keeps you updated on mobile app development news from MOTODEV and the Android developer community.

Subscribe to our RSS feed Subscribe via RSS

Follow Us:
Fan MOTODEV on Facebook Join the MOTODEV LinkedIn Group MOTODEV on YouTube

Our Blog & Comment Policy
Opinions expressed here and in any corresponding comments are the personal opinions of the original authors, not of Motorola. The content is provided for informational purposes only and is not meant to be an endorsement or representation by Motorola or any other party.

Remember, when you comment, please stay on topic and avoid spam, profanity, and anything else that violates our user guidelines. All comments require approval by Motorola and can be rejected for any reason.

For customer support issues with your Motorola phone go to the Motorola customer support website.