You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NativeScript集成Flutter模块加载AssetManifest.bin失败求助

NativeScript集成Flutter模块加载图片资源解决方案

针对你遇到的Cannot load asset: "AssetManifest.bin"错误,核心原因是NativeScript构建时未将Flutter模块的assets资源(含自动生成的AssetManifest.bin)打包到Android项目中,以下是具体解决步骤:

1. 确认Flutter模块的资源结构

确保assets目录位于Flutter模块根目录下,而非NativeScript项目根目录,示例结构如下:

你的项目
├── nativescript-app/       # NativeScript主项目
└── flutter-module/         # Flutter模块
    ├── assets/
    │   ├── icons/
    │   │   └── woman.png
    └── pubspec.yaml

保持Flutter模块pubspec.yaml的现有配置,然后在Flutter模块目录下执行:

flutter pub get

该命令会生成Flutter资源清单文件AssetManifest.bin。

2. 配置NativeScript的Android构建脚本

打开NativeScript项目的App_Resources/Android/app.gradle,添加复制任务,将Flutter模块的assets资源同步到NativeScript安卓项目的assets目录:

// 复制Flutter模块assets到NativeScript安卓资源目录
task copyFlutterAssets(type: Copy) {
    // 路径需根据你的Flutter模块相对位置调整,示例为同级目录的Flutter模块
    from '../flutter-module/assets'
    into 'src/main/assets'
}
// 构建前先执行资源复制
preBuild.dependsOn copyFlutterAssets

3. 验证Flutter模块的集成配置

确保NativeScript已正确引入Flutter模块:

  • 打开App_Resources/Android/settings.gradle,确认包含Flutter模块:
    include ':flutter-module'
    project(':flutter-module').projectDir = new File('../flutter-module')
    
  • 打开App_Resources/Android/app.gradle,确认添加了Flutter模块依赖:
    implementation project(':flutter-module')
    

4. 清理构建缓存

执行以下命令清理NativeScript和Flutter的构建缓存,避免旧缓存干扰:

# 在NativeScript项目目录执行
ns clean

# 在Flutter模块目录执行
flutter clean

完成以上步骤后重新构建NativeScript项目,Flutter的Image.asset组件即可正常加载图片资源。

内容的提问来源于stack exchange,提问作者Yosec Cervino

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 05:08:24