Python for Android Tutorial #7 - PyJNIus - Using Android native classes
This is the seventh post about mobile development with Python.
In this post, I’ll show you how to work with PyJNIus, a Python module to access Java classes as Python classes using JNI.
To show how it works, we’ll create a simple share button to open an intent and share a link with an external application :)
I - About the Tutorials
I’ll show you how to use each mobile API, like compass, camera, sensors, sound, and others. After the API, we will start to test some Python libs, as PyGame and OpenCV. I’m using Android with buildozer, but major part are compatible with iOS as well.
|Get updates||Follow @aron-bordin|
II - Index
- Tutorial #1 - Accelerometer
- Tutorial #2 - Camera
- Tutorial #3 - Kivy Designer
- Tutorial #4 - Debug
- Tutorial #5 - Android and iOS game with Python
- Tutorial #6 - Reading and saving user data
- Tutorial #7 - Use Android classes with PyJNIus
III - About this tutorial
This is a short post, just to show you how to start an Android Intent with Kivy.
In this post I’ll be using Pyjnius, a Python module to access Java classes as Python classes using JNI.
This post is explaining more about the game created at Tutorial #5 – Android and iOS game with Python
IV - Creating our project
Let’s start to work on it.
Create a new folder, and create two files, main.py, for the source code, and main.kv, the UI. Now, start a new buildozer project with the command:
As we are using an external library, Pyjnius, you need to add it to your buildozer.spec file. Open your buildozer.spec and add pyjnius to the following line:
Now to make sure that you are able to run this project, run the following command:
This command will prepare your environment, check requirements and run the app on Android. If you get some error, please comment here so I can try to help you.
Now, let’s create a simple UI to test the Android Intent.
V - A simple UI with KV Lang
Just add the following code to your main.kv:
With this code, the parent widget will be a RootWidget, a Python class that we’ll create later.
The RootWidget has a simple Button, with a text and on_press event.
When the user clicks on the button, it will can the method share() on RootWidget class.
VI - Python Code
Let’s add just the necessary to run our app. Use the following code in main.py:
It’s simple! We just added a RootWidget that expends a BoxLayout, and created the method share().
VII - Working with PyJNIus
Now, let’s learn more about PyJNIus and use it.
PyJNI can wrap Java classes to use it with Python! Take a look in this simple example provided by PyJNIus documentation:
With these commands you will be able to call Java System.out.println!
VIII - Starting an Android Intent with Kivy and PyJNIus
Now, let’s integrate PyJNIus with our Android Kivy application.
You can read the Android documentation here: http://developer.android.com/training/sharing/send.html
Add these lines to the top of your file:
As long as we can share only with Android, first we need to check the running platform and then import the required libraries.
Now, just update the share():
And here we can the the Android Intent working:
That’s it. I’ll be writing more tutorials soon.
Thanks for reading!