PyJnius to rule them all!

Last time I described how I would be rewriting some of the Plyer facades. I had some confusion about the how to go about this after listening to varying opinions from different Kivy community members. I even had a plan to study the sl4a style of doing things.  But finally after a discussion with  tito [1] and the rest of the group, we decided to drop that approach altogether and stick with using Pyjnius on Android.

In my introductory post to Kivy, I had described the motivation behind the Pyjnius project:

 … a Python module to access Java classes as Python classes using JNI. The idea behind Pyjnius is completely different from other frameworks offering similar functionality of providing device independent access to hardware features. Instead of having to write specific “interface” code using Android SDK to make calls to the device APIs.

Pyjnius let’s you autoclass any of the Java classes and even implement abstract classes that act as API interfaces. To illustrate this with an example, I’ll consider the accelerometer facade in Plyer that I re-worked upon:

Yes, it’s actually as simple as that! You can access any of the Android APIs just by specifying the corresponding Java classes.

Implementing an abstract class takes slightly more effort as you have to declare an interface with the methods that need to be implemented:

Except hashCode, the rest of the portions are quite self-explanatory. Wikipedia has the following to say about the hash code function in Java:

In the Java programming language, every class implicitly or explicitly provides a hashCode() method, which digests the data stored in an instance of the class into a single hash value (a 32-bit signed integer). This hash is used by other code when storing or manipulating the instance …

You would probably want to refer to, if you are interested in using Pyjnius yourself.

I think after this first accelerometer facade, implementation other similar features (other sensors) would a be a trivial task. I hope to make some good progress on them in the coming week.


  1. I must acknowledge that a most of the content in this post has been derived from this discussion. ^

3 thoughts on “PyJnius to rule them all!”

  1. Hello,
    Do you have some code sample in order to access NFC interface and receive datas from tags ?
    I have already:
    g_nfc_NfcAdapter = autoclass(“android.nfc.NfcAdapter”)
    g_nfcAdapter = g_nfc_NfcAdapter.getDefaultAdapter()
    print “NFC is enabled=”, g_nfcAdapter.isEnabled()
    g_content_Intent = autoclass(“android.content.Intent”)

    How to do the following ?

    Best regards

  2. >>> from jnius import autoclass
    >>> MediaRecorder = autoclass(‘’)
    Traceback (most recent call last):
    File “”, line 1, in
    File “build/bdist.linux-x86_64/egg/jnius/”, line 154, in autoclass
    File “jnius_export_func.pxi”, line 25, in jnius.find_javaclass (jnius/jnius.c:15247)
    jnius.JavaException: Class not found ‘android/media/MediaRecorder’

    can you please help me fix this issue.. (?) . Its looks like I can import java classes well but not android API’s .

Leave a Reply

Your email address will not be published. Required fields are marked *