调试发布版本
符号化堆栈跟踪
如果 React Native 应用在发布版本中抛出未处理的异常,输出内容可能会被混淆,难以阅读。
shell
07-15 10:58:25.820 18979 18998 E AndroidRuntime: FATAL EXCEPTION: mqt_native_modules
07-15 10:58:25.820 18979 18998 E AndroidRuntime: Process: com.awesomeproject, PID: 18979 07-15 10:58:25.820 18979 18998 E AndroidRuntime: com.facebook.react.common.JavascriptException: Failed, js engine: hermes, stack:
07-15 10:58:25.820 18979 18998 E AndroidRuntime: p@1:132161
07-15 10:58:25.820 18979 18998 E AndroidRuntime: p@1:132084
07-15 10:58:25.820 18979 18998 E AndroidRuntime: f@1:131854
07-15 10:58:25.820 18979 18998 E AndroidRuntime: anonymous@1:131119
在上面的堆栈跟踪中,`p@1:132161` 等条目是经过混淆的函数名和字节码偏移量。为了调试这些调用,我们需要将它们转换为文件、行号和函数名,例如 `AwesomeProject/App.js:54:initializeMap`。这被称为**符号化**。
您可以通过将堆栈跟踪和生成的源映射传递给 `metro-symbolicate` 来符号化上述混淆的函数名和字节码。
启用源映射
源映射是符号化堆栈跟踪所必需的。请确保目标平台的构建配置中已启用源映射。
- Android
- iOS
信息
在 Android 上,源映射默认是**启用**的。
要启用源映射生成,请确保 `android/app/build.gradle` 中存在以下 `hermesFlags`。
groovy
react {
hermesFlags = ["-O", "-output-source-map"]
}
如果操作正确,您应该在 Metro 构建输出中看到源映射的输出位置。
text
Writing bundle output to:, android/app/build/generated/assets/react/release/index.android.bundle
Writing sourcemap output to:, android/app/build/intermediates/sourcemaps/react/release/index.android.bundle.packager.map
信息
在 iOS 上,源映射默认是**禁用**的。请使用以下说明启用它们。
要启用源映射生成
- 打开 Xcode 并编辑构建阶段“Bundle React Native code and images”。
- 在其他导出之上,添加一个带有所需输出路径的 `SOURCEMAP_FILE` 条目。
diff
+ SOURCEMAP_FILE="$(pwd)/../main.jsbundle.map";
WITH_ENVIRONMENT="../node_modules/react-native/scripts/xcode/with-environment.sh"
如果操作正确,您应该在 Metro 构建输出中看到源映射的输出位置。
text
Writing bundle output to:, Build/Intermediates.noindex/ArchiveIntermediates/application/BuildProductsPath/Release-iphoneos/main.jsbundle
Writing sourcemap output to:, Build/Intermediates.noindex/ArchiveIntermediates/application/BuildProductsPath/Release-iphoneos/main.jsbundle.map
使用 `metro-symbolicate`
生成源映射后,我们现在可以转换我们的堆栈跟踪了。
shell
# Print usage instructions
npx metro-symbolicate
# From a file containing the stack trace
npx metro-symbolicate android/app/build/generated/sourcemaps/react/release/index.android.bundle.map < stacktrace.txt
# From adb logcat (Android)
adb logcat -d | npx metro-symbolicate android/app/build/generated/sourcemaps/react/release/index.android.bundle.map
源映射注意事项
- 构建过程可能会生成多个源映射。请确保使用示例中所示位置的源映射。
- 请确保您使用的源映射与崩溃应用的精确提交版本相对应。源代码的微小更改可能导致偏移量的巨大差异。
- 如果 `metro-symbolicate` 立即成功退出,请确保输入来自管道或重定向,而不是终端。