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

Flutter构建的macOS应用在M2 Mac启动崩溃求解决

Flutter macOS应用在M2 Mac启动崩溃问题解决

我用Flutter执行flutter build macos命令打包了一款macOS应用,在自己的M1 Mac上运行正常,但发给朋友的M2 Mac后,启动就崩溃。以下是崩溃日志:

Version:               1.0.0 (1)
Code Type:             ARM-64 (Native)
Parent Process:        launchd [1]
User ID:               501

Date/Time:             2022-11-02 15:44:34.0519 +0700
OS Version:            macOS 12.6 (21G115)
Report Version:        12
Anonymous UUID:        610CC70D-80DB-362E-6168-CB7FFD990E40

Sleep/Wake UUID:       686BA11E-F563-4C8F-AA19-C4937F9181DC

Time Awake Since Boot: 9500 seconds
Time Since Wake:       5014 seconds

System Integrity Protection: enabled

Crashed Thread:        0

Exception Type:        EXC_CRASH (SIGABRT)
Exception Codes:       0x0000000000000000, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Termination Reason:    Namespace DYLD, Code 1 Library missing
Library not loaded: '/Users/*/Library/Developer/Xcode/DerivedData/Runner-eixmpjopssepzedozqbaoavjkhar/Build/Intermediates.noindex/ArchiveIntermediates/Runner/IntermediateBuildFilesPath/native.build/cargo_target/aarch64-apple-darwin/release/deps/libnative.dylib'
Referenced from: '/private/var/folders/*/My App App.app/Contents/MacOS/My App'
Reason: tried: '/Users/*/Library/Developer/Xcode/DerivedData/Runner-eixmpjopssepzedozqbaoavjkhar/Build/Intermediates.noindex/ArchiveIntermediates/Runner/IntermediateBuildFilesPath/native.build/cargo_target/aarch64-apple-darwin/release/deps/libnative.dylib' (no such file), '' (no such file), '' (no such file)
(terminated at launch; ignore backtrace)
Thread 0 Crashed:
0   dyld                                   0x105103e98 __abort_with_payload + 8
1   dyld                                   0x10510b024 abort_with_payload_wrapper_internal + 104
2   dyld                                   0x10510b058 abort_with_payload + 16
3   dyld                                   0x1050c2a28 dyld4::halt(char const*) + 580
4   dyld                                   0x1050bdfb8 dyld4::prepare(dyld4::APIs&, dyld3::MachOAnalyzer const*) + 3644
5   dyld                                   0x1050bd06c start + 488


Thread 0 crashed with ARM Thread State (64-bit):
    x0: 0x0000000000000006   x1: 0x0000000000000001   x2: 0x000000016b11e928   x3: 0x00000000000001ab
    x4: 0x000000016b11e528   x5: 0x0000000000000000   x6: 0x0000000000000000   x7: 0x0000000000000000
    x8: 0x0000000000000020   x9: 0x0000000000000009  x10: 0x000000016b11e6fc  x11: 0x00000000000002d2
   x12: 0x0000000000000000  x13: 0x0000000000000036  x14: 0x00000002703cf8db  x15: 0x0000000000000000
   x16: 0x0000000000000209  x17: 0x00000001050bb14c  x18: 0x0000000000000000  x19: 0x0000000000000000
   x20: 0x000000016b11e528  x21: 0x00000000000001ab  x22: 0x000000016b11e928  x23: 0x0000000000000001
   x24: 0x0000000000000006  x25: 0x000000016b11e528  x26: 0x0000000000000400  x27: 0x00000000000003ca
   x28: 0x0000000000000316   fp: 0x000000016b11e4f0   lr: 0x000000010510b024
    sp: 0x000000016b11e4b0   pc: 0x0000000105103e98 cpsr: 0x00001000
   far: 0x0000000104df8000  esr: 0x56000080  Address size fault

Binary Images:
       0x1050b8000 -        0x105117fff dyld (*) <38ee9fe9-b66d-3066-8c5c-6ddf0d6944c6> /usr/lib/dyld

External Modification Summary:
  Calls made by other processes targeting this process:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0
  Calls made by this process:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0
  Calls made by all processes on this machine:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0

VM Region Summary:
ReadOnly portion of Libraries: Total=976K resident=0K(0%) swapped_out_or_unallocated=976K(100%)
Writable regions: Total=9216K written=0K(0%) resident=0K(0%) swapped_out=0K(0%) unallocated=9216K(100%)

                                VIRTUAL   REGION 
REGION TYPE                        SIZE    COUNT (non-coalesced) 
===========                     =======  ======= 
STACK GUARD                       56.0M        1 
Stack                             8176K        1 
VM_ALLOCATE                         16K        1 
__DATA                              96K        6 
__DATA_CONST                       176K        6 
__LINKEDIT                         480K        6 
__TEXT                             496K        6 
dyld private memory               1024K        1 
===========                     =======  ======= 
TOTAL                             66.2M       28 

问题原因

崩溃日志明确指向DYLD库缺失,应用试图加载的libnative.dylib路径是本地Xcode DerivedData的临时目录,该目录在朋友的M2 Mac上不存在,导致启动失败。从路径中的cargo_target可以看出,这个动态库是Rust编译生成的。

解决步骤

  • 确保动态库被正确打包进应用:打开Flutter项目的macos目录下的Xcode工程,找到libnative.dylib,进入Target的Build Phases -> Copy Files,确认该库已添加,目标位置设为Frameworks,并勾选Copy only when installing。
  • 修改动态库的加载路径:使用install_name_tool将库的安装路径改为应用内的相对路径,这样其他机器能正确找到:
    install_name_tool -id "@executable_path/../Frameworks/libnative.dylib" /path/to/your/libnative.dylib
    
    修改完成后重新打包应用。
  • 使用Release模式打包:执行flutter build macos --release命令打包,Debug模式会依赖本地开发环境的临时文件,不适合分发。
  • 检查Rust编译配置:编译Rust库时使用cargo build --release,并将编译产物复制到Flutter项目的macos目录下,避免依赖DerivedData中的临时文件。

内容的提问来源于stack exchange,提问作者Kingfisher Phuoc

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 05:20:31