Python Programming on Android
https://www.wendangku.net/doc/1d18036340.html, /python-programming-on-android/
by falcon wuzhangjin@https://www.wendangku.net/doc/1d18036340.html, of https://www.wendangku.net/doc/1d18036340.html,
2013/12/13Contents Introduction Download SL4A and Py4A Install SL4A and Py4A Run the samples Package python program to a standalone APK Conclusion
Author: Wu Zhangjin
Introduction
Python is a very graceful language, do you want to use it on Android? The SL4A and Py4A projects added Python programming support for Android.
Download SL4A and Py4A
Let’s install the already compiled SL4A and Py4A from https://www.wendangku.net/doc/1d18036340.html,/p/android-scripting/downloads/list and https://www.wendangku.net/doc/1d18036340.html,/p/python-for-android/downloads/list respectively. In the time of this writing, the latest SL4A is sl4a_r6.apk , the latest Py4A is PythonForAndroid_r6.apk .
123$ mkdir ~/workspace && cd ~/workspace
$ wget -c https://www.wendangku.net/doc/1d18036340.html,/files/PythonForAndroid_r6.apk
$ wget -c https://www.wendangku.net/doc/1d18036340.html,/files/sl4a_r6.apk
Install SL4A and Py4A
If you don’t have any Android devices, but want to try Python programming on Android, please learn how to create a virtual Android device from Java Programming on Android and this article also shows how to prepare the Android Java programming environment, which is also required by the coming practices.
Please make sure your sdcard has at least 1G free space. For the virtual Android device, please use the AVD Manager(android avd) to set the size of the virtual SD Card to bigger than 1G.
Now, Let’s install the packages.
12$ adb install ./sl4a_r6.apk
$ adb install ./PythonForAndroid_r6.apk
We still need to download the real Python libraries, modules and samples via the just installed PythonForAndroid package, please enter into the Home screen of your Android device, find out the Python For Android icon, start it and press Install to download and install more required files.
Run the samples
After installation, the samples will be downloaded to /sdcard/sl4a/scripts/:
1
2
3
4$ adb shell ls /sdcard/sl4a/scripts/bluetooth_chat.py hello_world.py notify_weather.py
5 6 7 8 9 10 11say_chat.py say_time.py say_weather.py speak.py
take_picture.py test.py weather.py
To run them, you can start the sl4a application on your Android, and it will list the above scripts and select any one of them, for example: say_time.py, press the left Terminal button to run it, after a while, the Android system will speak and tell you current time.
If want run the python program from command line, just need to issue:
1 2$ adb shell
$ am start -a com.googlecode.android_https://www.wendangku.net/doc/1d18036340.html,UNCH_FOREGROUND_SCRIPT -n com.googlecode.android_scripting/.activity.ScriptingLayerServiceLauncher -e
com.googlecode.android_scripting.extra.SCRIPT_PATH /sdcard/sl4a/scripts/say_time.py
Package python program to a standalone APK
If want to package python program to a standalone APK, we can use the template provided by the SL4A project, Let’s clone the SL4A project at first:
1$ git clone https://https://www.wendangku.net/doc/1d18036340.html,/damonkohler/sl4a.git
Enter into the template and you will see the Python script is stored in res/raw as a resource file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17$ cd sl4a/android/ScriptForAndroidTemplate
$ cat res/raw/script.py
import android,time
droid = android.Android()
droid.makeToast('Hello, Android!')
droid.vibrate(300)
try:
droid.startSensing()
time.sleep(1)
e=droid.eventPoll(1)
droid.eventClearBuffer()
droid.makeToast("Polled: "+str(e))
except:
droid.makeToast("Unexpected error:"+str(sys.exc_info()[0])) droid.makeToast("Done")
And this resource is parsed in Script.java:
1 2$ grep R.raw.script -ur src/com/dummy/fooforandroid/Script.java public final static int ID = R.raw.script;
Now, Let’s package it to a normal Android application.
1 2 3 4 5 6 7 8 9 10$ android list targets
Available Android targets:
----------
id: 1 or "android-19"
Name: Android 4.4
Type: Platform
API level: 19
Revision: 1
Skins: HVGA, WVGA800 (default), WXGA800, WVGA854, WQVGA400, WXGA800-7in, WQVGA432, QVGA, WSVGA, WXGA720
11 12 13 14 ABIs : armeabi-v7a
$ rm build.xml
$ android project update -p ./ -t 1 -s $ ant debug
$ ls bin/ScriptActivity-debug.apk
Now, we can install it and run it as the other Android Applications.
In a clean Android system, when running the above application, it will guide you to download SL4A and Py4A at first, after downloading SL4A and Py4A, it will run like a usual Android Java Application.
If want to package your own Python script, please replace the res/raw/script.py with your own, Let’s try one:
1 2 3 4 5 6 7$ cat > res/raw/script.py
import android
droid=android.Android()
droid.ttsSpeak('Android and Python, I love you!') $ ant debug
$ adb install -r bin/ScriptActivity-debug.apk
It will save love to Android and Python!!
Conclusion
It is really awesome to run Python on Android, thanks to both SL4A and Py4A projects.
To learn more about Python programming on Android, please read the resources from the wiki page of SL4A
.
Author: Wu Z hangjin
Linux系统的忠实用户,开发者和贡献者,04年开始学习Linux, 06年跟校友一起创立兰州大学开源社区,09年开始往Linux 官方贡献代码,曾学习或供职于嵌入式系统实验室、处理器芯片产业化公司、嵌入式系统公司,现服务于一家嵌入式产品公司,从事Linux Kernel Features方面的研发,致力于提供更稳定可靠高效节能的嵌入式产品,进而提升Linux产品的用户体验。