Skip to content
pubscout

jni

1.2.1, released today by dart.dev

Provides Dart/Flutter bindings to call native Java code via JNI.

$ flutter pub add jni
Source repository

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.

  • result_dart

    Kotlin and Swift inspired Result class for error handling in Dart and Flutter.

  • apollovm

    Portable multi-language VM to parse, run, translate, and compile Dart, Java, Kotlin, Go, C#, JS, and Python.

  • big_decimal

    A bugless implementation of BigDecimal in Dart based on Java's BigDecimal.

Works with

Packages commonly used together with jni: ones it builds on, ones built on it, and its own family.