Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Standalone Android Apps in Python
1. STANDALONE ANDROID APPS IN PYTHON
Baptiste Lagarde
Sydney Python Group, 2013.11.07
I. Toolchain
II. Kivy
2. 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++.
5. TOOLCHAIN (2/5)
Prerequisites ~6GB (sigh)
Option A
Download Android SDK + NDK
Follow pages and pages of Google instructions
Download more stuff from inside Eclipse (?!)
Set-up some environment variables
git clone ..., pip install ...
Option B
Download .vbi virtual machine image
Run with virtualbox
8. TOOLCHAIN (3/5)
Cross-compilation
Distribute
kv@iyVrulo:/nri/yhnfradod .dsrbt.h- kv
iykv-itaBx~adodpto-o-nri$ /itiues m iy
1. Creates a host Python
2. Compiles an ARM Python
3. Opt-in recipes with -m
Modules
Some Standard Library modules excluded by default to save space
Pure python modules can simply be included with the app's code
Many modules are available as "recipes"
9. 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.
#ts.y
etp
ipr kv
mot iy
fo kv.p ipr Ap
rm iyap mot p
fo kv.i.utnipr Bto
rm iyuxbto mot utn
casTsApAp:
ls etp(p)
dfbidsl)
e ul(ef:
rtr Bto(et'el Wrd)
eun utntx=Hlo ol'
i _nm_ = '_an_:
f _ae_ = _mi_'
TsAp)rn)
etp(.u(
$pto ts.y
yhn etp
10. TOOLCHAIN (5/5)
Build and Install
Build
$c ~adodpto-o-nri/itdfut
d /nri/yhnfradodds/eal
$.bidp -pcaecmeapemap-nm "yp"-vrin1-dr/aht/iyap
/ul.y -akg o.xml.yp -ae MAp -eso
-i pt/okv/p/
Install
Phone: Settings > Developer options > USB debugging
$abisalbnMAp1dbgak
d ntl i/yp--eu.p
...or simply open the .apk on the phone (transfer by email, USB, Dropbox, ...)
14. 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
Bxaot
oLyu:
oinain "etcl
retto: vria"
Lbl
ae:
i:ito
d nr
TxIpt
etnu:
i:pt
d ah
mliie Fle
utln: as
o_etvldt:apstah)
ntx_aiae p.ept(
Lbl
ae:
i:pors
d rges
Bxaot
oLyu:
oinain "oiotl
retto: hrzna"
Bto:
utn
tx:"tr cpue
et Sat atr"
o_rs:apsat)
npes p.tr(
Bto:
utn
tx:"tpcpue
et So atr"
o_rs:apso(
npes p.tp)
Bto:
utn
tx:"ut
et Qi"
o_rs:ei(
npes xt)
15. 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
>>fo jisipr atcas
> rm nu mot uols
>>Hrwr =atcas'r.ep.nri.adae)
> adae
uols(ogrnyadodHrwr'
>>Hrwr.ceeoeeEal(re
> adaeaclrmtrnbeTu)
>>pitHrwr.ceeoeeRaig)
> rn adaeaclrmtredn(
[.10000906,-.99996271,958016153]
0140005644 0109994323 .30008124
(iOS -> See pyobjus)
16. KIVY (4/5)
Kivy remote shell
Useful to try out or troubleshoot hardware-related functions
1. Install Remote Kivy App
2. SSH to a Python prompt
3. Poke around
$sh-80 amn121813
s p00 di@9.6..
amn121813spswr:#"iy
di@9.6..' asod
kv"
>>ipr pafr
> mot ltom
>>pafr.ahn(
> ltommcie)
'rvl
am7'
>>fo jisipr atcas
> rm nu mot uols
>>TxTSec =atcas'nri.pehtsTxTSec'
> etopeh
uols(adodsec.t.etopeh)
>>PtoAtvt =atcas'r.ep.nri.yhnciiy)
> yhnciiy
uols(ogrnyadodPtoAtvt'
>>ts=TxTSec(yhnciiymciiy Nn)
> t
etopehPtoAtvt.Atvt, oe
>>tssek'el Wrd' TxTSec.UU_LS,Nn)
> t.pa(Hlo ol., etopehQEEFUH oe
>>tssek'999999,TxTSec.UU_LS,Nn) #hh
> t.pa(999999' etopehQEEFUH oe
aa