Avalonia在macOS上创建.app包,核心是构建符合Apple规范的Bundle结构,并正确配置Info.plist、图标、可执行权限等要素。不需要Xcode IDE,纯命令行即可完成,但必须在macOS系统(或WSL2+macOS交叉环境)中生成最终.app才能确保签名和运行正常。
准备发布输出(dotnet publish)
先在项目根目录执行跨平台发布,指定macOS运行时标识符:
若目标为Intel芯片:`dotnet publish -r osx-x64 -c Release --self-contained true` 若目标为Apple Silicon(M1/M2/M3):`dotnet publish -r osx-arm64 -c Release --self-contained true` 输出路径通常为:bin/Release/net8.0/osx-x64/publish/(含MyApp.dll、Avalonia.dll等)
构建标准.app文件夹结构
手动创建或用脚本组织以下层级(以
MyApp.app为例):
MyApp.app/Contents/Info.plist—— 必须存在,且字段完整
MyApp.app/Contents/MacOS/MyApp—— 可执行入口(无.dll后缀),需
chmod +x
MyApp.app/Contents/Resources/MyApp.icns—— 图标文件(需提前转成icns格式)
注意:
MyApp(可执行名)必须与
CFBundleExecutable值完全一致,也应与
.dll主程序集名同名(不含.dll)。
关键Info.plist配置项
使用文本编辑器或Xcode打开
Info.plist,确保包含以下键值(部分为必需,部分提升兼容性): CFBundleExecutable:如
MyApp(对应
Contents/MacOS/下的二进制名) CFBundleName 和 CFBundleDisplayName:显示名称,后者建议≤15字符 CFBundleIdentifier:反向DNS格式唯一ID,如
com.mycompany.myappCFBundleVersion 与 CFBundleShortVersionString:分别填内部版本号(如
1.0.0)和用户版号(如
1.0) CFBundleIconFile:设为
MyApp.icns(含扩展名) NSHighResolutionCapable:设为
<true></true>,启用Retina支持 LSApplicationCategoryType(可选):如
public.app-category.utilities,影响Mac App Store分类
签名与公证(上线必备)
未签名的.app在macOS Catalina及以后默认被阻止运行:
用Apple Developer证书签名:codesign --force --deep --sign "Developer ID Application: Your Name" MyApp.app验证签名:
codesign --verify --verbose MyApp.app上传公证:
xcrun notarytool submit MyApp.app --keychain-profile "AC_PASSWORD" --wait公证通过后 stapler 打包:
xcrun stapler staple MyApp.app
若仅本地测试,可临时关闭Gatekeeper:
sudo spctl --master-disable(不推荐长期使用)。
整个过程不依赖Xcode图形界面,但Xcode命令行工具(
xcode-select --install)和Apple开发者账号是签名环节的硬性前提。打包完成后,双击.app即可运行,效果与原生macOS应用一致。
