Skip to content

Dart Native Assets for macOS/iOS#681

Open
robertmsale wants to merge 3 commits into
cunarist:mainfrom
robertmsale:native-assets-poc
Open

Dart Native Assets for macOS/iOS#681
robertmsale wants to merge 3 commits into
cunarist:mainfrom
robertmsale:native-assets-poc

Conversation

@robertmsale

Copy link
Copy Markdown

Changes

Implemented Dart Native Assets strictly for macOS/iOS

Partial completion of #641
Closes #673 (satisfies migrating away from CocoaPods)

This replaces the Apple-side Rust build path with a hook/build.dart Native Assets build hook in flutter_package, using native_toolchain_rust to compile the app-local native/hub crate during Flutter builds. The Apple proof path now works without CocoaPods driving Rust compilation, while keeping the existing native/hub project layout intact.

The implementation is intentionally scoped to macOS and iOS only. Other platforms remain on their existing build integrations, so the crate location stays consistently fixed at native/hub across the whole project. To match that existing cross-platform assumption, the hook does not introduce a configurable crate-path override. Instead it derives the consuming app root and resolves native/hub from there.

This also updates the package/toolchain floor to the first viable Native Assets generation and adds rust-toolchain.toml scaffolding for the hub crate template/example so the Rust hook build has a stable toolchain definition. The example app and template were adjusted only as needed to validate the Apple Native Assets path.

Before Committing

dart analyze flutter_package --fatal-infos
dart format .
cargo fmt
cargo clippy --fix --allow-dirty

Done ✅

Ran Code Formatter
sdk: ">=3.5.0 <4.0.0"
flutter: ">=3.24.0"
sdk: ">=3.10.0 <4.0.0"
flutter: ">=3.38.0"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raises the package floor to the first Dart/Flutter toolchain that can actually resolve Native Assets hooks, and adds the hook/build dependencies (code_assets, hooks, native_toolchain_rust, logging, path) needed to build the Rust crate from Dart instead of CocoaPods on Apple.

const _hubPath = 'native/hub';

void main(List<String> args) async {
await build(args, (input, output) async {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds the Native Assets build hook entrypoint for rinf. This is the core migration: Flutter now invokes this hook during Apple builds so Rust compilation is orchestrated from Dart’s native asset pipeline rather than from Podspec script phases.

final cratePath = _resolveCratePath(input);
logger.info('Building Rust crate from $cratePath');

await RustBuilder(

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uses native_toolchain_rust.RustBuilder to compile the hub crate as a code asset named rinf.dart, and maps Flutter’s build configuration to Rust build mode so debug simulator runs stay debug while linked/release builds use release mode.

});
}

String _resolveCratePath(BuildInput input) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolves the Rust crate path to the native/hub location and validates that it exists. This intentionally preserves Rinf’s longstanding cross-platform convention.

return './$relative';
}

Uri _resolveAppRoot(BuildInput input) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Derives the consuming app root from the hook runner output path, which is what allows the package-local hook to find the app-local native/hub crate. This is the key piece that makes Native Assets workable without extra pubspec.yaml configuration.


flutter:
config:
enable-swift-package-manager: false

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disables Flutter Swift Package Manager in the example app so the Apple Native Assets proof-of-concept is validated in isolation.

@@ -0,0 +1,22 @@
[toolchain]
channel = "1.91.0"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pinned a toolchain with all targets that rinf supports so testing is consistent across platforms. Included a template that can be added to new projects as well.

@robertmsale

Copy link
Copy Markdown
Author

CI was using an older version of flutter that did not have flutter.config.enable_swift_package_manager as a pubspec key. It also would not have worked with Dart Native Assets, so I bumped the CI flutter version to v3.38.1. It should pass now.

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.

Add Swift Package Manager support

1 participant