问题描述
在打包Xcode应用时,发现Xcode报如下错误:
"pngquant" must be rebuilt with support for the Hardened Runtime. Enable the Hardened Runtime capability in the project editor, test your app, rebuild your archive, and upload again.
这个报错信息来自macOS的代码签名与App安全机制,意思是:App 中的 pngquant 可执行文件没有启用 Hardened Runtime(加固运行时)支持,现在尝试将它包含在一个必须启用该功能的 App 中(比如上传到 Mac App Store 或者使用 Notarization 时)。
问题的来源在于,项目中使用了 pngquant 这个工具(可能是命令行可执行文件或库),它被打包进了 App。但这个文件没有使用开启 Hardened Runtime 的方式构建(rebuild),因此在打包、签名、notarization 或上传到 App Store 的过程中被拒绝。
什么是 Hardened Runtime?
Hardened Runtime 是 Apple 在 macOS 10.14 引入的安全机制,开启后系统会对 App 的运行行为进行更严格的限制和验证(比如防止代码注入、禁止调试等)。
上传到 App Store 或进行 App Notarization 的 App 必须开启 Hardened Runtime,App 中的所有可执行文件也必须支持该模式。
问题原因
把从 pngquant 官网或者某些第三方下载的 pngquant 执行文件打包进 App 中,但它:
1、没有在编译时启用 Hardened Runtime;
2、即使签名了,也没有附带 –options runtime;
3、所以在构建、签名或上传时就会报错:
"pngquant" must be rebuilt with support for the Hardened Runtime.
解决思路
一、重新编译并启用 Hardened Runtime 支持
1、克隆源代码:
git clone https://github.com/kornelski/pngquant.git
cd pngquant
修改 Xcode 工程或手动编译时:
使用 clang 编译时加上:
-fstack-protector-strong -Wno-unguarded-availability -Wno-deprecated-declarations
并在代码签名时加入 Hardened Runtime 选项:
codesign --options runtime --sign "Developer ID Application: YourName" ./pngquant
二、使用 Xcode 签名并启用 Hardened Runtime
1、在项目中找到 pngquant 可执行文件。
2、在 Xcode > TARGETS > Signing & Capabilities 中:
添加 Hardened Runtime 功能。
3、确保 pngquant 也通过签名,命令行示例:
codesign --force --options runtime --sign "Developer ID Application: YourName" path/to/pngquant
检查pngquant
使用 codesign 检查pngquant:
codesign -dvvv pngquant
输出信息:
Executable=/Users/ImageSlim/ImageSlim/Packages/pngquant
Identifier=pngquant
Format=Mach-O universal (x86_64 arm64)
CodeDirectory v=20400 size=4524 flags=0x0(none) hashes=136+2 location=embedded
Hash type=sha256 size=32
CMSDigestType=2
Signature size=8979
Authority=Developer ID Application: Kornel Lesinski (59KZTZA4XR)
Authority=Developer ID Certification Authority
Authority=Apple Root CA
Timestamp=Dec 19, 2023 at 04:35:28
Info.plist=not bound
TeamIdentifier=59KZTZA4XR
Sealed Resources=none
Internal requirements count=1 size=168
...
已签名:
Authority=Developer ID Application: Kornel Lesinski (59KZTZA4XR)
说明这是官方作者发布的 pngquant 可执行文件,确实是签名过的。
但是未启用 Hardened Runtime:
flags=0x0(none)
这一行的 flags=0x0(none) 说明此二进制文件未启用 Hardened Runtime,而 Apple 要求应用中的所有可执行内容都需要启用这个功能(特别是如果启用了 Notarization 或者上传 App Store 时)。
解决方案
重新签名这个可执行文件,并加上 Hardened Runtime 支持。
codesign --force --options runtime --sign "Developer ID Application: junyu fang (你的团队ID)" /Users/ImageSlim/ImageSlim/Packages/pngquant
签名完成后,会输出:
/Users/ImageSlim/ImageSlim/Packages/pngquant: replacing existing signature
注意事项:
1、–options runtime 表示启用 Hardened Runtime。
2、–sign 后面使用自己的Developer ID Application证书(不是原作者的)。
如果不清楚有没有运行Developer ID Application证书,可以使用下面的命令查看当前所有可用的签名身份:
security find-identity -v -p codesigning
如果没有Developer ID Application证书,签名时会提示no identity found。
请下滑到本文的“扩展知识”部分,申请Developer ID Application证书。
3、使用codesign命令时,个人姓名和团队ID要一致:
// 查看全部的开发证书
security find-identity -v -p codesigning
6780DEE7... "Developer ID Application: junyu fang (RRMSD743UQ)" // 个人姓名和团队ID
// codesign 签名命令
codesign --force --options runtime --sign "Developer ID Application: junyu fang (你的团队ID)" /Users/ImageSlim/ImageSlim/Packages/pngquant // 个人姓名和团队ID,要一致
如果不一致,也会提示no identity found。
验证签名状态
运行:
codesign -dvvv /Users/ImageSlim/ImageSlim/Packages/pngquant
可以在输出中看到:
flags=0x10000(runtime)

这就代表 Hardened Runtime 生效了。
总结
App中的可执行文件,如果没有启用Hardened Runtime,就无法上传到Mac App Store 或者使用 Notarization。
解决方案为,申请一张Developer ID Application证书,使用codesign命令给可执行文件重新签名并开启Hardened Runtime 支持,签名后问题得到解决。
扩展知识
如果在shiycodesign签名时,发现报错:
codesign --force --options runtime --sign "Developer ID Application: Fang Junyu (RRMSD743UQ)" ...
报错信息:
no identity found
说明macOS没有找到开发者证书,可能原因:
1、没有安装 Developer ID Application 证书(专用于发布到非 App Store 的 macOS 应用)。
2、或者实际使用的是 Mac Development / Mac App Distribution 证书,而不是 Developer ID。
3、证书安装在了其他用户、其他钥匙串,或者当前没有权限访问。
解决方案:
运行以下命令查看当前所有可用的签名身份:
security find-identity -v -p codesigning
输出类似这样:
1) XXXXXXXX "Apple Development: Fang Junyu (RRMSD743UQ)"
2) YYYYYYYY "Developer ID Application: Fang Junyu (RRMSD743UQ)"
如果没有看到 Developer ID Application: Fang Junyu (…),那当前并没有安装这张证书。
我本地的输出为:
security find-identity -v -p codesigning
1) 4E64C4... "Apple Development: junyu fang (DGB6H35P5R)"
2) A75C2C... "iPhone Distribution: junyu fang (RRMSD743UQ)"
3) CCD372... "Apple Distribution: junyu fang (RRMSD743UQ)"
没有Developer ID Application证书,因此需要手动申请。
建议使用Xcode申请证书,打开Xcode > Settings > Accounts,点击Manage Certificates,点击“+” > Developer ID Application,自动安装证书。详细的申请证书流程,请见《Apple Developer证书》。

申请完成后,重新运行查看签名命令:
security find-identity -v -p codesigning
查看是否有输出Developer ID Application的证书信息:
6780DEE7... "Developer ID Application: junyu fang (RRMSD743UQ)"
相关文章
1、macOS安全机制Hardened Runtime(强化运行时):https://fangjunyu.com/2025/06/22/macos%e5%ae%89%e5%85%a8%e6%9c%ba%e5%88%b6hardened-runtime%ef%bc%88%e5%8a%a0%e5%9b%ba%e8%bf%90%e8%a1%8c%e6%97%b6%ef%bc%89/
2、Apple数字签名codesign:https://fangjunyu.com/2025/07/04/apple%e6%95%b0%e5%ad%97%e7%ad%be%e5%90%8dcodesign/
3、Apple Developer证书:https://fangjunyu.com/2025/07/06/apple-developer%e8%af%81%e4%b9%a6/