“Python for Android Development”- heard a lot about it?? Lets here see how it works and how to do with a small brief introduction.
Getting up and running on python-for-android (p4a) is a simple process and should only take you a couple of minutes. Its better if referred to Python for android as p4a. Android APK packager for Python scripts and apps. … The executable is called python-for-android or p4a.
p4a is now available on Pypi, so you can install it using pip:
pip install python-for-android
p4a has several dependencies that must be installed:
• git • ant • python2 • cython (can be installed via pip) • a Java JDK (e.g. openjdk-8) • zlib (including 32 bit) • libncurses (including 32 bit) • unzip • virtualenv (can be installed via pip) • ccache (optional) • autoconf (for ffpyplayer_codecs recipe) • libtool (for ffpyplayer_codecs recipe) • cmake (required for some native code recipes like jpeg’s recipe)
Installing Android SDK
You need to download and unpack the Android SDK and NDK to a directory (let’s say $HOME/Documents/):
• Android SDK
• Android NDK
For the Android SDK, you can download ‘just the command line tools’. When you have extracted these you’ll see only a directory named tools, and you will need to run extra commands to install the SDK packages needed.
The following are some of the applications of P4A ;
1- Build a Kivy or SDL2 application
To build your application, you need to specify name, version, a package identifier, the bootstrap you want to use (sdl2 for kivy or sdl2 apps) and the requirements:
p4a apk --private $HOME/code/myapp --package=org.example.myapp --name "My application ˓→" --version 0.1 --bootstrap=sdl2 --requirements=python3,kivy
2- Build a WebView application
To build your application, you need to have a name, version, a package identifier, and explicitly use the webview bootstrap, as well as the requirements:
p4a apk --private $HOME/code/myapp --package=org.example.myapp --name "My WebView ˓→Application" --version 0.1 --bootstrap=webview --requirements=flask --port=5000
The following are set of commands that is generally used in android dev;
–debug Print extra debug information about the build, including all compilation output.
–sdk_dir The filepath where the Android SDK is installed. This can alternatively be set in several other ways.
–android_api The Android API level to target; python-for-android will check if the platform tools for this level are installed.
–ndk_dir The filepath where the Android NDK is installed. This can alternatively be set in several other ways.
–name NAME The name of the distribution. Only one distribution with a given name can be created.
–requirements LIST,OF,REQUIREMENTS The recipes that your distribution must contain, as a comma separated list. These must be names of recipes or the pypi names of Python modules.
–force-build BOOL Whether the distribution must be compiled from scratch.
–arch The architecture to build for. Currently only one architecture can be targeted at a time, and a given distribution can only include one architecture.
–bootstrap BOOTSTRAP The Java bootstrap to use for your application. You mostly don’t need to worry about this or set it manually, as an appropriate bootstrap will be chosen from your –requirements
Working on Android
1- Runtime permissions
With API level >= 21, you will need to request runtime permissions to access the SD card, the camera, and other things. This can be done through the android module which is available per default unless you blacklist it. Use it in your app like this:
from android.permissions import request_permissions, Permission request_permissions([Permission.WRITE_EXTERNAL_STORAGE] )
2- Dismissing the splash screen
With the SDL2 bootstrap, the app’s splash screen may be visible longer than necessary (with your app already being loaded) due to a limitation with the way we check if the app has properly started. In this case, the splash screen overlaps the app gui for a short time. To dismiss the loading screen explicitely in your code, use the android module:
from android import hide_loading_screen hide_loading_screen()
Android phones always have a back button, which users expect to perform an appropriate in-app function. If you do not handle it, Kivy apps will actually shut down and appear to have crashed. In SDL2 bootstraps, the back button appears as the escape key (keycode 27, codepoint 270). You can handle this key to perform actions when it is pressed. For instance, in your App class in Kivy:
from kivy.core.window import Window class YourApp(App): def build(self): Window.bind(on_keyboard=self.key_input) return Widget() # your root widget here as normal def key_input(self, window, key, scancode, codepoint, modifier): if key == 27: return True # override the default behaviour else: # the key now does nothing return False
4- Pausing the App
When the user leaves an App, it is automatically paused by Android, although it gets a few seconds to store data etc. if necessary. Once paused, there is no guarantee that your app will run again. With Kivy, add an on_pause method to your App class, which returns True:
def on_pause(self): return True
The above are some of the common and most used usages in Python for android development.