“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.
Installing p4a in Python for Android Development
p4a is now available on Pypi, so you can install it using pip:
pip install python-for-android
p4a has several dependencies:
- cython (Install via pip)
- Java JDK (e.g. openjdk-8)
- zlib (including 32 bit)
- libncurses (including 32 bit)
- virtualenv (Install 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 fetch 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 get 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.
Using Python for Android Development
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 all the logging information.
- –sdk_dir The directory for Android SDK location.
- –android_api The android API level to use.
- –ndk_dir Complete file path where Android NDK is present.
- –name Name of the app distribution.
- –requirements List of all python modules in the app.
- –force-build [BOOL] Forcibly build the app from scratch.
- –arch Architecture for which you need to build your app. Examples, arm7, arm64, armeabi-v7a, etc.
- –bootstrap BOOTSTRAP The complete List of bootstrap for your requirements. Usually, this command is optional and it automatically detects the List of bootstraps for your project.
Working on Python for Android Development
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()
3- Handling the back button
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.