Invoke Java Native Interface APIs from Dart.

To use, import dart:jni.

Example:

JavaClass dateFormatClass = Java.getClass('java.text.SimpleDateFormat');
JavaObject format = dateFormatClass.newInstance('yyyy.MM.dd');
print(format.parse('2016.01.01').getYear());

This library provides a way to access Java classes that are accessible to the Android runtime hosting Flutter (including Android system APIs).

The library consists of two parts: a raw module that offers Dart access to low-level JNI functions, and a helper module that uses reflection to build Java object wrappers that act like standard Dart objects.

Raw JNI

Call JniClass.fromName to load a JNI class given its fully-qualified name. The resulting JniClass object provides methods for constructing instances, looking up field and method IDs, getting and setting static fields, and calling static methods. These methods closely match the JNI functions provided by the VM.

Getters/setters of object fields and calls to methods that return objects will return instances of JniObject. JniObject similarly offers wrappers of the JNI functions that get and set object fields and call object methods.

Array instances are represented by JniArray and its subclasses, which provide a Dart List interface backed by an underlying Java array.

Java Object Wrappers

Call Java.getClass to create a JavaClass instance that acts as a higher-level wrapper for a Java class. Using this wrapper, you can call static methods and read and write static fields using standard Dart field and method syntax. The wrapper will use reflection to locate the corresponding Java fields and methods and map Dart operations to JNI calls.

Use the JavaClass.newInstance method to construct instances of the class. newInstance will pass its arguments to the appropriate constructor.

Instances of Java objects are represented by JavaObject, which similarly exposes field and method accessors derived via reflection.

Dart boolean, number, and string types as well as JavaObject instances can be passed as arguments to wrapped Java methods. The JNI libraries will convert these arguments from Dart types to the matching Java type.

Dart does not support function overloading, and as a result the Java method wrappers need to locate the method on the underlying Java class that most closely matches the arguments passed to the wrapper. If there are multiple Java method overloads that could be suitable for a given set of arguments and the wrappers do not choose the desired overload, then you can obtain the JniObject wrapped by the JavaObject and use the raw JNI library to invoke the method.

Classes

Java

A class that provides access to Java objects from within Dart.

JavaClass

A wrapper for a JNI class that uses reflection to provide access to the class' static fields, methods, and constructors.

JavaObject

A wrapper for a JNI object that provides access to the object's fields and methods.

JniApi

A group of methods which invoke Java Native Interface APIs from Dart.

JniArray

Wrapper for a Java array.

JniBooleanArray

Wrapper for a Java boolean array.

JniByteArray

Wrapper for a Java byte array.

JniCharArray

Wrapper for a Java char array.

JniClass

Low-level wrapper for a Java class accessed via JNI. These methods map directly to the corresponding JNI functions. See the JNI documentation for more information.

JniDoubleArray

Wrapper for a Java double array.

JniFloat

Used to pass arguments of type "float" to Java methods.

JniFloatArray

Wrapper for a Java float array.

JniIntArray

Wrapper for a Java int array.

JniLongArray

Wrapper for a Java long array.

JniObject

Low-level wrapper for a Java object accessed via JNI. These methods map directly to the corresponding JNI functions. See the JNI documentation for more information.

JniObjectArray

Wrapper for a Java Object array.

JniShortArray

Wrapper for a Java short array.

JniString

Wrapper for a Java string.

Exceptions / Errors

JavaError