NewsCrawler.org

Flutter can now call native APIs more directly: Dart Direct Native Interop

Direct Native Interop has been in the making for several years: in 2024, Dart 3.5 began fleshing out direct interop with C, Java, Kotlin and Objective-C; in May 2025, Dart 3.8 officially announced the early-access program for FFIgen and JNIgen; and by 2026 the official documentation has consolidated the interop approaches for C, Java, Objective-C/Swift and JavaScript under dart.dev/interop while the surrounding toolchain keeps being refined.

Dart can now bypass the traditional Platform Channel[1] message-passing flow and call native methods directly through FFI[2] or JNI[3], and FFIgen and JNIgen generate Dart bindings from native interfaces, so developers no longer have to maintain Kotlin and Swift bridge code by hand. Flutter's thread architecture has been adjusted in step, with the UI thread and the Platform thread merged. In 2026, Pigeon 29 arrived with official experimental support for FFI/JNI Direct Native Interop, meaning the implementation is close to rolling out at scale and the two-way path Dart → generated binding → JNI/FFI → Kotlin/Swift is set to become the mainstream route. After switching on useJni for Kotlin and useFfi for Swift in the PigeonOptions configuration, running dart run pigeon calls jnigen and ffigen to generate the Kotlin and Swift bridge code together with the Dart bindings. With a JNIgen-generated binding, a Dart call such as Example.sum(17, 25) goes through JNI straight into the Java VM, and FFIgen can read a header such as AVAudioPlayer.h and expose the Objective-C API as a Dart API, letting Dart build an NSURL and an NSString and drive AVAudioPlayer through AVAudioPlayer.alloc().initWithContentsOfURL(fileUrl). Swift takes more work than Objective-C because Dart FFI targets the C ABI, so Swift classes still have to be bridged through Objective-C-compatible interfaces marked with @objc.

Dart 3.13 adds @RecordUse and package:record_use, which let native FFI bindings take part in the reachability analysis performed during ahead-of-time (AOT) compilation. Unused Dart wrapper functions could already be removed by tree shaking[4], but native libraries had to be packaged whole; with Link Hook and native toolchains that support trimming, unused native symbols can now be stripped as well, reducing the size of the build artifacts. The author considers the support very good overall, just slow to arrive, and notes it matters most for plugins that depend on SQLite, cryptography or audio/video libraries — though it requires the matching build and link configuration, and upgrading Dart alone does not trim every native library automatically.

Read the original juejin.cn

Discussion

Most commenters simply praise the update, but one questions the performance benefit, arguing that JNI calls carry their own overhead so the direct path may actually be slower.

Sources

  • CNJuejin ZH

    Flutter 可以更直接地调用原生 API 了,Dart Direct Native Interop

    9 likes717 views3 comments5 saves

Published: First seen: Читать по-русски

Open as a page