[ANN] Pyjnius: Accessing Java classes from Python

605 views
Skip to first unread message

Mathieu Virbel

unread,
Aug 20, 2012, 10:05:16 AM8/20/12
to python-...@googlegroups.com, kivy-...@googlegroups.com, kivy...@googlegroups.com
Hi everyone,

Gabriel and i made a new library called Pyjnius to access Java classes
from Python. It's as simple as this example:

>>> Stack = autoclass('java.util.Stack')
>>> stack = Stack()
>>> stack.push('hello')
>>> stack.push('world')
>>> stack.pop()
'world'
>>> stack.pop()
'hello'

The library is already available in the python-for-android project, so
you can use it to access Android API! We got some examples on the
documentation, check them :)

Source code: http://github.com/kivy/pyjnius
Documentation at: http://pyjnius.readthedocs.org/

You can also read a more detailled blog post about it at:
http://txzone.net/2012/08/pyjnius-accessing-java-classes-from-python/

Enjoy!

Mathieu & Gabriel

Daniel Kinsman

unread,
Aug 20, 2012, 9:05:40 PM8/20/12
to kivy-...@googlegroups.com, python-...@googlegroups.com, kivy...@googlegroups.com
Nice work :)

Arnaud Waels

unread,
Aug 20, 2012, 9:27:49 PM8/20/12
to kivy-...@googlegroups.com
that's great news!

2012/8/21 Daniel Kinsman <daniel...@gmail.com>
--
 
 
 

deakblue

unread,
Aug 21, 2012, 12:44:23 PM8/21/12
to kivy...@googlegroups.com, python-...@googlegroups.com, kivy-...@googlegroups.com

This is epic.

Is there anything in the current python-for-android bridge that can't be handled via Pyjnius, or are there things only the Android class can do?

Just epic,
-db-

Antonio reKiem

unread,
Aug 21, 2012, 2:44:38 PM8/21/12
to kivy...@googlegroups.com, python-...@googlegroups.com, kivy-...@googlegroups.com
Great and like deakblue say EPIC!, with this and cython you can convine lenguajes very well, on other hand, we have been working in some improbements to Kivy-Berkelium (by now it can handle new tabs, some touch events and some components to make the ui), we hope we can free this code for next moth, thanks for such amazing tools!

Nik Klever

unread,
Aug 28, 2012, 6:10:06 PM8/28/12
to python-...@googlegroups.com, kivy-...@googlegroups.com, kivy...@googlegroups.com
Hi Mathieu,

congratulations to this interesting concept !

I've tested it, and it seems an excellent approach !

But nevertheless, I've a problem in accessing the methods and fields, if I create my own java classes like your accelerometer example.
This is not the case, when I access normal Android Java Classes.

I'm able to get the names of my methods by a call to getClass().getDeclaredMethods() but they are not accessable via the dictionary of the class (like e.g. densityDpi in DisplayMetrics).
I believe I miss something during the jni process, but I can't imagine. 

Here a short output what I mean - the first line is an output of autoclass('android.util.DisplayMetrics') and the next lines are an output of autoclass('org.renpy.android.Hardware'):

[INFO              ] TEST: DisplayMetrics ['DENSITY_DEFAULT', 'DENSITY_DEVICE', 'DENSITY_HIGH', 'DENSITY_LOW', 'DENSITY_MEDIUM', 'DENSITY_XHIGH', '__class__', '__cls_storage', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__javaclass__', '__javaconstructor__', '__module__', '__new__', '__pyx_vtable__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'density', 'densityDpi', 'equals', 'getClass', 'hashCode', 'heightPixels', 'notify', 'notifyAll', 'scaledDensity', 'setTo', 'setToDefaults', 'toString', 'updateMetrics', 'wait', 'widthPixels', 'xdpi', 'ydpi']
[INFO              ] TEST: Hardware ['__class__', '__cls_storage', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__javaclass__', '__javaconstructor__', '__module__', '__new__', '__pyx_vtable__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'equals', 'getClass', 'hashCode', 'notify', 'notifyAll', 'toString', 'wait']
[INFO              ] TEST: getClass ['__class__', '__cls_storage', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__javaclass__', '__metaclass__', '__module__', '__new__', '__pyx_vtable__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'forName', 'getConstructors', 'getDeclaredFields', 'getDeclaredMethods', 'getFields', 'getMethods', 'getName']
[INFO              ] TEST: getDeclaredMethods [<Method at 0x469d5d50 jclass=java/lang/reflect/Method jself=<jnius.jnius.LocalRef object at 0x469b7bc0>>, <Method at ... ]
[INFO              ] TEST: Method.getName = accelerometerEnable
[INFO              ] TEST: Method = ['__class__', '__cls_storage', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__javaclass__', '__metaclass__', '__module__', '__new__', '__pyx_vtable__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'getModifiers', 'getName', 'getParameterTypes', 'getReturnType', 'toString']

Thanks in advance,
Greetings
Nik

Nik Klever

unread,
Sep 4, 2012, 3:11:59 AM9/4/12
to python-...@googlegroups.com, kivy-...@googlegroups.com, kivy...@googlegroups.com
Hi Mathieu,

I found the failure by myself:

autoclass finds methods and fields only if they are declared public !

In addition to this, I provided a patch to Hardware.java in python-for-android to support pyjnius as you described in the docs.

Greetings and thanks again for pyjnius !
Nik

Amirouche Boubekki

unread,
Sep 28, 2012, 9:34:32 AM9/28/12
to python-...@googlegroups.com, kivy-...@googlegroups.com, kivy...@googlegroups.com
Héllo,

I'm wondering why you are not using JPype [http://jpype.sourceforge.net/documentation.html], any reasons ? What  are the advantages of this implementation ? Is it usable outside kivy/android ? What about performance ? 

Gabriel Pettier

unread,
Sep 28, 2012, 11:48:39 AM9/28/12
to kivy-...@googlegroups.com
So after some review, it appears we weren't aware of jpype existence,
our search for a solution of this kind only returned py4j, which one we
disliked for a few reasons.

Now, for jpype, it seems to be quite similar to pyjnius in conception,
except for a design apparently more inclined toward java types than
pyjnius, and a conception based on C++, instead of cython for pyjnius.
Also, pyjnius hide more stuff like loading the jvm (and it reuses the
existing one on android). On the other hand, jpype allow implementing
Java interfaces from python, something pyjnius doesn't currently do
(but work has been started on that).

For questions, yes, pyjnius is usable outside of kivy and android, it's
an independant project, although it has been built to make kivy more
usable on android (by easing access to android api). For performances,
we think the overhead of pyjnius is quite low, but i don't think they
should be significatively different between the two projects, if none
of us did a too bad job.
> --
>
>
>


noel...@gmail.com

unread,
Feb 15, 2016, 10:29:05 AM2/15/16
to Kivy users support, python-...@googlegroups.com, kivy...@googlegroups.com
Hi,

I think there is some issues by the installing, there is some Cython issues, compiling jni.h

Kind regards
Kevin
Reply all
Reply to author
Forward
0 new messages