cnativeapi 0.6.3
cnativeapi: ^0.6.3 copied to clipboard
Raw Dart FFI bindings to the nativeapi C API. The native library is built from source by a build hook.
cnativeapi #
Raw Dart FFI bindings to the C API of libnativeapi, generated with ffigen.
This package provides low-level FFI bindings and is typically used as an internal dependency of
nativeapi. You generally don't need to depend on it directly.
It is a plain Dart package and does not depend on Flutter. Its build hook (hook/build.dart) compiles the nativeapi core into a shared library whenever a Dart or Flutter app that depends on it is built, and every function is an @Native external bound to that library.
Platform Support #
| Android | iOS | Linux | macOS | Windows |
|---|---|---|---|---|
| ✅ | ✅ | ✅ | ✅ | ✅ |
Building needs a C++17 toolchain for the target: Xcode on macOS and iOS, the Android NDK, Visual Studio on Windows, and on Linux clang or GCC plus the GTK 3, X11 and Xi development packages (found with pkg-config).
Leaving the library out on some platforms #
An app that uses nativeapi on only some of its platforms can skip building and bundling the native library on the others. List them in the app's pubspec.yaml, or in the workspace root's if the app is in a pub workspace:
hooks:
user_defines:
cnativeapi:
skip_platforms: [android, ios]
The names are android, ios, linux, macos and windows. On a skipped platform, importing the package works, but calling into nativeapi throws an ArgumentError, so guard those calls with a platform check.
Enabling the WinUI 3 backend on Windows #
On Windows, the build hook compiles the Win32 backend by default. An app can opt in to nativeapi-core's WinUI 3 backend (ContentDialog message dialogs, MenuFlyout menus, WinUI 3 views, AppWindow title-bar colors and AppNotifications) in its pubspec.yaml (or the pub workspace root's):
hooks:
user_defines:
cnativeapi:
winui3: true
When enabled on Windows, the hook restores the pinned Windows App SDK 1.6, C++/WinRT and WebView2 NuGet packages once into its shared build directory, generates the C++/WinRT projections, compiles core with NATIVEAPI_ENABLE_WINUI3, and bundles Microsoft.WindowsAppRuntime.Bootstrap.dll beside the executable. Target machines must have the Windows App Runtime 1.6 installed, and the host executable should declare PerMonitorV2 DPI awareness in its manifest (enabled by default in Flutter's Windows runner). On other platforms this setting is ignored.
Usage #
import 'package:cnativeapi/cnativeapi.dart';
See example/cnativeapi_example.dart; run it with dart run example/cnativeapi_example.dart.
For higher-level Dart APIs, use the nativeapi package instead.
Regenerating Bindings #
Bindings are generated from the C headers in core/src/capi/. From the repository root:
./codegen
or, for this package alone, ./codegen ffigen (set LIBCLANG_PATH if ffigen cannot find libclang).