Develop Android App using Python: Set up Environment

Home / Develop Android App using Python: Set up Environment

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.

The Scripting Layer for Android (SL4A) brings scripting language to Android by allowing you to edit and execute scripts and interactive interpreters directly on the Android device. Started out as 20% project by Google employee Damon Kohler, the project now supports Python, Ruby, Perl, Lua, JavaScript, BeanShell, and more platform.

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:

  1. Slackware64 14.0 as developer machine
  2. Windows 8 64-bit as developer machine
  3. Sony XPeria U as target machine
  4. Java Development Kit (JDK) 1.7.0_13
  5. Android Software Development Kit (SDK)
  6. 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:

  1. SmallSat
  2. Cellbots – http://www.cellbots.com/
  3. Oplop – http://code.google.com/p/oplop/
  4. Broadcast – http://github.com/mleone/broadcast/

Why Python?

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.

Obtain Materials

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):

  1. sl4a_r6.apk
  2. PythonForAndroid_r4.apk

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.

screenshot_2013-09-26_1215

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:

screenshot_2013-09-26_1241

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.

screenshot_2013-09-26_1305

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.

Reference:
  1. Installing Android SDK on Linux
  2. Installing Android SDK on Windows
  3. Installing Java Development Kit on Linux

Testing

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:

  • ActivityResult
  • Android
  • ApplicationManager
  • BatteryManager
  • Camera
  • CommonIntents
  • Contacts
  • Event
  • EyerFree
  • Location
  • MediaPlayer
  • MediaRecorder
  • Phone
  • Preferences
  • SensorManager
  • Settings
  • Sms
  • SpeechRecognition
  • ToneGenerator
  • WakeLock
  • Wifi
  • UI

, ,

About Author

about author

xathrya

A man who is obsessed to low level technology.

Leave a Reply

Your email address will not be published. Required fields are marked *

Social media & sharing icons powered by UltimatelySocial