Skip to content

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

Done

Description

@dbebawy

Problem

An app that uses window_manager only on desktop still builds and ships cnativeapi on Android and iOS, where nothing calls it. cnativeapi's build hook compiles and bundles the library for every target OS, and an app has no way to turn that off.

With window_manager 0.5.2 the native code was desktop-only plugin code, so mobile builds carried none of it.

#25 and #44 made the library smaller, which helps. This request is different: let an app leave the library out on platforms where it doesn't use it, saving the remaining size and the C++ compile time.

Suggestion

Skipping mobile for everyone would break apps that use nativeapi's Android and iOS features, so I'd suggest an opt-in setting through the hooks package's user_defines:

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

In bindings/dart/cnativeapi/hook/build.dart, after targetOS is read:

if (_skippedPlatforms(input).contains(targetOS.name)) {
  return;
}
Set<String> _skippedPlatforms(BuildInput input) {
  final value = input.userDefines['skip_platforms'];
  if (value == null) return const {};
  final known = [
    for (final os in [OS.android, OS.iOS, OS.linux, OS.macOS, OS.windows])
      os.name,
  ];
  if (value is! List || !value.every(known.contains)) {
    throw FormatException(
      'hooks.user_defines.cnativeapi.skip_platforms must be a list of '
      '${known.join(', ')}; got $value',
    );
  }
  return value.cast<String>().toSet();
}

What I tried

I patched the published cnativeapi 0.6.2 this way and ran a small Dart program that imports nativeapi 0.6.2, on macOS arm64 with Dart 3.14.0:

skip_platforms Result
not set Library built, DisplayManager.instance.getAll() works
[macos], no nativeapi call Starts normally, library not built
[macos], calling getAll() ArgumentError: "Couldn't resolve native function … No asset with id 'package:cnativeapi/cnativeapi.dart' found"
[android, ios] Same as not set on macOS
[beos] Build hook fails

So a skipped platform only fails if the app calls into nativeapi there. I tested by skipping macOS. I haven't yet built or run an Android or iOS app with this, or measured the saving.

Would you accept this, and is this the shape you'd want? I'm happy to open a PR.

Activity

  1. dbebawy commented on Nov 23, 2026

    @dbebawy
    ContributorAuthor

    I've now tried this on Android and iOS, which I hadn't when I opened the issue.

    The branch is skip-platforms, on top of 0b6135f: the hook change, a README section, and 15 test cases for the setting. They use testCodeBuildHook, and pass on Linux, macOS and Windows (run).

    For the measurements I used a flutter create app with window_manager 0.7.1, the patched cnativeapi, 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
    libcnativeapi.so arm64-v8a 1,445,216 bytes not in the APK
    libcnativeapi.so armeabi-v7a 1,069,624 bytes not in the APK
    libcnativeapi.so x86_64 1,346,808 bytes not in the APK
    cnativeapi.framework binary, iOS release 781,136 bytes not in the app
    flutter build apk, two runs 5m17s, 5m08s 2m59s, 3m28s
    flutter build ios, two runs 4m26s, 2m34s 0m46s, 1m18s

    The build times are from shared GitHub runners, so they're only a rough guide.

    Each app then drew its first frame and called WindowManager.instance.getCurrent(), in a release build on an Android emulator and a debug build on an iOS simulator:

    Default Skipped
    Android starts, the call returns starts, the call throws ArgumentError
    iOS starts, the call returns starts, the call throws ArgumentError

    In the first run both iOS launch checks failed, with and without the setting, because my script only searched the console output. The second run checks for a file the app writes, and all four builds pass.

    I can open a PR from that branch if you'd like this.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions