Skip to content

feat: add build hooks support to plugin template#411

Open
realmeylisdev wants to merge 13 commits intoVeryGoodOpenSource:mainfrom
realmeylisdev:feat/add-build-hooks-support
Open

feat: add build hooks support to plugin template#411
realmeylisdev wants to merge 13 commits intoVeryGoodOpenSource:mainfrom
realmeylisdev:feat/add-build-hooks-support

Conversation

@realmeylisdev
Copy link
Contributor

Description

Adds optional build hooks support for compiling native code in the very_good_flutter_plugin template, enabling developers to quickly ramp up when their plugin includes native assets.

Closes #392

Changes

Core Changes

  • ✨ Added use_build_hooks boolean variable to brick.yaml (defaults to false for backward compatibility)
  • 📦 Added build hooks dependencies (hooks, code_assets, native_toolchain_c) to platform packages when enabled

Platform Support

Added build hooks to all native platforms:

  • 🐧 Linux
  • 🍎 macOS
  • 🪟 Windows
  • 📱 iOS
  • 🤖 Android

Implementation

Each platform includes:

  • hook/build.dart - Build hook script that compiles C code using CBuilder
  • src/native_example.c - Example C function (add()) demonstrating FFI usage
  • Updated pubspec.yaml with conditional dependencies

Documentation

  • 📚 Added comprehensive README section explaining:
    • What build hooks are and their benefits
    • How to use the example native function
    • How to add more native code
    • Link to official Dart build hooks documentation

Benefits

Quick Ramp Up: Developers can immediately start using native code with pre-configured build hooks
Modern Best Practices: Uses the new stable build hooks system from Dart 3.10 / Flutter 3.38
Complete Example: Working C code example shows the full FFI workflow
Backward Compatible: Optional feature (off by default) doesn't affect existing workflows
Multi-Platform: Consistent implementation across all native platforms

Testing

To test this feature:

  1. Generate a plugin with build hooks enabled:

    very_good create flutter_plugin my_plugin --use_build_hooks true
  2. The generated plugin will include build hooks infrastructure for compiling native code

  3. Build the plugin to verify hooks execute correctly:

    cd my_plugin_linux
    dart run

Requirements

  • ✅ All CI/CD checks are passing (to be verified by CI)
  • ✅ There is no drop in test coverage percentage (to be verified by CI)

References

@realmeylisdev realmeylisdev requested a review from a team as a code owner December 21, 2025 09:59
@omartinma
Copy link
Contributor

Thanks for the contribution @meylis1998, we will proceed with the review of this shortly!

Copy link
Member

@marcossevilla marcossevilla left a comment

Choose a reason for hiding this comment

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

thanks for opening this PR! I'll take a closer look later today, in the meantime, can we leave upgrades to VGA for another PR?

Add optional build hooks support for compiling native code in the
very_good_flutter_plugin template. This enables developers to quickly
ramp up when their plugin includes native assets.

Features:
- New use_build_hooks variable in brick.yaml (defaults to false)
- Build hooks dependencies (hooks, code_assets, native_toolchain_c)
- Example hook/build.dart for all native platforms (Linux, macOS,
  Windows, iOS, Android)
- Example C source file (src/native_example.c) with an add() function
- Documentation in README explaining build hooks usage

Build hooks are automatically invoked during dart/flutter build,
allowing seamless compilation of native code alongside Dart code.

Closes VeryGoodOpenSource#392
@realmeylisdev realmeylisdev force-pushed the feat/add-build-hooks-support branch from c98a939 to 41743bd Compare January 7, 2026 18:45
@realmeylisdev
Copy link
Contributor Author

Done! Removed the VGA upgrade from this PR and created a separate PR for it: #417

@marcossevilla
Copy link
Member

will wait for @ryzizub to give his review by EOW in order to approve 👍

@marcossevilla marcossevilla requested a review from ryzizub January 8, 2026 20:00
@realmeylisdev
Copy link
Contributor Author

realmeylisdev commented Jan 8, 2026

will wait for @ryzizub to give his review by EOW in order to approve 👍

@ryzizub inspired, encouraged to do all these. Huge thanks to him

Copy link
Contributor

@ryzizub ryzizub left a comment

Choose a reason for hiding this comment

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

Overally looks great! Good job, im just getting some dependency issues

@realmeylisdev realmeylisdev requested a review from ryzizub January 9, 2026 13:44
@realmeylisdev
Copy link
Contributor Author

realmeylisdev commented Jan 9, 2026

@ryzizub feel free to review my other PRs. @marcossevilla reported that all others 'LGTM' too. Appreciate it!

Copy link
Contributor

@ryzizub ryzizub left a comment

Choose a reason for hiding this comment

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

Can we also use that hook assets in dart? I believe it would be awesome for example, since this way its now it can be easily left out

@realmeylisdev realmeylisdev requested a review from ryzizub January 12, 2026 17:19
@realmeylisdev
Copy link
Contributor Author

@marcossevilla please review!

@ryzizub
Copy link
Contributor

ryzizub commented Jan 13, 2026

@meylis1998 I was probably unclear on my comment.. sorry 💙, what i meant with Can we also use that hook assets in dart is using that hook add method inside the dart (for example like https://dart.dev/tools/hooks#reference-assets) . Cause right now is the hook method unused . Hope that makes sense!

@realmeylisdev
Copy link
Contributor Author

@meylis1998 I was probably unclear on my comment.. sorry 💙, what i meant with Can we also use that hook assets in dart is using that hook add method inside the dart (for example like https://dart.dev/tools/hooks#reference-assets) . Cause right now is the hook method unused . Hope that makes sense!

Done! Added @Native bindings for the hook assets in 4afc8da.

@realmeylisdev realmeylisdev force-pushed the feat/add-build-hooks-support branch from 4afc8da to e49ff06 Compare January 13, 2026 21:40
@realmeylisdev realmeylisdev force-pushed the feat/add-build-hooks-support branch from e49ff06 to 8379f51 Compare January 13, 2026 21:43
@realmeylisdev
Copy link
Contributor Author

Finally was able to get the CI checks right)

We can review it now.

@ryzizub
Copy link
Contributor

ryzizub commented Jan 15, 2026

@meylis1998 Great work on native bindings!! I would include add method to platform interface and use that binding on each platform side. That way on dart side we wont care about each platform but we will use the plugin itself.

So instead of
https://github.com/VeryGoodOpenSource/very_good_templates/pull/411/files#diff-fb58dfe143fc9c78540dcceb2e7800f9dbcfa1499e40e36d119365e2eaa84c83R31

We will use something like _platform. add in my_plugin :) Hope that makes sense

@realmeylisdev realmeylisdev requested a review from ryzizub January 15, 2026 09:02
@realmeylisdev
Copy link
Contributor Author

realmeylisdev commented Jan 15, 2026

New Usage Pattern

// Before (platform-specific import)
import 'package:my_plugin_android/my_plugin_android.dart';
final result = add(24, 18);

// After (main plugin import)
import 'package:my_plugin/my_plugin.dart';
final result = add(24, 18);

@realmeylisdev realmeylisdev force-pushed the feat/add-build-hooks-support branch from c18e689 to c668598 Compare January 15, 2026 09:55
@realmeylisdev
Copy link
Contributor Author

Can't wait to see them merge.

@ryzizub ryzizub assigned ryzizub and unassigned realmeylisdev Feb 4, 2026
Copy link
Contributor

@ryzizub ryzizub left a comment

Choose a reason for hiding this comment

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

Im getting also on generated plugin some format issues mainly Missing a newline at the end of the file.

I would recommend generating this template via mason and verify the code

// Configure the C library builder
final cbuilder = CBuilder.library(
name: packageName,
assetName: '$packageName.dart',
Copy link
Contributor

Choose a reason for hiding this comment

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

I was testing this on package and i found issues one of them is that assetName should match the library URI where @Native is declared.
Can you verify that its properly running when the plugin is connected and add method is used?

@marcossevilla marcossevilla moved this from Needs Triage to Community in VGV Open Source 🦄 🧙🌟 Feb 4, 2026
@ryzizub
Copy link
Contributor

ryzizub commented Mar 2, 2026

@meylis1998 let me know if you need assistance to resolve issues

@marcossevilla
Copy link
Member

@meylis1998 were you able to take a look at this?

@marcossevilla
Copy link
Member

@ryzizub @realmeylisdev let me know if we can close this one for now or if we want to fix the issues

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

Labels

None yet

Projects

Status: Community

Development

Successfully merging this pull request may close these issues.

feat: Add build hooks to plugin template

4 participants