Skip to main content

PyJNIus

PyJNIus is a Python library for accessing Java classes using the Java Native Interface (JNI).

PyJNIus is managed by the Kivy Team and can be used with python-for-android.

It can also be used independently of Kivy, on desktop and mobile platforms.

Backers on Open Collective Sponsors on Open Collective GitHub contributors Contributor Covenant

PyPI - Version PyPI - Python Version

Tests Tests (x86) Builds

Installation

pip install pyjnius

Quick overview

    >>> from jnius import autoclass
    >>> autoclass('java.lang.System').out.println('Hello world')
    Hello world
    
    >>> Stack = autoclass('java.util.Stack')
    >>> stack = Stack()
    >>> stack.push('hello')
    >>> stack.push('world')
    >>> print(stack.pop())
    world
    >>> print(stack.pop())
    hello

Usage with python-for-android

  • Get python-for-android
  • Compile a distribution with kivy (PyJNIus will be automatically added)

Then, you can do this kind of thing:

from time import sleep
from jnius import autoclass

Hardware = autoclass('org.renpy.android.Hardware')
print('DPI is', Hardware.getDPI())

Hardware.accelerometerEnable(True)
for x in range(20):
    print(Hardware.accelerometerReading())
    sleep(0.1)

It will output something like:

I/python  ( 5983): Android kivy bootstrap done. __name__ is __main__
I/python  ( 5983): Run user program, change dir and execute main.py
I/python  ( 5983): DPI is 160
I/python  ( 5983): [0.0, 0.0, 0.0]
I/python  ( 5983): [-0.0095768067985773087, 9.3852710723876953, 2.2218191623687744]
I/python  ( 5983): [-0.0095768067985773087, 9.3948478698730469, 2.2218191623687744]
I/python  ( 5983): [-0.0095768067985773087, 9.3948478698730469, 2.2026655673980713]
I/python  ( 5983): [-0.028730420395731926, 9.4044246673583984, 2.2122423648834229]
I/python  ( 5983): [-0.019153613597154617, 9.3852710723876953, 2.2026655673980713]
I/python  ( 5983): [-0.028730420395731926, 9.3852710723876953, 2.2122423648834229]
I/python  ( 5983): [-0.0095768067985773087, 9.3852710723876953, 2.1835119724273682]
I/python  ( 5983): [-0.0095768067985773087, 9.3756942749023438, 2.1835119724273682]
I/python  ( 5983): [0.019153613597154617, 9.3948478698730469, 2.2122423648834229]
I/python  ( 5983): [0.038307227194309235, 9.3852710723876953, 2.2218191623687744]
I/python  ( 5983): [-0.028730420395731926, 9.3948478698730469, 2.2026655673980713]
I/python  ( 5983): [-0.028730420395731926, 9.3852710723876953, 2.2122423648834229]
I/python  ( 5983): [-0.038307227194309235, 9.3756942749023438, 2.2026655673980713]
I/python  ( 5983): [0.3926490843296051, 9.3086557388305664, 1.3311761617660522]
I/python  ( 5983): [-0.10534487664699554, 9.4331550598144531, 2.1068975925445557]
I/python  ( 5983): [0.26815059781074524, 9.3469638824462891, 2.3463177680969238]
I/python  ( 5983): [-0.1149216815829277, 9.3852710723876953, 2.31758713722229]
I/python  ( 5983): [-0.038307227194309235, 9.41400146484375, 1.8674772977828979]
I/python  ( 5983): [0.13407529890537262, 9.4235782623291016, 2.2026655673980713]

Advanced example

When you use autoclass, it will discover all the methods and fields of the class and resolve them. You may want to declare and use only what you need. The previous example can be done manually as follows:

from time import sleep
from jnius import MetaJavaClass, JavaClass, JavaMethod, JavaStaticMethod

class Hardware(JavaClass):
    __metaclass__ = MetaJavaClass
    __javaclass__ = 'org/renpy/android/Hardware'
    vibrate = JavaStaticMethod('(D)V')
    accelerometerEnable = JavaStaticMethod('(Z)V')
    accelerometerReading = JavaStaticMethod('()[F')
    getDPI = JavaStaticMethod('()I')

# use that new class!
print('DPI is', Hardware.getDPI())

Hardware.accelerometerEnable()
for x in range(20):
    print(Hardware.accelerometerReading())
    sleep(0.1)

You can use the signatures method of JavaMethod and JavaMultipleMethod, to inspect the discovered signatures of a method of an object

>>> String = autoclass('java.lang.String')
>>> dir(String)
['CASE_INSENSITIVE_ORDER', '__class__', '_JavaClass__cls_storage', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__javaclass__', '__javaconstructor__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__pyx_vtable__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'charAt', 'checkBounds', 'clone', 'codePointAt', 'codePointBefore', 'codePointCount', 'compareTo', 'compareToIgnoreCase', 'concat', 'contains', 'contentEquals', 'copyValueOf', 'empty', 'endsWith', 'equals', 'equalsIgnoreCase', 'finalize', 'format', 'getBytes', 'getChars', 'getClass', 'hashCode', 'indexOf', 'indexOfSupplementary', 'intern', 'isEmpty', 'join', 'lastIndexOf', 'lastIndexOfSupplementary', 'length', 'matches', 'nonSyncContentEquals', 'notify', 'notifyAll', 'offsetByCodePoints', 'regionMatches', 'registerNatives', 'replace', 'replaceAll', 'replaceFirst', 'split', 'startsWith', 'subSequence', 'substring', 'toCharArray', 'toLowerCase', 'toString', 'toUpperCase', 'trim', 'valueOf', 'wait']
>>> String.format.signatures()
[(['java/util/Locale', 'java/lang/String', 'java/lang/Object...'], 'java/lang/String'), (['java/lang/String', 'java/lang/Object...'], 'java/lang/String')]

Each pair contains the list of accepted arguments types, and the returned type.

Troubleshooting

Make sure a Java Development Kit (JDK) is installed on your operating system if you want to use PyJNIus on desktop. OpenJDK is known to work, and the Oracle Java JDK should work as well.

On Windows, make sure JAVA_HOME points to your Java installation, so PyJNIus can locate the jvm.dll file allowing it to start Java. This shouldn't be necessary on macOS and Linux, but in case PyJNIus fails to find it, setting JAVA_HOME should help.

License

PyJNIus is MIT licensed, actively developed by a great community and is supported by many projects managed by the Kivy Organization.

Documentation

Documentation for this repository.

Support

Are you having trouble using PyJNIus or any of its related projects in the Kivy ecosystem? Is there an error you don’t understand? Are you trying to figure out how to use it? We have volunteers who can help!

The best channels to contact us for support are listed in the latest Contact Us document.

Contributing

PyJNIus is part of the Kivy ecosystem - a large group of products used by many thousands of developers for free, but it is built entirely by the contributions of volunteers. We welcome (and rely on) users who want to give back to the community by contributing to the project.

Contributions can come in many forms. See the latest Contribution Guidelines for how you can help us.

Code of Conduct

In the interest of fostering an open and welcoming community, we as contributors and maintainers need to ensure participation in our project and our sister projects is a harassment-free and positive experience for everyone. It is vital that all interaction is conducted in a manner conveying respect, open-mindedness and gratitude.

Please consult the latest Code of Conduct.

Contributors

This project exists thanks to all the people who contribute. [Become a contributor].

Backers

Thank you to all of our backers! 🙏 [Become a backer]

Sponsors

Special thanks to all of our sponsors, past and present. Support this project by [becoming a sponsor].

Here are our top current sponsors. Please click through to see their websites, and support them as they support us.

Release files for pyjnius 1.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for pyjnius 1.8.0
File
pyjnius-1.8.0-cp314-cp314-android_24_x86_64.whl CPython 3.14 CPython 3.14 Android API level 24+ x86-64 Details
pyjnius-1.8.0-cp314-cp314-android_24_arm64_v8a.whl CPython 3.14 CPython 3.14 Android API level 24+ ARM64 v8a Details
pyjnius-1.8.0-cp313-cp313-android_24_x86_64.whl CPython 3.13 CPython 3.13 Android API level 24+ x86-64 Details
pyjnius-1.8.0-cp313-cp313-android_24_arm64_v8a.whl CPython 3.13 CPython 3.13 Android API level 24+ ARM64 v8a Details

Total release size: 904.0 kB

Release files / pyjnius-1.8.0-cp314-cp314-android_24_x86_64.whl

Download URL pyjnius-1.8.0-cp314-cp314-android_24_x86_64.whl
Size 233.5 kB
Tags Android API level 24+ x86-64 CPython 3.14
SHA-256 checksum
How to use checksums
cc59aa9d11077ce24a1d655b8113bbdffa946155c6d7ad9f17392bdb07095e20
BLAKE2b-256 checksum
How to use checksums
8ed792a1dc41c1a3613b1f34df9f40c6a8e36393d19f975f3903230ad31bfebd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 26, 2026.

Transparency log

Release files / pyjnius-1.8.0-cp314-cp314-android_24_arm64_v8a.whl

Download URL pyjnius-1.8.0-cp314-cp314-android_24_arm64_v8a.whl
Size 219.9 kB
Tags Android API level 24+ ARM64 v8a CPython 3.14
SHA-256 checksum
How to use checksums
2ccc7b60bd42f4bcf9ea3435384122fc30456d620000a8a81534be00b7a9df43
BLAKE2b-256 checksum
How to use checksums
fb37bb406e4e5a49cbb5ecfeefc237b0624e23ae2a160694be0d6c6336578bc6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 26, 2026.

Transparency log

Release files / pyjnius-1.8.0-cp313-cp313-android_24_x86_64.whl

Download URL pyjnius-1.8.0-cp313-cp313-android_24_x86_64.whl
Size 233.0 kB
Tags Android API level 24+ x86-64 CPython 3.13
SHA-256 checksum
How to use checksums
527c128e5038b7085a7c8ed2a2c064495fefc109b749ae72c814b94c487a8e10
BLAKE2b-256 checksum
How to use checksums
b977e83beec85608a9e5355f52fae4ec3dfe04f41572ddb930527b0542706541
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 26, 2026.

Transparency log

Release files / pyjnius-1.8.0-cp313-cp313-android_24_arm64_v8a.whl

Download URL pyjnius-1.8.0-cp313-cp313-android_24_arm64_v8a.whl
Size 217.6 kB
Tags Android API level 24+ ARM64 v8a CPython 3.13
SHA-256 checksum
How to use checksums
22a0b5f96446f3d4aef9fa1d8ae5a3412cb86cbd9e1a18e6287ad8a30b8dcca9
BLAKE2b-256 checksum
How to use checksums
180009695fe036afc3ccf18c636fe15bfa6539e4d548f8b451054aa248944b74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 26, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.8.0 This release

4 release files

1.5.0

42 release files

1.4.1

26 release files

1.4.0

19 release files

1.2.0

1 release file

1.1.4

1 release file

1.1.3

1 release file

1.1.1

1 release file

1.0.2

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page