Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

docs.flutter.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.

Learn more

Flutter 3.41 is live! Check out theblog post!

Binding to native Android code using dart:ffi

To use C code in your Flutter program, use the dart:ffi library.

Warning

This page documents a legacy approach to C interop on Android.

Since Flutter 3.38, we recommend using apackage_ffi template withbuild hooks for C interop.

However, the legacy FFI plugin template (plugin_ffi) documented here is still useful if you need to:

  • Access the Flutter Plugin API.
  • Configure a Google Play services runtime on Android.

Flutter mobile and desktop apps can use thedart:ffi library to call native C APIs.FFI stands forforeign function interface. Other terms for similar functionality includenative interface andlanguage bindings.

Note

Before your library or program can use the FFI library to bind to native code, you must ensure that the native code is loaded and its symbols are visible to Dart. This page focuses on compiling, packaging, and loading Android native code within a Flutter plugin or app.

This tutorial demonstrates how to bundle C/C++ sources in a Flutter plugin and bind to them using the Dart FFI library on both Android and iOS. In this walkthrough, you'll create a C function that implements 32-bit addition and then exposes it through a Dart plugin named "native_add".

Dynamic vs static linking

#

A native library can be linked into an app either dynamically or statically. A statically linked library is embedded into the app's executable image, and is loaded when the app starts.

Symbols from a statically linked library can be loaded usingDynamicLibrary.executable orDynamicLibrary.process.

A dynamically linked library, by contrast, is distributed in a separate file or folder within the app, and loaded on-demand. On Android, a dynamically linked library is distributed as a set of.so (ELF) files, one for each architecture.

A dynamically linked library can be loaded into Dart viaDynamicLibrary.open.

API documentation is available from theDart API reference documentation.

On Android, only dynamic libraries are supported (because the main executable is the JVM, which we don't link to statically).

Create an FFI plugin

#

To create an FFI plugin called "native_add", do the following:

flutter create --platforms=android,ios,macos,windows,linux --template=plugin_ffi native_addcd native_add
Note

You can exclude platforms from--platforms that you don't want to build to. However, you need to include the platform of the device you are testing on.

This will create a plugin with C/C++ sources innative_add/src. These sources are built by the native build files in the various os build folders.

The FFI library can only bind against C symbols, so in C++ these symbols are markedextern "C".

You should also add attributes to indicate that the symbols are referenced from Dart, to prevent the linker from discarding the symbols during link-time optimization.__attribute__((visibility("default"))) __attribute__((used)).

On Android, thenative_add/android/build.gradle links the code.

The native code is invoked from dart inlib/native_add_bindings_generated.dart.

The bindings are generated withpackage:ffigen.

Other use cases

#

Platform library

#

To link against a platform library, use the following instructions:

  1. Find the desired library in theAndroid NDK Native APIs list in the Android docs. This lists stable native APIs.

  2. Load the library usingDynamicLibrary.open. For example, to load OpenGL ES (v3):

    dart
    DynamicLibrary.open('libGLES_v3.so');

You might need to update the Android manifest file of the app or plugin if indicated by the documentation.

First-party library

#

The process for including native code in source code or binary form is the same for an app or plugin.

Open-source third-party

#

Follow theAdd C and C++ code to your project instructions in the Android docs to add native code and support for the native code toolchain (either CMake orndk-build).

Closed-source third-party library

#

To create a Flutter plugin that includes Dart source code, but distribute the C/C++ library in binary form, use the following instructions:

  1. Open theandroid/build.gradle file for your project.
  2. Add the AAR artifact as a dependency.Don't include the artifact in your Flutter package. Instead, it should be downloaded from a repository, such as JCenter.

Android APK size (shared object compression)

#

Android guidelines in general recommend distributing native shared objects uncompressed because that actually saves on device space. Shared objects can be directly loaded from the APK instead of unpacking them on device into a temporary location and then loading. APKs are additionally packed in transit—that's why you should be looking at download size.

Flutter APKs by default don't follow these guidelines and compresslibflutter.so andlibapp.so—this leads to smaller APK size but larger on device size.

Shared objects from third parties can change this default setting withandroid:extractNativeLibs="true" in theirAndroidManifest.xml and stop the compression oflibflutter.so,libapp.so, and any user-added shared objects. To re-enable compression, override the setting inyour_app_name/android/app/src/main/AndroidManifest.xml in the following way.

xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.your_app_name">xmlns:tools="http://schemas.android.com/tools"package="com.example.your_app_name" ><!-- io.flutter.app.FlutterApplication is an android.app.Application that         calls FlutterMain.startInitialization(this); in its onCreate method.         In most cases you can leave this as-is, but you if you want to provide         additional functionality it is fine to subclass or reimplement         FlutterApplication and put your custom class here.--><applicationandroid:name="io.flutter.app.FlutterApplication"android:label="your_app_name"android:icon="@mipmap/ic_launcher">android:icon="@mipmap/ic_launcher"android:extractNativeLibs="true"tools:replace="android:extractNativeLibs">

Other Resources

#

To learn more about C interoperability, check out these videos:

Was this page's content helpful?

Unless stated otherwise, the documentation on this site reflects Flutter 3.41.2. Page last updated on 2025-12-23.View source orreport an issue.


[8]ページ先頭

©2009-2026 Movatter.jp