standalone android apps in python
DESCRIPTION
TRANSCRIPT
STANDALONE ANDROID APPS IN PYTHONBaptiste Lagarde
Sydney Python Group, 2013.11.07
I. Toolchain II. Kivy
JAVA APP VS. PYTHON APP
Python cross-compiled for ARM using the Android NDK
Android NDK - The Native Development Kit allows you to implement parts
of your app using native-code languages such as C and C++.
I. TOOLCHAIN
TOOLCHAIN (1/5)Overview
http://python-for-android.readthedocs.org/en/latest/toolchain/
TOOLCHAIN (2/5)Prerequisites ~6GB (sigh)
Option A
Download Android SDK + NDKFollow pages and pages of Google instructionsDownload more stuff from inside Eclipse (?!)Set-up some environment variablesgit clone ..., pip install ...
Download .vbi virtual machine image Run with virtualbox
Option B
TOOLCHAIN (2/5)
TOOLCHAIN (2/5)
TOOLCHAIN (3/5)Distribute
Cross-compilationkivy@kivy-VirtualBox:~/android/python-for-android$ ./distribute.sh -m kivy
1. Creates a host Python 2. Compiles an ARM Python3. Opt-in recipes with -m
ModulesSome Standard Library modules excluded by default to save spacePure python modules can simply be included with the app's codeMany modules are available as "recipes"
TOOLCHAIN (4/5)Make Kivy App
Kivy - Open source Python library for rapid development of applications
that make use of innovative user interfaces, such as multi-touch apps.
# test.pyimport kivy
from kivy.app import Appfrom kivy.uix.button import Button
class TestApp(App): def build(self): return Button(text='Hello World')
if __name__ == '__main__': TestApp().run()
$ python test.py
TOOLCHAIN (5/5)Build and Install
Build
InstallPhone: Settings > Developer options > USB debugging
$ cd ~/android/python-for-android/dist/default$ ./build.py --package com.example.myapp --name "MyApp" --version 1 --dir /path/to/kivy/app/
$ adb install bin/MyApp-1-debug.apk
...or simply open the .apk on the phone (transfer by email, USB, Dropbox, ...)
TOOLCHAIN (5/5)
II. KIVYThe KV languageAccessing the hardwareUseful resources
KIVY (1/5)Example app
(Saves the phone's accelerometer readings to CSV)
KIVY (2/5)The KV Language
Features Move UI declaration to separate file
Root widget
Templates
Widget classes, inheritance
Anything right of ":" is Python!
Python imports
Event bindings , e.g. on_press Keyword arguments
Hooks, e.g. id, root, app, self
BoxLayout: orientation: "vertical" Label: id: intro TextInput: id: path multiline: False on_text_validate: app.setpath() Label: id: progress BoxLayout: orientation: "horizontal" Button: text: "Start capture" on_press: app.start() Button: text: "Stop capture" on_press: app.stop() Button: text: "Quit" on_press: exit()
KIVY (3/5)Accessing the hardware with pyjnius
pyjnius - Python module to access Java classes as Python classes, using JNI.
JNI - The Java Native Interface is a programming framework that enables
Java code running in a Java Virtual Machine to call, and to be called by,
native applications and libraries written in other languages
(iOS -> See pyobjus)
>>> from jnius import autoclass >>> Hardware = autoclass('org.renpy.android.Hardware') >>> Hardware.accelerometerEnable(True) >>> print Hardware.accelerometerReading() [0.11400000005960464, -0.19099999964237213, 9.5380001068115234]
KIVY (4/5)Kivy remote shell
Useful to try out or troubleshoot hardware-related functions
1. Install Remote Kivy App2. SSH to a Python prompt3. Poke around$ ssh -p8000 [email protected]@192.168.1.3's password: # "kivy"
>>> import platform>>> platform.machine()'armv7l'>>> from jnius import autoclass>>> TextToSpeech = autoclass('android.speech.tts.TextToSpeech')>>> PythonActivity = autoclass('org.renpy.android.PythonActivity')>>> tts = TextToSpeech(PythonActivity.mActivity, None)>>> tts.speak('Hello World.', TextToSpeech.QUEUE_FLUSH, None)>>> tts.speak('999999999999', TextToSpeech.QUEUE_FLUSH, None) # haha
KIVY (5/5)More useful resources
4. irc.freenode.net http://www.youtube.com/watch?v=S2sFqFGDu1k
3. InteractiveLauncher
2. Kivy Catalog
1. Kivy Showcase
#kivy
THAT'S ALL
References
http://python-for-android.readthedocs.org/en/latest/
http://kivy.org/docs/gettingstarted/intro.html
http://developer.android.com/tools/sdk/ndk/index.html
http://mdqinc.com/blog/2011/09/cross-compiling-python-for-android/
ACTUALLY...I haven't been completely honest
Python App gets woken up by Java AppJava App stays alive to pass events through JNI