项目作者: kivy

项目描述 :
Access Java classes from Python
高级语言: Python
项目地址: git://github.com/kivy/pyjnius.git
创建时间: 2012-08-14T01:39:07Z
项目社区:https://github.com/kivy/pyjnius

开源协议:MIT License

下载


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.

[!WARNING]
The PyPI package name is now
pyjnius instead of jnius.

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

PyPI - Version
PyPI - Python Version

Tests
Tests (x86)/badge.svg)
Builds

Installation

  1. pip install pyjnius

Quick overview

  1. >>> from jnius import autoclass
  2. >>> autoclass('java.lang.System').out.println('Hello world')
  3. Hello world
  4. >>> Stack = autoclass('java.util.Stack')
  5. >>> stack = Stack()
  6. >>> stack.push('hello')
  7. >>> stack.push('world')
  8. >>> print(stack.pop())
  9. world
  10. >>> print(stack.pop())
  11. 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:

  1. from time import sleep
  2. from jnius import autoclass
  3. Hardware = autoclass('org.renpy.android.Hardware')
  4. print('DPI is', Hardware.getDPI())
  5. Hardware.accelerometerEnable(True)
  6. for x in range(20):
  7. print(Hardware.accelerometerReading())
  8. sleep(0.1)

It will output something like:

  1. I/python ( 5983): Android kivy bootstrap done. __name__ is __main__
  2. I/python ( 5983): Run user program, change dir and execute main.py
  3. I/python ( 5983): DPI is 160
  4. I/python ( 5983): [0.0, 0.0, 0.0]
  5. I/python ( 5983): [-0.0095768067985773087, 9.3852710723876953, 2.2218191623687744]
  6. I/python ( 5983): [-0.0095768067985773087, 9.3948478698730469, 2.2218191623687744]
  7. I/python ( 5983): [-0.0095768067985773087, 9.3948478698730469, 2.2026655673980713]
  8. I/python ( 5983): [-0.028730420395731926, 9.4044246673583984, 2.2122423648834229]
  9. I/python ( 5983): [-0.019153613597154617, 9.3852710723876953, 2.2026655673980713]
  10. I/python ( 5983): [-0.028730420395731926, 9.3852710723876953, 2.2122423648834229]
  11. I/python ( 5983): [-0.0095768067985773087, 9.3852710723876953, 2.1835119724273682]
  12. I/python ( 5983): [-0.0095768067985773087, 9.3756942749023438, 2.1835119724273682]
  13. I/python ( 5983): [0.019153613597154617, 9.3948478698730469, 2.2122423648834229]
  14. I/python ( 5983): [0.038307227194309235, 9.3852710723876953, 2.2218191623687744]
  15. I/python ( 5983): [-0.028730420395731926, 9.3948478698730469, 2.2026655673980713]
  16. I/python ( 5983): [-0.028730420395731926, 9.3852710723876953, 2.2122423648834229]
  17. I/python ( 5983): [-0.038307227194309235, 9.3756942749023438, 2.2026655673980713]
  18. I/python ( 5983): [0.3926490843296051, 9.3086557388305664, 1.3311761617660522]
  19. I/python ( 5983): [-0.10534487664699554, 9.4331550598144531, 2.1068975925445557]
  20. I/python ( 5983): [0.26815059781074524, 9.3469638824462891, 2.3463177680969238]
  21. I/python ( 5983): [-0.1149216815829277, 9.3852710723876953, 2.31758713722229]
  22. I/python ( 5983): [-0.038307227194309235, 9.41400146484375, 1.8674772977828979]
  23. 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:

  1. from time import sleep
  2. from jnius import MetaJavaClass, JavaClass, JavaMethod, JavaStaticMethod
  3. class Hardware(JavaClass):
  4. __metaclass__ = MetaJavaClass
  5. __javaclass__ = 'org/renpy/android/Hardware'
  6. vibrate = JavaStaticMethod('(D)V')
  7. accelerometerEnable = JavaStaticMethod('(Z)V')
  8. accelerometerReading = JavaStaticMethod('()[F')
  9. getDPI = JavaStaticMethod('()I')
  10. # use that new class!
  11. print('DPI is', Hardware.getDPI())
  12. Hardware.accelerometerEnable()
  13. for x in range(20):
  14. print(Hardware.accelerometerReading())
  15. sleep(0.1)

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

  1. >>> String = autoclass('java.lang.String')
  2. >>> dir(String)
  3. ['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']
  4. >>> String.format.signatures()
  5. [(['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.