jni (experimental module)

This is a utility library to access JNI from Dart / Flutter code, intended as a supplement for jnigen code generator, as well as provide the common base components (such as managing the JVM instance) to the code generated by jni_gen.

This library contains:

  • functions to access the JNIEnv and JavaVM variables from JNI, and wrapper functions to those provided by JNI. (Jni.getEnv, Jni.getJavaVM).

  • Functions to spawn a JVM on desktop platforms (Jni.spawn).

  • Some utility functions to make it easier to work with JNI in Dart; eg: To convert a java string object to Dart string (mostly as extension methods on Pointer<JniEnv>).

  • Some Android-specific helpers (get application context and current activity references).

  • Some helper classes and functions to simplify one-off uses (JniObject and JniClass intended for calling functions by specifying the name and arguments. It will reduce some boilerplate when you're debugging. Note: this API is slightly incomplete).

This is intended for one-off / debugging uses of JNI, as well as providing a base library for code generated by jni_gen.

To interface a complete java library, look forward for jni_gen.

Platform support

The focus of this project is Flutter Android, since Flutter Android apps already have a JVM, and JNI enables interop with existing Java code and Android Platform APIs. This project also (partially) supports Linux desktop by spawning a JVM through JNI.

Version note

This library is at an early stage of development and we do not provide backwards compatibility of the API at this point.

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.

jni_gen

This library is a part of jni_gen - a 2022 GSoC project.

The broader aim of jni_gen is making Java APIs accessible from dart in an idiomatic way.