text stringlengths 1 474 |
|---|
that implements 32-bit addition and then |
exposes it through a Dart plugin named “native_add”.<topic_end> |
<topic_start> |
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 using DynamicLibrary.executable or |
DynamicLibrary.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 via DynamicLibrary.open.API documentation is available from the Dart dev channel: |
Dart API reference documentation.On Android, only dynamic libraries are supported |
(because the main executable is the JVM, |
which we don’t link to statically).<topic_end> |
<topic_start> |
Create an FFI plugin |
To create an FFI plugin called “native_add”, |
do the following:info 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 in native_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 marked extern "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, the native_add/android/build.gradle links the code.The native code is invoked from dart in lib/native_add_bindings_generated.dart.The bindings are generated with package:ffigen.<topic_end> |
<topic_start> |
Other use cases |
<topic_end> |
<topic_start> |
Platform library |
To link against a platform library, |
use the following instructions:Load the library using DynamicLibrary.open. |
For example, to load OpenGL ES (v3):You might need to update the Android manifest |
file of the app or plugin if indicated by |
the documentation.<topic_end> |
<topic_start>First-party library |
The process for including native code in source |
code or binary form is the same for an app or |
plugin.<topic_end> |
<topic_start>Open-source third-party |
Follow the Add 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 or ndk-build).<topic_end> |
<topic_start>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:<topic_end> |
<topic_start> |
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 compress libflutter.so and libapp.so—this |
leads to smaller APK size but larger on device size.Shared objects from third parties can change this default |
setting with android:extractNativeLibs="true" in their |
AndroidManifest.xml and stop the compression of libflutter.so, |
libapp.so, and any user-added shared objects. |
To re-enable compression, override the setting in |
your_app_name/android/app/src/main/AndroidManifest.xml |
in the following way.<topic_end> |
<topic_start> |
Other Resources |
To learn more about C interoperability, check out these videos: |
<topic_end> |
<topic_start>Hosting native Android views in your Flutter app with Platform Views |
Platform views allow you to embed native views in a Flutter app, |
so you can apply transforms, clips, and opacity to the native view |
from Dart.This allows you, for example, to use the native |
Google Maps from the Android SDK |
directly inside your Flutter app.info Note |
This page discusses how to host your own native Android views |
within a Flutter app. |
If you’d like to embed native iOS views in your Flutter app, |
see Hosting native iOS views.Flutter supports two modes: |
Hybrid composition and virtual displays.Which one to use depends on the use case. |
Let’s take a look:Hybrid composition |
appends the native android.view.View to the view hierarchy. |
Therefore, keyboard handling, and accessibility work out of the box. |
Prior to Android 10, this mode might significantly |
reduce the frame throughput (FPS) of the Flutter UI. |
For more context, see Performance.Virtual displays |
render the android.view.View instance to a texture, |
so it’s not embedded within the Android Activity’s view hierarchy. |
Certain platform interactions such as keyboard handling |
and accessibility features might not work.To create a platform view on Android, |
use the following steps:<topic_end> |
<topic_start> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.