December 9, 2015 | Article | No Comments
A common misconception about developing for the Google Android Platform is that you have to write your code in the Java programming language. The truth is you actually have many options. OK, aside of NDK (Native Development Kit) using C/C++, you can develop application using Scripting Layer for Android.
This article is part of series of articles explores how to use Python and Scripting Layer for Android (SL4A) to build application for Android.
This article will discuss about how to setup environment for developing Android app using Python. What I use for this article are:
- Slackware64 14.0 as developer machine
- Windows 8 64-bit as developer machine
- Sony XPeria U as target machine
- Java Development Kit (JDK) 1.7.0_13
- Android Software Development Kit (SDK)
- Python 2.7.3
Note that I use Slackware (Linux) and Windows as proof of concept. You can either use one or both but I will try to make it as general as possible.
Who have Use SL4A?
Many! But notable users are:
- Cellbots – http://www.cellbots.com/
- Oplop – http://code.google.com/p/oplop/
- Broadcast – http://github.com/mleone/broadcast/
So the first question, why python?
Python is a great tool for writing both simple scripts and complex, multi-threaded applications. The great thing about having Python on Android is the opportunity to use the untold thousands of lines of code already written and freely available. Python is an easy language to learn if you’ve never used it before, and you will find many resources available on the Internet to help get you up to speed.
SL4A has several packages. But for our goal, we need only two packages: core SL4A and Python for android. Both you can download from SL4A download site.
Here what you need (last version per September 26th, 2013):
As the popularity of Python for Android grow, the project has been forked to new project which stand side to SL4A. You can visit Python for Android project page and download their latest release.
If you don’t have Oracle JDK, Android SDK, and Python yet then grab them.
Installation and Setup
On Android Device
Install SL4A on Android device. This process is similar to any other Android application installation process.
Before installation, make sure you enable the “Unknown sources” option in your device’s “Application” settings.
Open up terminal / your command prompt. Make sure you can access adb from your “$ANDROIDSDK/platform-tools” path.
Connect your phone to your machine and invoke following commands:
adb install -r sl4a_r6.apk adb install -r PythonForAndroid_r4.apk
To make sure Python for Android installed, see your application list and search for Python for Android. It should has following user interface:
Tap on Install to install the interpreter needed. Your phone then downloading some extra packages.
To verify, open application named SL4A on your launcher. See the “Interpreter” on “View” menu. You should see Python 2.6.2 there.
On Development Machine
The SL4A needs Oracle Java Development Kit (JDK) and Android Software Development Kit (SDK). All of them must be provided before we install SL4A. We also need to install Python. Note that all dependencies should have correct architecture based on your machine. As I use 64-bit machine, I will need to install the 64-bit version.
At this point, I assume you have all the dependencies installed.
- Installing Android SDK on Linux
- Installing Android SDK on Windows
- Installing Java Development Kit on Linux
Let’s test the environment by creating a simple hello world project.
Write this on your development machine as hello.py:
import android droid = android.Android() result = droid.makeToast('Hello, world!')
Then send it to your Android phone. The simplest way is using adb.
adb push hello.py /sdcard/sl4a/scripts/hello.py
Now run the script by running SL4A. You should see lot of python script in the list. Our script is hello.py, just tap it to run it and you can see the toast.
You can also create a simple script to upload your script:
Appendix A: SL4A Functionality
The SL4A has following functionality which can be used on Python: