调试正式版本
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
符号化堆栈跟踪
当 React Native 应用在正式版本中抛出未处理异常时,输出结果可能经过混淆处理而难以阅读。
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。这个过程称为符号化(symbolication)。
您可以通过将堆栈跟踪和生成的源映射传递给 metro-symbolicate,对上述压缩后的函数名和字节码进行符号化处理。
启用源映射
符号化堆栈跟踪需要源映射支持。请确保在目标平台的构建配置中已启用源映射生成。
- Android
- iOS
On Android, source maps are enabled by default.
To enable source map generation, ensure the following hermesFlags are present in android/app/build.gradle.
react {
hermesFlags = ["-O", "-output-source-map"]
}
If done correctly you should see the output location of the source map during Metro build output.
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
On iOS, source maps are disabled by default. Use the following instructions to enable them.
To enable source map generation:
- Open Xcode and edit the build phase "Bundle React Native code and images".
- Above the other exports, add a
SOURCEMAP_FILEentry with the desired output path.
+ export SOURCEMAP_FILE="$(pwd)/../main.jsbundle.map"
WITH_ENVIRONMENT="../node_modules/react-native/scripts/xcode/with-environment.sh"
If done correctly you should see the output location of the source map during Metro build output.
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
生成源映射后,我们现在可以转换堆栈跟踪信息了。
# 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立即成功退出,请检查输入是否来自管道/重定向而非终端直接输入