如何在使用Dart Hooks的 Flutter应用中添加闭源动态库?
我的环境信息:
=== Dart信息 ===
- Dart 3.11.1(稳定版)(2026年2月24日,星期二00:03:07 -0800)在 "macos_arm64" 上
- 在macos / 版本26.3.1 (a) (Build 25D771280a)
- 区域设置为en-US
项目信息
- SDK约束:'^3.11.1'
- 依赖项:code_assets, dart_ipc, data_assets, ffi, flutter, hooks, logging, native_toolchain_c, path
- 开发依赖:ffigen, flutter_lints, test
=============
我的问题:
我已经用ffigen将我的Flutter应用与Holepunch的 bare-kit集成,可以无问题地执行一个简单的JavaScript。
然而,执行一个调用Holepunch的 'bare-tcp' npm模块的JavaScript(ipc_test.bundle)时失败了(见下方错误日志)。
bare-tcp依赖动态库,例如libbare-dns.2.1.4.dylib。
在我的hook/build.dart脚本中,我把每个所需的动态库作为CodeAsset添加,例如:
output.assets.code.add(CodeAsset(
package: input.packageName,
name: "libbare-dns.2.1.4.so",
linkMode: DynamicLoadingBundled(),
file: input.packageRoot.resolve('lib/src/bare/libs/android/arm64/arm64-v8a/libbare-dns.2.1.4.so'),
)
)
但运行脚本时出现运行时错误;似乎无法加载动态库。
请参见下方的错误日志,以及Android Studio显示的apk内容截图。
有人能解释我到底哪里做错了吗?
提前致谢
错误日志:
E/xample.bare_kit( 3780): Uncaught (in promise) AddonError: ADDON_NOT_FOUND: Cannot find addon '.' imported from 'file:///app.bundle/node_modules/bare-dns/binding.js'
E/xample.bare_kit( 3780): Candidates:
E/xample.bare_kit( 3780): - linked:libbare-dns.2.1.4.dylib
E/xample.bare_kit( 3780): at Addon.resolve (bare:/bare.js:3535:22)
E/xample.bare_kit( 3780): at require.addon (bare:/worklet.bundle/node_modules/bare-module/index.js:776:33)
E/xample.bare_kit( 3780): at file:///app.bundle/node_modules/bare-dns/binding.js:1:26
E/xample.bare_kit( 3780): at Module.\_evaluate (bare:/worklet.bundle/node_modules/bare-module/index.js:210:7)
E/xample.bare_kit( 3780): at Module.\_transform (bare:/worklet.bundle/node_modules/bare-module/index.js:122:12)
E/xample.bare_kit( 3780): at Module.load (bare:/worklet.bundle/node_modules/bare-module/index.js:467:21)
E/xample.bare_kit( 3780): at require (bare:/worklet.bundle/node_modules/bare-module/index.js:763:25)
E/xample.bare_kit( 3780): at file:///app.bundle/node_modules/bare-dns/index.js:1:17
E/xample.bare_kit( 3780): at Module.\_evaluate (bare:/worklet.bundle/node_modules/bare-module/index.js:210:7)
E/xample.bare_kit( 3780): at Module.\_transform (bare:/worklet.bundle/node_modules/bare-module/index.js:122:12) {
E/xample.bare_kit( 3780): \[cause\]: Error: dlopen failed: library "libbare-dns.2.1.4.dylib
解决方案
我已经解决了问题。解决办法来自Holepunch这边:JavaScript必须使用Holepunch的 bare-pack工具打包。我使用了一个较旧的版本;用最新版bare-pack重新打包JavaScript,并提供正确的主机参数,问题就解决了。
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。