standalone android apps in python

19
STANDALONE ANDROID APPS IN PYTHON Baptiste Lagarde Sydney Python Group, 2013.11.07 I. Toolchain II. Kivy

Upload: baptiste-lagarde

Post on 18-Nov-2014

1.435 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Standalone Android Apps in Python

STANDALONE ANDROID APPS IN PYTHONBaptiste Lagarde

Sydney Python Group, 2013.11.07

I. Toolchain II. Kivy

Page 2: Standalone Android Apps in Python

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++.

Page 3: Standalone Android Apps in Python

I. TOOLCHAIN

Page 4: Standalone Android Apps in Python

TOOLCHAIN (1/5)Overview

http://python-for-android.readthedocs.org/en/latest/toolchain/

Page 5: Standalone Android Apps in Python

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

Page 6: Standalone Android Apps in Python

TOOLCHAIN (2/5)

Page 7: Standalone Android Apps in Python

TOOLCHAIN (2/5)

Page 8: Standalone Android Apps in Python

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"

Page 9: Standalone Android Apps in Python

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

Page 10: Standalone Android Apps in Python

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, ...)

Page 11: Standalone Android Apps in Python

TOOLCHAIN (5/5)

Page 12: Standalone Android Apps in Python

II. KIVYThe KV languageAccessing the hardwareUseful resources

Page 13: Standalone Android Apps in Python

KIVY (1/5)Example app

(Saves the phone's accelerometer readings to CSV)

Page 14: Standalone Android Apps in Python

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

Page 15: Standalone Android Apps in Python

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]

Page 16: Standalone Android Apps in Python

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

Page 17: Standalone Android Apps in Python

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

Page 18: Standalone Android Apps in Python

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/

Page 19: Standalone Android Apps in Python

ACTUALLY...I haven't been completely honest

Python App gets woken up by Java AppJava App stays alive to pass events through JNI