Standalone Android Apps in Python

Baptiste 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 B

    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 ...
    • Download .vbi virtual machine image 
    • Run with virtualbox

    Toolchain (2/5)


    Toolchain (2/5)


    Toolchain (3/5)

    Distribute

    Cross-compilation

    kivy@kivy-VirtualBox:~/android/python-for-android$ ./distribute.sh -m kivy


    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"


    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.py
    import kivy from kivy.app import App from 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
    $ cd ~/android/python-for-android/dist/default$ ./build.py --package com.example.myapp --name "MyApp" --version 1 --dir /path/to/kivy/app/ debug

    Install
    • Phone: Settings > Developer options > USB debugging
    $ adb install bin/MyApp-1-debug.apk
    • ...or simply open the .apk on the phone (transfer by email, USB, Dropbox, ...)

    Toolchain (5/5)





    II. Kivy

    • The KV language
    • Accessing the hardware
    • Useful 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 App
    2. SSH to a Python prompt
    3. Poke around

    $ ssh -p8000 admin@192.168.1.3
    admin@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
    #kivy
    3. InteractiveLauncher
    2. Kivy Catalog

    1. Kivy Showcase



    That's all



    References

    Actually...

    I haven't been completely honest


    • Python App gets woken up by Java App
    • Java App stays alive to pass events through JNI
    Made with Slides.com