Android™ Apps with python-for-android


Android Robot + Python Logo

Kevin Howell

2017-02-23

A Little About Android

Android Stack

  • Mobile operating system with Linux at the core
  • Apps typically written in Java-like language
  • Android SDK is in Java-like language

Java? What About Python?

Android Stack

  • "Native libraries" in Android are not implemented in Java-like language, but in native code (generally C/C++) using the NDK.
  • Python is implemented in C, thus we can get Python running via the NDK

Kivy - History/Context

Kivy Logo

  • Cross-platform Python framework for writing applications.
  • Started circa early 2011.
  • Lots of useful things originating from Kivy...

Kivy - python-for-android

Kivy Logo

Toolchain for building APKs for python projects.

pip install python-for-android

main.py is your entrypoint. To build an APK:

p4a apk --private . \
  --name TGG \
  --package net.kahowell.tgg \
  --version 0.1.0 \
  --bootstrap=sdl2 \
  --requirements=python2,kivy

Mobile Application Architectures

model-view-controller

Model-View-Controller

Mobile Application Architectures

model-view-controller

Client-Server

Mobile Application Architectures

model-view-controller

Client-Server with Local Server

Mobile User Interfaces

Native UI

Possible, but lots of switching between Java & Python... not straight-forward.

Mobile User Interfaces

Cross-Platform UI

Kivy

Quick Demo - Kivy UI

Mobile User Interfaces

SDL2

Custom graphics, ex. a game

Mobile User Interfaces

WebView

Use your webapp skills (HTML, CSS, JavaScript)

Quick Demo - WebView

Porting Existing Code to Android

Dependencies

It's all about dependencies...

If your dependencies are pure-Python, then no extra work required.

If they involve C extensions (Cython, SWIG, etc)., then you need to use a recipe.

Recipes are written in Python, responsible for patching and building modules.

Popular Libraries as Recipes

  • cffi
  • cryptography
  • m2crypto
  • mysqldb
  • numpy
  • openssl
  • pil
  • pygame
  • pytz
  • pyusb
  • pyzmq
  • sqlalchemy
  • twisted
  • vispy
  • zope

Accessing Android APIs from Python

If you want to use smartphone features (ex. GPS)

High-level: Plyer

Cross-platform interfaces to mobile device features (iOS-friendly!)

from plyer import tts

tts.speak('Hello world.')

Accessing Android APIs from Python

If you want to use smartphone features (ex. GPS)

Low-level: Pyjnius

Wraps Java code into python wrappers

# example from https://pyjnius.readthedocs.io/en/latest/android.html#using-texttospeech
from jnius import autoclass
Locale = autoclass('java.util.Locale')
PythonActivity = autoclass('org.renpy.android.PythonActivity')
TextToSpeech = autoclass('android.speech.tts.TextToSpeech')
tts = TextToSpeech(PythonActivity.mActivity, None)

# Play something in english
tts.setLanguage(Locale.US)
tts.speak('Hello World.', TextToSpeech.QUEUE_FLUSH, None)

Android App Considerations

Apps get paused by Android itself; the application should save its state in case it is closed afterwards

# not necessary for webview app
class MyApp(App):
    def on_pause(self):
        pass  # save state here

There are idiomatic ways to handle things in an Android app that are different from desktop/server Python applications

  • background processes
  • data storage
  • permissions

python-for-android builds for ARM by default, partial multiarch support

Quick Demo - How I Used It

Questions?

Credits

Android is a trademark of Google Inc.

The Android robot is reproduced or modified from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

Portions of this presentation are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

The Python logo is a trademark of the Python Software Foundation.

Kivy is a trademark of the Kivy Organization.

The Kivy logo is a work done by Vincent Autin. The logo is placed under CC-BY-NC-ND.