A release build is the one your users run, and it leaves out every tool from the last two lessons. When it crashes, the stack trace names minified functions and offsets instead of your files. Getting back to file, line and function names takes a source map from that exact build and one metro-symbolicate command.

Key terms

  • __DEV__: the pseudo-global variable you use to guard development-only blocks of code.
  • Symbolication: translating the minified function names and bytecode offsets in a stack trace into file, line and function names.
  • metro-symbolicate: the command that symbolicates a stack trace when you pass it the trace and a generated source map.
  • SOURCEMAP_FILE: the variable that makes the iOS bundling script emit a source map, and sets where it is written.

What a release build drops

The Dev Menu, LogBox and React Native DevTools are all disabled in release builds. React Native installs LogBox at startup only when __DEV__ is true, and with __DEV__ false every LogBox method, ignoreLogs included, is a stub that does nothing. Your ignoreLogs calls can stay in the code. On Android, release builds also compile Hermes without its debugger. On iOS, the bundling script makes a development bundle for the Debug configuration and a release bundle for any other configuration.

__DEV__ is inlined during compilation, and in the minified build it is stripped out together with the if blocks it guards. A block such as if (__DEV__) { console.log('cart', cart); } is development-only code of that kind.

From p@1:132161 to a line of code

An unhandled exception in a release build can produce an obfuscated stack trace with frames such as p@1:132161. Each one is a minified function name and a bytecode offset. Turning it into something like src/screens/Checkout.tsx:88:submitOrder is symbolication, and you do it by passing the trace and a generated source map to metro-symbolicate. Without a source map there is nothing to symbolicate with, so maps have to be enabled in the build config of the platform that crashed.

Android maps are on by default

Android generates source maps by default. They depend on the Hermes compiler flags in android/app/build.gradle: the template leaves hermesFlags commented out, with a note that its default is "-O", "-output-source-map". If you set hermesFlags yourself, keep -output-source-map in the list.

The build prints a Writing sourcemap output to: line pointing at index.android.bundle.packager.map under build/intermediates. The build can produce several maps, so give metro-symbolicate the one at android/app/build/generated/sourcemaps/react/release/index.android.bundle.map.

Turning on iOS maps

iOS source maps are off by default. Open Xcode, edit the "Bundle React Native code and images" build phase, and add a SOURCEMAP_FILE export with the output path you want. The template's script for that phase has no other exports, so the build phase script adds the line right above WITH_ENVIRONMENT.

The bundling script emits a map only when SOURCEMAP_FILE is set to a non-empty value. With Hermes, it has Metro write a map into the build products folder and the Hermes compiler write a second one, then composes both into the file at SOURCEMAP_FILE and deletes the two intermediate maps. The Writing sourcemap output to: line in a Release build log shows the Metro map under BuildProductsPath. Read that line as proof that maps are being generated, and pass metro-symbolicate the path you set in SOURCEMAP_FILE.

Running metro-symbolicate

The symbolicate commands sit in the second code example. npx metro-symbolicate with no arguments prints its usage instructions. To symbolicate a trace you saved, pass the map path and redirect the file into the command with < stacktrace.txt. On Android you can skip the file and pipe adb logcat -d straight into it.

Two mistakes give you output that is wrong or missing. The map has to come from the exact commit of the crashing app, because small source changes can shift offsets a lot, so keep the map of every build you ship. If metro-symbolicate exits immediately with success, it is reading from the terminal, so check that the trace comes in through a pipe or a redirection.

Code Examples

bash
set -e

export SOURCEMAP_FILE="$(pwd)/../main.jsbundle.map"
WITH_ENVIRONMENT="$REACT_NATIVE_PATH/scripts/xcode/with-environment.sh"
REACT_NATIVE_XCODE="$REACT_NATIVE_PATH/scripts/react-native-xcode.sh"

/bin/sh -c "\"$WITH_ENVIRONMENT\" \"$REACT_NATIVE_XCODE\""
bash
# Print usage instructions
npx metro-symbolicate

# iOS: the map at the SOURCEMAP_FILE path, and a saved trace
npx metro-symbolicate main.jsbundle.map < stacktrace.txt

# Android: a saved trace, or logcat piped in
npx metro-symbolicate android/app/build/generated/sourcemaps/react/release/index.android.bundle.map < stacktrace.txt
adb logcat -d | npx metro-symbolicate android/app/build/generated/sourcemaps/react/release/index.android.bundle.map
✓ Completed