Skip to content

Let apps skip building the native library on platforms they choose - #56

Merged
lijy91 merged 1 commit into
libnativeapi:mainfrom
dbebawy:skip-platforms
Nov 24, 2026
Merged

lijy91 merged 1 commit into
libnativeapi:mainfrom
dbebawy:skip-platforms

Conversation

@dbebawy

@dbebawy dbebawy commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #55

An app that calls nativeapi only on desktop, such as one using window_manager, still compiles and ships cnativeapi in its Android and iOS builds. This adds an opt-in setting to leave the library out on the platforms an app lists:

hooks:
  user_defines:
    cnativeapi:
      skip_platforms: [android, ios]

Nothing changes for apps that don't set it. On a skipped platform, importing the package works and calling into nativeapi throws ArgumentError. A value that isn't a list of known platform names fails the build with a message naming the setting.

Changes

  • hook/build.dart: return before compiling when the target is listed.
  • README.md: how to use it, and that calls on a skipped platform throw.
  • test/build_hook_test.dart: 15 cases, using testCodeBuildHook. They compile no C++ themselves, though flutter test still runs the package's build hook first. CI's melos run test will pick up cnativeapi, which had no test/ directory before.

I left the changelog alone, since it goes with the version bump at release.

Measurements

A flutter create app with window_manager 0.7.1 and Flutter 3.50.0 (run):

Default skip_platforms: [android, ios]
Android release APK (arm, arm64, x64) 46,985,920 bytes 43,086,233 bytes
cnativeapi binary, iOS release 781,136 bytes not in the app

Validation

  • The new tests, dart format and dart analyze on hook and test pass on Linux, macOS and Windows (run). That run's two iOS launch checks failed, with and without the setting, because the check only read console output. The later run above fixes the check, and its four builds pass.
  • With and without the setting, the app draws its first frame on an Android emulator (release) and an iOS simulator (debug). Calling WindowManager.instance.getCurrent() returns by default and throws ArgumentError when skipped.
  • Skipping macOS works under dart run. Skipping Linux and Windows is covered only by the hook tests. I haven't tried a physical device.

cnativeapi's build hook compiled and bundled the library for every target,
including Android and iOS builds of apps that call nativeapi only on desktop.
An app can now list the platforms to leave it out on in its pubspec, as
hooks.user_defines.cnativeapi.skip_platforms. Nothing changes for apps that
don't set it.
@lijy91
lijy91 merged commit 25ab251 into libnativeapi:main Nov 24, 2026
9 checks passed

lijy91 commented Nov 24, 2026

Copy link
Copy Markdown
Member

Thanks @dbebawy for the implementation, tests, and detailed validation!

I independently verified the Android release app on an emulator and the iOS debug app on a simulator, plus the unsigned iOS device release bundle. With skipping enabled, the native library is omitted, the app still renders its first frame, and native calls throw ArgumentError as documented. The workspace-root configuration also works.

I also verified real Dart processes on macOS, Windows, and Linux, including switching skipping on and off to check cached outputs. All 9 Dart CI checks passed, and this is now merged as 25ab2518, closing #55. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cnativeapi: let apps skip building the native library on platforms where they don't use it

2 participants