jni
1.2.1, released today by dart.dev
Provides Dart/Flutter bindings to call native Java code via JNI.
What it does
Exposes the JNI environment and JavaVM to Dart, allowing creation of Java objects and calling methods. Includes utilities for spawning a JVM on desktop and common Java collection wrappers.
AI-written summary of version 1.1.2, based on the package's README and metadata. Report an inaccuracy
Use it when
- You need to call existing Java libraries from Flutter on Android or desktop.
- You are generating type‑safe bindings with JNIgen and need runtime support.
Skip it when
- You only need simple platform channels; a full JNI bridge is overkill.
- You target iOS or web where JNI is unavailable.
Strengths
- Low‑level access to Java without writing platform channel code
- Works on Android, Windows, and Linux
- Integrates with JNIgen for automated binding generation
Weaknesses
- Requires native build setup and familiarity with JNI
- Limited to platforms that support JVM (no iOS/web)
- Potential for memory‑management bugs if misused
Setup
- Kind
- Plugin (native code)
- Effort
- Medium
- Code generation
- No
- Direct dependencies
- 8
Add the plugin, ensure C/C++ toolchain is installed, configure Android NDK and desktop build scripts; no extra permissions needed beyond normal Android setup.
import 'package:jni/jni.dart';
void main() async {
final jni = await Jni.spawn(); // start JVM on desktop
final stringClass = JClass.forName('java.lang.String');
final ctor = stringClass.getConstructor('([B)');
final hello = ctor.newInstance([
JArray.fromList([104, 101, 108, 108, 111].map((c) => JByte(c)))
]);
print(hello.callMethod('toString', []));
}Alternatives
Packages that do the same job and could replace jni.
- ffi_leak_tracker
Tracks and detects native memory leaks in Dart FFI allocations.
81Compare - result_dart
Kotlin and Swift inspired Result class for error handling in Dart and Flutter.
69Compare - apollovm
Portable multi-language VM to parse, run, translate, and compile Dart, Java, Kotlin, Go, C#, JS, and Python.
76Compare - big_decimal
A bugless implementation of BigDecimal in Dart based on Java's BigDecimal.
64Compare
Works with
Packages commonly used together with jni: ones it builds on, ones built on it, and its own family.
- package_configjni depends on itRead and write Dart package configuration files.
- ffijni depends on itUtilities for working with Foreign Function Interface (FFI) code in Dart.
- argsjni depends on itLibrary for parsing raw command-line arguments into options and values using GNU and POSIX styles.
- plugin_platform_interfacejni depends on itReusable base class for platform interfaces of Flutter federated plugins.
- built_valueSame familyImmutable value types with builders, enum classes and JSON serialization for Dart.
- platformSame familyCross‑platform abstraction for querying runtime platform info in Dart.
- asyncSame familyUtility classes and helpers that extend Dart's async library.
- hooksSame familyDart API for the JSON-based protocol for package build and link hooks.