Scripting Layer for Android
SL4A does what you want. You can easily install it directly onto your device from their site, and do not need root.
It supports a range of languages. Python is the most mature. By default, it uses Python 2.6, but there is a 3.2 port you can use instead. I have used that port for all kinds of things on a Galaxy S2 and it worked fine.
API
SL4A provides a port of their android
library for each supported language. The library provides an interface to the underlying Android API through a single Android
object.
from android import Android
droid = Android()
droid.ttsSpeak('hello world') # example using the text to speech facade
Each language has pretty much the same API. You can even use the JavaScript API inside webviews.
let droid = new Android();
droid.ttsSpeak("hello from js");
User Interfaces
For user interfaces, you have three options:
- You can easily use the generic, native dialogues and menus through the
API. This is good for confirmation dialogues and other basic user inputs.
- You can also open a webview from inside a Python script, then use HTML5
for the user interface. When you use webviews from Python, you can pass
messages back and forth, between the webview and the Python process that
spawned it. The UI will not be native, but it is still a good option to
have.
- There is some support for native Android user interfaces, but I am not
sure how well it works; I just haven't ever used it.
You can mix options, so you can have a webview for the main interface, and still use native dialogues.
QPython
There is a third party project named QPython. It builds on SL4A, and throws in some other useful stuff.
QPython gives you a nicer UI to manage your installation, and includes a little, touchscreen code editor, a Python shell, and a PIP shell for package management. They also have a Python 3 port. Both versions are available from the Play Store, free of charge. QPython also bundles libraries from a bunch of Python on Android projects, including Kivy, so it is not just SL4A.
Note that QPython still develop their fork of SL4A (though, not much to be honest). The main SL4A project itself is pretty much dead.
Useful Links
- SL4A Project (now on GitHub): https://github.com/damonkohler/sl4a
- SL4A Python 3 Port: https://code.google.com/p/python-for-android/wiki/Python3
- QPython Project: http://qpython.com
- Learn SL4A (Tutorialspoint): https://www.tutorialspoint.com/sl4a/index.htm