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
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.
If you want to use smartphone features (ex. GPS)
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)
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
python-for-android
builds for ARM by default, partial multiarch support
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.