pub repository
mirror.p-hatan.comjni
0.15.1A library to access JNI from Dart and Flutter that acts as a support library for package:jnigen.
Readme
Build Status Coverage Status pub package package publisher
JNI
This is a support library to access JNI from Dart / Flutter code. This provides the common infrastructure to bindings generated by JNIgen, as well as some utility methods.
This library contains:
- Functions to access the JNIEnv and JavaVM variables from JNI, and wrapper functions to those provided by JNI. JNIEnv is exposed via
GlobalJniEnvtype which provides a thin abstraction over JNIEnv, so that it can be used from multiple threads. - Functions to spawn a JVM on desktop platforms (
Jni.spawn). - Some Android-specific helpers (get application context and current activity references).
JObjectclass, which provides base class for classes generated by JNIgen.- Commonly used Java classes like
JList,JMap,JInteger, ...
Apart from being the base library for code generated by JNIgen this can also be used for one-off uses of the JNI and debugging. To generate type-safe bindings from Java libraries, use JNIgen.
Documentation
The test/ directory contains files with comments explaining the basics of this module, and the example/ directory contains a flutter example which also touches some Android-specifics.
Using this library assumes some familiarity with JNI - it's threading model and object references, among other things.
Migrating to 0.8.0
Check the changelog.
The API for standalone use of package:jni (without package:jnigen) has
changed to use JClass.forName for finding classes. Find the methods and fields
by accessing them from the retrieved JClass. For example find an instance
method with a name and a signature using jClass.instanceMethodId. Then you can
call the resulting JInstanceMethodId to call the instance method.
Changelog
0.15.1
- Breaking Change: Removed the
engineIdargument fromJni.androidApplicationContextas the application context is not dependant on the engine ID.
0.15.0
- Breaking Change: Made
Jni.envinternal. - Breaking Change: Renamed
JObjTypetoJType. - Breaking Change: Made all of the type classes internal.
- Breaking Change: Removed
Jni.getApplicationClassLoader(),Jni.getCurrentActivity(), andJni.getCachedApplicationContext(). Instead useJni.androidApplicationContext(engineId)to access the application context and useJni.androidActivity(engineId)to acccess the activity. - Update to the latest lints.
0.14.2
- Fixed a bug where certain method of
JList,JSet, andJMapdid not work with nullable elements. - Fixed a bug in
JList.lastIndexOf.
0.14.1
- Updated
bin/setup.dartto use Gradle instead of Maven for building Java sources. Added gradle executables and bootstrap jars #2003 - Added
JObject.isInstanceOfwhich checks whether aJObjectis an instance of a java class. - Fixed a bug where Java interfaces implemented in on the main thread in Dart could deadlock when invoked from the main thread outside the context of a Dart isolate.
0.14.0
- Added
DynamicLibraryLoadErrorwhich is thrown when the dynamic library fails to load.HelperNotFoundErrorwill only be thrown when the helper library cannot be found. - Update the README.md to include info about generating bindings for built-in java types.
- Do not require a
dylibDirwhen runningJni.spawnfrom Dart standalone, instead use the default value ofbuild/jni_libs. - Added
JArray.of, which allows aJArrayto be constructed from anIterable. - Added
JObject.isA, which checks whether aJObjectis an instance of a java typeclass. - Do not require JAWT when building for desktop.
- Added
JByteArray.from, which allows aJByteArrayto be constructed from anIterable<int>.
0.13.0
-
Breaking Change: Separated primitive arrays from object arrays. Previously, a primitive array like an array of bytes was typed
JArray<jbyte>. NowJArray<T>only acceptsJObjects as types and primitive arrays like arrays of bytes have their own types such asJByteArray.This enables all arrays to implement
Iterablewhich makes it possible to use them in a for-loop or use methods such asmapon them. -
Added nullable type classes for all Java objects.
-
Fixed a problem where interfaces implemented in Dart would crash when calling the default object methods:
equals,hashCode, andtoString.
0.12.2
- Added
JniUtils.fromReferenceAddresswhich helps with sendingJObjects through method channels. You can send the address of the pointer aslongand reconstruct the class using the helper method. - Fixed a bug where it would be possible for a type class inference to fail.
- Return 'null' when calling
toStringon a null object.
0.12.0
- Breaking Change: Renamed
castTotoas. - Renamed library
internal_helpers_for_jnigento_internal. - Using 16KB page size to support Android 15.
- Added
JImplementerwhich enables building an object that implements multiple Java interfaces.
0.11.0
- Breaking Change Removed
Jni.accessors. - Made most
Jni.envmethods into leaf functions to speed up their execution. - Removed the dependency on
kotlin_gradle_plugin.
0.10.1
- Fixed an issue with
JObject.castTowhere the type checking could fail in debug mode. - Used
package:dart_flutter_team_lints.
0.9.3
- Added lifetime event handling for the thread-local JNI env. Now
jvm.DetachNativeThreadis called when the thread detaches as recommended here. - Removed
JValueChar.fromStringconstructor.
0.9.2
- Bumped
minSdkto 21.
0.9.1
- Fixed compilation on macOS for consumers that don't use JNI on macOS (which is still not supported) (#1122).
0.9.0
- Breaking Change
(#1004): Changed the return
type
operator []ofJArray<jchar>tointinstead ofString. Similarly, change the argument type ofoperator []=to acceptint. - Added
getRangemethod toJArrayof primitive types that returns aTypedDatalist depending on the kind of the array. - Improved the performance of
JArray'ssetRangeandoperator []=.
0.8.0
-
Breaking Change (#981):
-
JObject.referencenow returns aJReferenceinstead ofPointer<Void>. -
.fromRefconstructors are now called.fromReferenceand they take aJReferenceinstead ofPointer<Void>. -
JObjectreflective field retrieving and method calling methods are removed. UseJClassAPI instead. -
The following
Jni.accessorsmethods have been removed:getClassOfgetMethodIDOfgetStaticMethodIDOfgetFieldIDOfgetStaticFieldIDOfnewObjectWithArgscallMethodWithArgscallStaticMethodWithArgs
Instead use the
JClassAPI. -
Jni.findJClassis replaced withJClass.forName(String name) -
JClasshas been refactored. Instead of directly calling methods, getting and setting fields, useJClass.instanceMethodId,JClass.staticMethodId,JClass.constructorId,JClass.instanceFieldId, andJClass.staticFieldIdto first get access to the member. -
Renamed
JObject.getClass()toJObject.jClass. -
Removed
Jni.deleteAllRefs.
-
-
Breaking Change (#548): Converted various
Exceptions intoErrors:UseAfterReleaseException->UseAfterReleaseErrorDoubleReleaseException->DoubleReleaseErrorSpawnException->JniError(It's now asealed class)JNullException->JNullErrorInvalidCallTypeException->InvalidCallTypeErrorHelperNotFoundException->HelperNotFoundErrorJvmExistsException->JniVmExistsErrorNoJvmInstanceException->NoJvmInstanceError
-
Breaking Change: Removed
InvalidJStringException. -
Breaking Change:
JTypeis nowsealed. -
Breaking Change: Primitive types and their type classes are now
final. -
Breaking Change:
JArray.fillednow uses the generated type class of thefillobject and not its Java runtime type. -
JObjects now check the types usinginstanceofin debug mode when usingcastTo. -
Breaking Change:
Jni.initDLApi()is removed. -
Added the ability to share
JObjects across isolates.// This now works. final foo = 'foo'.toJString(); Isolate.run(() { // `foo` is usable from another isolate. print(foo); });
0.7.3
- Fixed a bug where
get(Static)MethodIDandget(Static)FieldIDcould access null and throw.
0.7.2
- Fixed a bug where reading non-null terminated strings would overflow.
- Use
package:dart_flutter_team_lints.
0.7.1
- Removed macOS Flutter plugin until package:jni supports it (#780).
0.7.0
- Breaking Change (#563):
Added
JBufferandJByteBufferclasses as default classes forjava.nio.Bufferandjava.nio.ByteBufferrespectively. - Breaking Change: Made the type classes
final. - Fixed a bug where
addAll,removeAllandretainAllinJSetwould run their respective operation twice. - Fixed a bug where
JList.insertAllwould not throw the potentially thrown Java exception.
0.6.1
- Depend on the stable version of Dart 3.1.
0.6.0
- Breaking Change (#707):
Renamed
delete*torelease*. - Added
PortProxyand related methods used for interface implementation. - Added the missing binding for
java.lang.Character.
0.5.0
- Breaking Change (#711):
Java primitive types are now all lowercase like
jint,jshort, ... - The bindings for
java.util.Set,java.util.Map,java.util.Listand the numeric types likejava.lang.Integer,java.lang.Boolean, ... are now included inpackage:jni.
0.4.0
- Type classes now have
superCountandsuperTypegetters used for type inference.
0.3.0
- Added
PortContinuationused forsuspend funin Kotlin. dartjninow depends ondart_api_dl.h.
0.2.1
- Added
.clang-formatto pub.
0.2.0
- Added array support
- Added generic support
JniXturned intoJXfor a more terse code.
0.1.1
- Windows support for running tests and examples on development machines.
0.1.0
- Initial version: Android and Linux support, JObject API
Installing
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.com
flutter pub add jnipubspec.yaml
# Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
name: jni
description: A library to access JNI from Dart and Flutter that acts as a support library for package:jnigen.
version: 0.15.1
repository: https://github.com/dart-lang/native/tree/main/pkgs/jni
issue_tracker: https://github.com/dart-lang/native/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Ajni
topics:
- interop
- ffi
- java
- kotlin
- jni
environment:
sdk: '>=3.3.0 <4.0.0'
flutter: '>=3.35.6'
dependencies:
args: ^2.5.0
ffi: ^2.1.3
meta: ^1.15.0
package_config: ^2.1.0
path: ^1.9.0
plugin_platform_interface: ^2.1.8
dev_dependencies:
dart_flutter_team_lints: ^3.5.2
ffigen:
path: ../ffigen
jnigen:
path: ../jnigen
logging: ^1.2.0
test: ^1.25.8
# The following section is specific to Flutter packages.
flutter:
plugin:
platforms:
linux:
ffiPlugin: true
windows:
ffiPlugin: true
android:
ffiPlugin: true
package: com.github.dart_lang.jni
pluginClass: JniPlugin
Version history
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 0.1.0 | 2022-09-12T19:38:21.510124Z | Download | b237560e99f3d0cc77dc094fc76de0a8c46ecadeb2b879ce387bba984cae6db4 |
| 0.1.1 | 2022-09-22T08:27:41.671613Z | Download | 47000d71954b2667e612639ad16facf72b360e62aa255c53220ef66e323f9cd8 |
| 0.2.0 | 2023-01-25T14:14:33.494654Z | Download | bea8d106d3894beffd4de5b8888e9ba81d3f3346564fee758b83cd7e1748d578 |
| 0.2.1 | 2023-01-26T15:50:43.550432Z | Download | 075d1622ce164b52d719d42a6cc55c605797658cd8841d6564764deb08ff4e27 |
| 0.3.0 | 2023-02-16T09:58:59.023236Z | Download | d13615809094831d7db722cd5eca384eede118ac38e551c04346ca177c7feb3d |
| 0.4.0 | 2023-05-02T13:55:33.452741Z | Download | 1b5c37083b6e55cdcd4d5c2d5d9d71ca1db407df01e0045bb2abf7f2329946d7 |
| 0.5.0 | 2023-05-31T19:05:14.914295Z | Download | a19d9f6374e745f9a14b80c8dec9f7c5ef1aa877b36be551a1335bdbf7b0143c |
| 0.6.0 | 2023-09-01T14:13:46.776536Z | Download | ef31e7787650ff69e551f36eb017e0cb5173d75210e77fe623cabb9b1d1ad72b |
| 0.6.1 | 2023-09-01T14:15:40.800854Z | Download | b4bed1f44e10743ad45b5251dd7e9b6b7f5c2697dd398a9cee785d3a05d2d02d |
| 0.7.0 | 2023-09-13T12:50:52.930348Z | Download | 6b3227764b3d8a1d848c5e8067a9cff17a6a428b96a7258488ef0611831baa72 |
| 0.7.1 | 2023-11-02T18:31:27.029917Z | Download | e63e1cca26a9fa0f9c95b55d3a796e5f08bf44b0f511819250664f43e276ffa2 |
| 0.7.2 | 2023-11-03T09:30:41.157109Z | Download | 0d88790bdf7e298aa65a9094c62b58ea231169a2deb84f23defc7d7955885b43 |
| 0.7.3 | 2024-03-20T04:24:30.914284Z | Download | 499558e919997adfc45809a66caf0b95b91393e23289dd2826b152f8f04e6611 |
| 0.8.0 | 2024-04-04T08:18:23.901456Z | Download | 63984817b9ac1d07a02d0a8da20b1d526ba1872d75c6667b4d667f4facc39222 |
| 0.9.0 | 2024-05-02T08:38:19.855145Z | Download | 4f20a803314e2e07d3a284b4841f1109bbc17784f111f02f1a37bd5ffd3176af |
| 0.9.1 | 2024-05-14T07:25:14.011775Z | Download | 2a99a0ccfbdeb7a40ecd3f3e3768a49d4915f747fec1303a810730053749f21c |
| 0.9.2 | 2024-05-14T07:54:32.240950Z | Download | 6f8e04339f96cb32879acb181c60b2c1e2310ff86f3c62bcd1afcfb4b28b665d |
| 0.9.3 | 2024-06-25T12:20:30.733708Z | Download | 85cf3fbc8ce837e09b4cfe59d688c0249a5b9f4cc9a9a5be38e2823f218988a8 |
| 0.10.0 | 2024-07-11T19:19:07.521600Z | Download | 08a0074b38315c4c011ee5ffcfdc23a40281288bb6e9fa44625a6f3d19ca2270 |
| 0.10.1 | 2024-07-11T20:18:27.238022Z | Download | f377c585ea9c08d48b427dc2e03780af2889d1bb094440da853c6883c1acba4b |
| 0.11.0 | 2024-08-29T17:40:04.619570Z | Download | caefbbf9ed3b54b87bd3d479438e3837e7f8512015d04046a60b13c0b90d126f |
| 0.12.0 | 2024-10-01T17:08:17.674179Z | Download | 100fd0a76f6b3e75b2ae8dbdd69d50f3807386df5cba1fb536241ceae75e595b |
| 0.12.2 | 2024-11-15T15:26:29.723109Z | Download | 459727a9daf91bdfb39b014cf3c186cf77f0136124a274ac83c186e12262ac4e |
| 0.13.0 | 2024-12-13T12:03:33.105761Z | Download | 3296e4327bd74d57763ba71fd9efeb6f132634a27ea8754101e4ff3f8bca5faa |
| 0.14.0 | 2025-01-31T13:41:09.222656Z | Download | 302f50fd0595cb6440d12aac04b1b83a6de268cfc4721595450ba1d9bcdb3b1f |
| 0.14.1 | 2025-02-27T16:08:14.198864Z | Download | cfd663ec90d87ec6abd338da7e21189e503bf6753a0027c1862796f5826adba6 |
| 0.14.2 | 2025-05-23T09:03:19.059330Z | Download | d2c361082d554d4593c3012e26f6b188f902acd291330f13d6427641a92b3da1 |
| 0.15.0 | 2025-11-07T08:32:37.447784Z | Download | 5b8d72fae5cbcd9978af03b10af730ee85d04071f29e655ab423c878dacdf76b |
| 0.15.1 | 2025-11-12T08:43:22.228215Z | Download | b4d27985afeeb6d82b46ac8cbd66a368bde00b626f8641cce9995a124d61f3df |
| 0.15.2 | 2025-11-14T13:04:06.789306Z | Download | 8706a77e94c76fe9ec9315e18949cc9479cc03af97085ca9c1077b61323ea12d |
| 1.0.0 | 2026-04-01T00:07:50.862602Z | Download | c2230682d5bc2362c1c9e8d3c7f406d9cbba23ab3f2e203a025dd47e0fb2e68f |
| 1.0.1 | 2026-07-27T01:10:21.135385Z | Download | ca1efa31ef27a0b8ecbb4f8dcf3785e5ad1b8a54c64af9b72d287ae81f93a4f4 |
| 1.0.2 | 2026-07-28T04:51:11.065271Z | Download | 5bc9a9daac5ccfbd6a758377600b9f7fdce13d93f26e8012a8941014a5d978d1 |
| 1.0.3 | 2026-07-30T07:49:04.428207Z | Download | f038e58b4dc2c9037f50e233175086337e0b305e356d28211bf55f21c504cbd3 |
| 1.1.0 | 2026-10-01T02:54:48.874689Z | Download | 6b9ca0602fef0230e83149afbe3f9f8227e94f2bbe84a1da4f14d699742f402d |
| 1.1.1 | 2026-10-27T10:51:08.270676Z | Download | d8df6dab6847855512fe44dd9032b0bbe34dda7bf7597e41d30750879779813f |
Dependencies
args "^2.5.0"
ffi "^2.1.3"
meta "^1.15.0"
package_config "^2.1.0"
path "^1.9.0"
plugin_platform_interface "^2.1.8"