Expo Modules 2.0 replaces fbjni with Kolibri on Android
Expo has published a post on Kolibri, the JNI library that Expo Modules 2.0 uses on Android. The team built it to replace fbjni, which ships with React Native and is used by TurboModules. An earlier look at Expo Modules 2.0 covered the new way to write native modules on iOS and noted that Android was still in the works.
What Kolibri is
Kolibri has three parts: a C++ library that depends only on <jni.h>, a small Kotlin runtime, and a Kotlin compiler plugin that removes JNI boilerplate at compile time. It is written in C++20. The library keeps safety features such as managed references and automatic exception checks, but lets developers opt out of them on hot paths. A Ref class owns a JNI reference and deletes it when destroyed, while raw JNI references can also be passed directly.
How it differs from fbjni
- Exception checks: Java methods are described with a name and a C++ function type. Adding
noexceptremoves the exception check at compile time, for example onhasNext()ofjava.util.Iterator. Expo measured the check at about 24 ns on anArrayList.size()call that takes 97 ns without it. - Explicit JNIEnv: Kolibri takes the
JNIEnvas an argument instead of looking it up on each call, as fbjni does. - Scopes for hybrid objects: Kolibri's hybrid objects, called
NativeObject, can be bound to a scope. When a JS runtime is destroyed, for instance on reload, the scope is invalidated and every bound object releases its resources, avoiding use-after-free bugs. - Pointer passing: With
@NativeMethod, the compiler plugin rewrites an external Kotlin method so the C++ pointer is passed as the first argument. Parameters marked@AsNativePointercross the boundary the same way. This takes a call into a hybrid object from 76–138 ns with fbjni to 2.7 ns. - Binary buffer: Instead of building Java objects one JNI call at a time, as TurboModules and Expo Modules 1.0 do, JS data is written into a per-thread direct
ByteBuffer, exposed viaBinaryBuffer.shared(), and read by Kotlin with no JNI calls. Kolibri defines a tagged format for booleans, numbers, strings, lists, maps and byte arrays.
Benchmarks
Tests ran on an Apple M3 Pro with JDK 17, on a desktop JVM rather than an Android device. Calls into C++ objects were 28 to 51 times faster than with fbjni. Calls from C++ into Java matched raw JNI within about 2%. Passing JS arguments through the binary buffer ranged from 1.5× faster for a 16-character string to 13.0× for an array of 1,000 numbers, with objects reaching Kotlin 3 to 5 times faster.
Tradeoffs
Expo notes that Kolibri is deliberately minimal and not a general-purpose JNI library, with almost no wrappers for java.lang classes. It adds one more library to apps, whereas fbjni already ships with React Native. In Expo's words: "In the end, we think the tradeoff is worth it."
A follow-up post is planned on how Expo Modules 2.0 is built on top of Kolibri. Feedback is requested in the #creating-expo-modules channel on the Expo Discord.
Links
Source: expo.dev