How to Create an Android App and APK on Android with Python

I’m a developer (among other things). I love Android, but I think Java is cumbersome and annoying. I want to create Android apps using Python. With the Scripting Layer for Android (SL4A) I can create little scripts on the Android device, or I can develop bigger apps using Eclipse and the Android SDK on a “real” computer. About six months ago the Android Java IDE (AIDE) was created. It lets you create real, distributable Android apps (APKs) on Android itself, which is very cool, but you have to do it Java.

Or do you?

Here’s how I combined SL4A’s Eclipse app development process with AIDE to create an Android app and APK on Android, written in Python.

Preliminary Setup
Before we begin, you will need a few Android Apps installed:

OK, let’s get started!

On your computer:

  1. Download script_for_android_template.zip
  2. Extract it to `Dropbox/Projects/MyProject`
  3. Wait for Dropbox sync to finish


On Android:

  1. Open AIDE
  2. Cancel app creation if it pops up
  3. Select “Download Dropbox Folder here”
  4. Select “Projects”
  5. Select “MyProject”
  6. Tap “Download”
  7. Wait for Dropbox sync to finish
  8. Select “Open this App Project”
  9. Select “res”
  10. Select “raw”
  11. Select “script.py”
  12. Make some changes
  13. Menu -> Run

Tips and Tricks

  • Your Python project lives in the `MyProject/res/raw` folder.
  • `script.py` is the entry point of your Python project. You can change this, but I don’t have instructions for doing so yet.
  • You can skip the Dropbox part of the instructions and do it all on Android, but if you include Dropbox it is easy to go back and forth between AIDE and Eclipse. I think it’s worth the extra step.
  • To create a new file or folder in your project, long-press on an existing file for a file menu.
  • The first time someone runs your APK they will be prompted to install Python for Android if they don’t already have it. The process is the same as you went through when installing the Python Interpreter for SL4A.
  • If your app refuses to install or run, you probably need to sign it. I suggest creating signing keys in Eclipse on your desktop and storing them in Dropbox. You can then use them from within AIDE. AIDE does have built-in key generation, but it didn’t work when I tried it.
  • If “Download” is greyed out when you try to get your folder from Dropbox from within AIDE, that may mean you already have a project with that name in AIDE. Each project name must be unique. You can delete the old project from within AIDE, or rename the new project’s folder in Dropbox.

Where should you go from here?
You should head over to the SL4A Tutorials to learn what all you can do with it.

What’s next for this project?
My goal is to encapsulate this process into an Android app you can download from Google Play. The app will download SL4A Python and the script template, extract it to Dropbox or a location of your choosing, setup the app name you want, and setup the entry point you want (instead of script.py). It will be an easy Android App creator for Python on Android.

My Sources
I scraped together this information from several sources. These are the big ones:

Updates

  • [2012-07-31] I’ve just learned about the android-python27 and related projects. Using this, you can embed the Python runtime in your application to avoid your users having to download it on first run. There is a lot of cool stuff going on around this project including PyQT support and advanced UIs with Kivy.
This entry was posted in android, grlug, grpug, python, tech, wmlug. Bookmark the permalink.
  • Mathieu Virbel

    Hi,

    Kivy (http://kivy.org/) is not related with android-python27, they are both independent project. If you use Kivy for doing a graphical user interface, then we provide instructions for packaging your application for Android (using our own github.com/kivy/python-for-android, which is not the same as P4A), on iOS (using kivy-ios), and Window/Linux/OSX using pyinstaller.
    :)

    Mathieu

    • brousch

      Thanks for clearing that up. So many of the Android+Python projects have a similar name – I think it’s confusing right now. I haven’t dug into any of them yet, so I appreciate the pointers. Maybe I’ll put together a Python and Android website where interested people can find out about all of the different ways of using Python on Android.

  • Pingback: How to Create an Android App and APK on Android with Python | Android Development

  • Pingback: New, unsorted | Pearltrees

  • Donald Carroll

    Im tring to find a way to run the scripts I’ve written in sl4a on my device, simce I can’t run the from the command line in Android. I tried this, but.it don’t work for me (first it said I didn’t have Python For Android installed, which I do, and then it just started crashing without error. I don’t know what it means to sign the script. I’m a newb!! Will someone take pitty and help me?

    • brousch

      I have very limited experience with signing applications. I suggest you join the SL4A discussion list for help from folks very experienced in these things. https://groups.google.com/forum/?fromgroups#!forum/android-scripting

      • Geomath Ahmet

        Hi. I have an android application using PySide and QML. It uses Python27 for android. But I can’t use this with SL4A. How can I import SL4A libraries into python2.7 ? Thanks

        • brousch

          I have no idea. I have moved on to using Kivy for Python on Android stuff.

  • Pingback: PyOhio 2012 Presentation – Python and Android | Ben Rousch's Cluster of Bleep

  • newt

    1. Download script_for_android_template.zip
    2. Extract it to `Dropbox/Projects/MyProject`

    i don’t understand this part.
    where’s the directory “Dropbox/Projects/MyProject” ? is those in the sdcard?
    or should I create those directory in my sdcard? like “/mnt/sdcard/Dropbox/Projects/MyProject” ?

    • brousch

      In this post I assume you are using Dropbox to sync with your device. You need to make the Dropbox/Projects/MyProject directory on your main computer, not on your device.

  • Pingback: Android python | Istudyweb

  • srikanth nagabhushan

    can u post a video on how to do it right from the beginning,from setting up android sdk till installing SL4A.I had edited the eclipse to run python but these setting didn,t apply to the entire package downloaded from google

  • Onsterion

    Very good tutorial for noobs like me :D.

    Question, when i create and install the apk. Where is installed?. And how can change the name Application “Dummy Script”.

    • Onsterion

      and other problem, when i tranform the script.py in to apk, install and play, webview don’t open :S

      • http://www.facebook.com/valentine.bondar Valentine Bondar

        Use the webViewShow function:
        droid = android.Android()
        webViewShow(“path/to/file/you/want/to/display”)

  • Pingback: update bookmark blog 12/19/2012 (p.m.) | Kevin Mullet's Bookmarks

  • jumping jack flash

    “My goal is to encapsulate this process into an Android app you can download from Google Play.” Any news about this app?

    • brousch

      I’ve actually moved to using Kivy. I still do not have an entirely on-Android process completed yet. QPython does have some more on-device capabilities. http://qpython.com/

  • Pingback: kivy | Annotary