33[ ![ pub package] ( https://img.shields.io/pub/v/code_assets.svg )] ( https://pub.dev/packages/code_assets )
44[ ![ package publisher] ( https://img.shields.io/pub/publisher/code_assets.svg )] ( https://pub.dev/packages/code_assets/publisher )
55
6- A library to use in build hooks ( ` hook/build.dart ` ) for building and bundling
7- code assets .
6+ This package provides the API for code assets to be used with
7+ [ ` package:hooks ` ] ( https://pub.dev/packages/hooks ) .
88
99A code asset is an asset containing executable code which respects the native
10- application binary interface (ABI).
10+ application binary interface (ABI). These assets are bundled with a Dart or
11+ Flutter application. They can be produced by compiling C, C++, Objective-C,
12+ Rust, or Go code for example.
13+
14+ This package is used in a build hook (` hook/build.dart ` ) to inform the Dart
15+ and Flutter SDKs about the code assets that need to be bundled with an
16+ application.
1117
1218Code assets can be added in a build hook as follows:
1319
@@ -33,11 +39,74 @@ void main(List<String> args) async {
3339}
3440```
3541
36- For more documentation of hooks, refer to the API docs of
37- [ ` package:hooks ` ] ( https://pub.dev/packages/hooks ) .
38-
3942When compiling C, C++ or Objective-C code from source, consider using
40- [ ` package:native_toolchain_c ` ] ( https://pub.dev/packages/native_toolchain_c ) .
43+ [ ` package:native_toolchain_c ` ] ( https://pub.dev/packages/native_toolchain_c ) :
44+
45+ <!-- file://./example/sqlite/hook/build.dart -->
46+ ``` dart
47+ import 'package:code_assets/code_assets.dart';
48+ import 'package:hooks/hooks.dart';
49+ import 'package:native_toolchain_c/native_toolchain_c.dart';
50+
51+ void main(List<String> args) async {
52+ await build(args, (input, output) async {
53+ if (input.config.buildCodeAssets) {
54+ final builder = CBuilder.library(
55+ name: 'sqlite3',
56+ assetName: 'src/third_party/sqlite3.g.dart',
57+ sources: ['third_party/sqlite/sqlite3.c'],
58+ defines: {
59+ if (input.config.code.targetOS == OS.windows)
60+ // Ensure symbols are exported in dll.
61+ 'SQLITE_API': '__declspec(dllexport)',
62+ },
63+ );
64+ await builder.run(input: input, output: output);
65+ }
66+ });
67+ }
68+ ```
69+
70+ See the full example in [ example/sqlite/] ( example/sqlite/ ) .
71+
72+ When interfacing with system libraryies, the API in this package is enough:
73+
74+ <!-- file://./example/host_name/hook/build.dart -->
75+ ``` dart
76+ import 'package:code_assets/code_assets.dart';
77+ import 'package:hooks/hooks.dart';
78+
79+ void main(List<String> args) async {
80+ await build(args, (input, output) async {
81+ if (input.config.buildCodeAssets) {
82+ switch (input.config.code.targetOS) {
83+ case OS.android || OS.iOS || OS.linux || OS.macOS:
84+ output.assets.code.add(
85+ CodeAsset(
86+ package: 'host_name',
87+ name: 'src/third_party/unix.dart',
88+ linkMode: LookupInProcess(),
89+ ),
90+ );
91+ case OS.windows:
92+ output.assets.code.add(
93+ CodeAsset(
94+ package: 'host_name',
95+ name: 'src/third_party/windows.dart',
96+ linkMode: DynamicLoadingSystem(Uri.file('ws2_32.dll')),
97+ ),
98+ );
99+ case final os:
100+ throw UnsupportedError('Unsupported OS: ${os.name}.');
101+ }
102+ }
103+ });
104+ }
105+ ```
106+
107+ See the full example in [ example/host_name/] ( example/host_name/ ) .
108+
109+ For more information see [ dart.dev/tools/hooks] ( https://dart.dev/tools/hooks ) .
41110
42111## Status: In Preview
43112
0 commit comments