Using Scripting Languages in Android

Shubham Soni
Code Yoga
Published in
3 min readMar 13, 2018

At launch, Java was the only officially supported programming language for building distributable APK.Android Native Development Kit (Android NDK) which will allow developers to build Android software components with C and C++.In addition to delivering support for native code, Google is also extending Android to support popular dynamic scripting languages and Google launched the Android Scripting Environment (ASE) but now it is Open Source and Known as Scripting Layer for Android(SL4A brings scripting languages to Android by allowing you to edit and execute scripts and interactive interpreters directly on the Android device.) and allows third-party developers to build simple Android applications with perl, JRuby, Python, LUA and BeanShell.SL4A makes it possible to quickly prototype applications for Android on the device itself using high-level scripting languages. These scripts have access to many of the APIs available to full-fledged Android applications, but with a greatly simplified interface and You can embed your script in a stand alone APK.

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.

--

--

Shubham Soni
Code Yoga

Senior Executive @Ketto @Dart @Flutter @Java @Android, Editor @FlutterCommunity @CodeBurst.io, App Developer @Senior @Moderator @FlutterDeveloper