Monday, June 8, 2009 at 12:52 PM
The Android Scripting Environment (ASE) brings scripting languages to Android by allowing you to edit and execute scripts and interactive interpreters directly on the Android device. These scripts have access to many of the APIs available to full-fledged Android applications, but with a greatly simplified interface that makes it easy to:- Handle intents
- Start activities
- Make phone calls
- Send text messages
- Scan bar codes
- Poll location and sensor data
- Use text-to-speech (TTS)
- And more
Scripts can be run interactively in a terminal, started as a long running service, or started via Locale. Python, Lua and BeanShell are currently supported, and we're planning to add Ruby and JavaScript support, as well.

Scripts can be edited directly on the phone.

The script manager displays available scripts.

Interactive terminals can be started for interpreters that support it.

Scripts can use the Android UI to get user input.
You may ask, why write scripts instead of real Android applications? Admittedly, Android's development environment makes life pretty easy, but you're tied to a computer to do your work. ASE lets you develop on the device itself using high-level scripting languages to try out your idea now, in the situation where you need it, quickly. Have a look at the following example Lua script to see for yourself:
--Placing the phone face down will disable the ringer. Turning it face up again will enable
--the ringer.
require "android"
android.startSensing()
android.sleep(1) --Give the sensors a moment to come online.
silent = false
while true do
s = android.readSensors()
facedown = s.result and s.result.zforce and s.result.zforce > 9
if facedown and not silent then
android.vibrate() --A short vibration to indicate we're in silent mode.
android.setRingerSilent(true)
silent = true
elseif not facedown and silent then
android.setRingerSilent(false)
silent = false
end
android.sleep(1)
end
Here's another useful script, this time in Python.
"""Say chat messages aloud as they are received."""
import android, xmpp
_SERVER = 'talk.google.com', 5223
class SayChat(object):
def __init__(self):
self.droid = android.Android()
username = self.droid.getInput('Username')['result']
password = self.droid.getInput('Password')['result']
jid = xmpp.protocol.JID(username)
self.client = xmpp.Client(jid.getDomain(), debug=[])
self.client.connect(server=_SERVER)
self.client.RegisterHandler('message', self.message_cb)
if not self.client:
print 'Connection failed!'
return
auth = self.client.auth(jid.getNode(), password, 'botty')
if not auth:
print 'Authentication failed!'
return
self.client.sendInitPresence()
def message_cb(self, session, message):
jid = xmpp.protocol.JID(message.getFrom())
username = jid.getNode()
text = message.getBody()
self.droid.speak('%s says %s' % (username, text))
def run(self):
try:
while True:
self.client.Process(1)
except KeyboardInterrupt:
pass
saychat = SayChat()
saychat.run()
These scripts demonstrates several of the available APIs available for both Lua and Python. It is intended to be run as a service and silences the ringer when the phone is placed face down. For some scripting languages, like BeanShell, it's possible to access Android's Java API directly. To simplify things, ASE provides the AndroidFacade class. For other languages, like Python and Lua, the API is made available via JSON RPC calls to a proxy. Naturally this means that only the part of the API which has been wrapped by the AndroidFacade and AndroidProxy are available to cross-compiled interpreters like Python and Lua. Thankfully, both AndroidFacade and AndroidProxy are simple to extend.
If you'd like to give ASE a try, it's not yet published to the Market, but will be soon. You can download the latest APK from our project page. Some sample scripts and documentation are also included there to help you get started. We always love to hear what you think, so please send us feedback or ask your questions in the ASE discussion group.


35 comments:
Folks - this *really* needs to be installable to the SD card. No joke. It's huge.
Any plans for Perl support? Please!
Awesome! As a developer and end-user -- thanks a lot guys! I'm sure this will increase the no. of useful applications for the Android platform.
Clojure would be awesome as well.
This is exciting!
However after briefly going through this post (and other links) its not clear... has Jython been ported to Android? Or is this a native port?
This is all well and good but how about ensuring that JVM languages such as JRuby, Jython, Rhino, and Clojure may be used to their fullest extent on the platform? At the moment, the former 3 exception out when reflecting on any class in android.*. Resolving this would greatly expedite application development and also potentially enlist those of us who are less than thrilled to be coding in plain Java.
Nice one. Any chance of support for Groovy (or Java Scripting/BSF)?
Looking forward to testing this out.
Is there a chance we could see more coverage of the framework in the scripting languages ?
Very cool. But it seems you're advertising this as a way of quickly testing things on the device itself. Will I be able to create distributable APKs from Python scripts so I can put full-fledged apps on the Market?
VERY exciting. Looking forward to the ruby support and hoping one will be able to call the java apis like jruby. Also, I love the idea of being able to code while waiting for my wife to finish shopping. :)
Lua on android is simply awesome! Finally prototyping will be as easy as cake, thanks guys!
Great work guys! My HTC Dream just got a lot more interesting.
Just because I'm not sure I got it right. Will it also be possible to build .apks with Python or any of those other languages? (I ask because my English is as far from perfect as my Java is...)
cowmix, read the FAQ:
How does ASE work?
For some scripting languages, like BeanShell, it's possible to access Android's Java API directly. The AndroidFacade is available for use by these languages for convenience. For other languages, like Python and Lua, the API is made available via JSON RPC calls to a proxy. Only the part of the API which has been wrapped by the AndroidFacade and AndroidProxy are available to cross-compiled interpreters like Python and Lua.
I want to port an existing Python application to Android. This application has a clean core/UI separation. Does ASE mean I can reuse the Python core library, and just rewrite the UI for Dalvik?
But it will support a real *nix env? with my own shell, editors, compilers..? Or we will be tied to the tools google chooses?
Anyway, I just hope this platform do not die!!
That's it, I just ordered an HTC Magic. I'll start digging in to the Python-Android docs while waiting for the phone.
This is brilliant . You have suddenly welcomed all the sandle wearing, bearded, opensource types like myself. Thank you
Why will anyone want JRuby, Jython, Rhino, Clojure or Groovy on Android? Are they delirious?
You CAN NOT program Android in "plain Java". If we are talking about non-trivial program, of course. All Java niceties should be thrown out of the window if you want decent speed. Instead of array of objects you must use arrays of plain ints (with appropriate calculations in methods) - or the speed will be horrible. Avoid interfaces. Avoid fields (use local variables instead). And so on. And all hight-level JVM languages are even slower thus even more pointless. JIT hides a lot of that slowness and makes all these high-level JVM-languages usable on PC, but Android does not have JIT! If Google will introduce JIT at some point then and only then we can start talking about "other JVM languages"...
For small scripts Python is more then enough, complex things must be done in "crippled Java" (see above) anyway. C/C++ support will make significantly more sense then JRuby or Clojure at this point of time.
The Python in the screenshot is a native Python---it claims to have been compiled with GCC.
Please add Scala.
Firstly .. WOW!
I started to TRY and learn JAVA via Eclipse & GWT but the help was appalling for someone without low level programming experience.
Python is a fantastic introduction to the Android platform and really will accelerate development of some funky apps.
I just need to get me know now!
Yes Javascript Yes!
Well, I'll admit that I'm pretty stoked about finally getting to write android apps in Python, but JSON? That's gotta be slow. It's pretty easy to get JNI and the Python C API to work together- any chance of that happening, or should I just continue to write my own if I need the performance?
Where is perl ???
Why ASE support Lua, BSH, but not Perl?
Did you guy understand what user really need in fact?
Apologies, seems that I had changed the hostname without updating /system/etc/hosts, and so it was getting an UnknownHostException resolving the local hostname.
-- Hazel
I installed ASE 0.7 alpha from the link on the Google Code project page using the browser on my G1, but when I try to run any of the Python scripts, it attempts to set AP_PORT to 0, mumbles about prefixes and generally fails.
See the screenshot of the error messages I grabbed with DDMS.
-- Hazel
I saw this line in the code that speaks your chat messages:
auth = self.client.auth(jid.getNode(), password, 'botty')
Who's password is botty? Did you mean to include that? :)
I can connect to my own (non-Gtalk) XMPP server now. What xmpp lib is this?
This is very impressive! You just convinced me I need to buy an Android handset. :)
So, uh, any chance you might add PHP in the future??
@cowmix The Lua and Python interpreters are the C variants that have
been cross compiled for the Android platform.
@WadeMealing Adding support for additional Android APIs is pretty
straight forward. I expect the set of supported APIs to grow as the
needs for those that are unsupported become clear.
@andy My current priority is reducing the size of the install in
various ways. The most significant change will be making it possible
to install interpreters À la carte.
@davorg @TonyHursh @ukdavo @Oyo @howa @alexxisr The issues page lists planned
enhancements including new language support. There are a lot of people
like you that would like to see Perl and other languages added to ASE.
@jfedor @Marcus There are not currently plans to support building full
APKs from scripts. However, it would not be difficult to modify ASE to
be a base for distributing an application in that fashion.
@EvanLight Unfortunately, I'm not familiar with the issues you
describe. However, I can say that it is outside the scope of this
project to patch all JVM-based scripting languages to work with
Dalvik/Android.
@andyskelton The xmpppy package is available to Python scripts.
@patternpusher botty is not a password. It's a resource identification.
@geremycondra First, I would say that most of the Android API that's
exposed by JSON RPC is not performance critical. Secondly, using JSON
RPC makes it trivial to add support for additional languages. Using
JNI would require extensive work to add support for each new cross
compiled language. Lastly, the focus of ASE is not on replacing the
Android development environment. I would definitely suggest using the
official Android development tools if you are concerned about
performance characteristics.
@Tom Scala is not a scripting language and it is already possible to
build Android applications using Scala. See
http://www.scala-lang.org/node/160
@Nicolas_Raoul The answer to your question depends on a lot of things.
If all the required modules for the project are available to the ASE
version of Python, then yes. Otherwise, it would probably take more
work.
This is very nice. I like the fact that there seem to be some access to the phone's functions exposed to the scripting languages, but it doesn't look like making a full blown application in Python, for example, is possible. I am debating about whether I need that or not. That is what would tip me over between choosing an Android device or a Nokia Maemo device like the N900. With the N900 one can write full blown Python applications using either PyQt or PyGtk as the GUI. Decisions, decisions. ;)
Python on Android - woot!
Can't wait for it to hit the Market
Post a Comment