Skip to content
Preview: packages are not published yet.

Restore, debug and crashes

How surfaces come back after Android kills your process, what a Debug build of your app contains, how to run your app against the React project while it changes, and how to read a crash.

This page assumes you already show a surface in your app. If not, do the Quickstart first. The examples use the artifact ShopReact and its surface PointsHistory.

Android can kill your process in the background and re-create your screens later. A surface is fully described by its descriptor: an id, the surface name and its props. The hosts save the descriptor for you, so the surface comes back with the same id and its latest props.

The React state inside the surface is not saved. It starts again from the props, like a native screen that only keeps its arguments.

The hosts do the saving. You need to get three things right:

  1. Start the runtime in Application.onCreate. Android re-creates a SurfaceFragment inside Activity.onCreate, before your screen code runs. The surface mounts once the runtime is started, so start must not depend on a screen.
  2. Give every embedded SurfaceHostView a view id. It saves its descriptor in its view state, and Android only saves the state of views with an id. Your screen must also add the view again with the same id when it is re-created, as it does for any view it builds in code. The saved descriptor then replaces the one you passed in, so the surface keeps its id and its latest props. A SurfaceFragment needs nothing: the FragmentManager restores it.
  3. Attach your listener and fallback again. The listener and fallbackViewFactory properties are not saved.

For the listener, the simplest way is to let the host find it. A restored host with no listener set looks for one in this order:

Host Listener lookup, first match wins
SurfaceFragment its listener property, then the parent fragment if it implements SurfaceHostListener, then the Activity if it implements it, then BrownfieldRuntime.defaultSurfaceListener
SurfaceHostView its listener property, then the Activity if it implements SurfaceHostListener, then BrownfieldRuntime.defaultSurfaceListener

So if your Activity implements SurfaceHostListener, as in the retry example in Surfaces in your app, restored hosts report to it with no extra code. To observe every host in the app, set the default listener next to start:

class App : Application() {
override fun onCreate() {
super.onCreate()
ShopReact.start(this, BootPolicy.LAZY)
BrownfieldRuntime.defaultSurfaceListener = object : SurfaceHostListener {
override fun onSurfaceStateChanged(host: SurfaceHost, state: SurfaceState) {
if (state is SurfaceState.Failed) {
Log.w("app", "${host.descriptor.name}: ${state.failure.cause}")
}
}
}
}
}

The fallback has no lookup. Set fallbackViewFactory again when the fragment is attached again. A FragmentOnAttachListener does it for every SurfaceFragment your Activity holds, restored or new. Add it before super.onCreate, because that is where the FragmentManager re-attaches restored fragments:

class ShopActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
supportFragmentManager.addFragmentOnAttachListener { _, fragment ->
(fragment as? SurfaceFragment)?.fallbackViewFactory = { context, failure ->
MyErrorView(context, failure)
}
}
super.onCreate(savedInstanceState)
// build the screen as usual
}
}

The listener sees fragments added to the Activity’s own FragmentManager. For a SurfaceFragment inside another fragment, add it to that fragment’s childFragmentManager the same way, before its super.onCreate.

iOS does not restore hosts for you. If your app restores its screens, save the descriptor and open the host again from it. The same id and props come back.

// Save
let data: Data = screen.descriptor.encoded()
// Restore
let descriptor = try SurfaceDescriptor.decode(data)
let screen = SurfaceViewController(descriptor: descriptor)
// or, for an embedded host: SurfaceHostView(descriptor: descriptor)

The artifact you depend on is the release variant in every configuration of your app, Debug included. Your Debug build runs the same binary and the same embedded React content as your store build. It never looks for a development server and never contacts one.

This means a Debug build of your app is a faithful test of what ships, and it needs no tool from the React side to run.

What a Debug build does and does not add:

  • Logs: on Android, yes; on iOS, not by themselves. On Android a debuggable build of your app logs each boot stage, and a release build is silent until you run adb shell setprop log.tag.brownfield DEBUG. On iOS the release artifact is compiled once, in release, so it is silent in every configuration of your app until you launch with -BrownfieldLogging YES.
  • No development loading. Fast Refresh, reload and the developer menu are not in the release variant. They exist only in a separate dev variant, which is put in your app on your machine for a development session and is never published. The next section shows how.

When you work together with the React team, or you have the React project checked out, you can run your real app with the surfaces loaded from a development server. A saved change in the React code appears in your app without a rebuild.

In the React project’s package.json, the brownfield key points to your native project:

package.json
"brownfield": {
"dev": { "port": 8081 },
"hosts": {
"ios": "../native-ios-app",
"android": {
"path": "../native-android-app",
"module": "app"
}
}
}

If your Xcode project has several schemes, hosts.ios also needs the scheme to build.

From the React project, with a simulator booted or an emulator running:

Terminal window
# builds your app with the dev variant, installs it on the booted simulator,
# starts the dev server and launches the app
bun run nuib dev ios
Terminal window
# the same on the running emulator
bun run nuib dev android

Your app now shows the surfaces from the dev server. Save a file in the React project and the surface updates. If the server does not answer, the app shows its embedded content instead.

Press q or Ctrl-C to end the session. Your app points back at its published dependency, and your next build resolves it again.

nuib dev covers the usual loop. For the rest, use nuib link from the React project. It points your app at a local build of the artifact and leaves the building and running to you.

Terminal window
# an unpublished release build of the React project
bun run nuib link ../native-app
Terminal window
# the dev variant, for live loading
bun run nuib link --dev ../native-app
Terminal window
# undo
bun run nuib unlink ../native-app
  • Build and run your app as usual. On iOS build through the project’s workspace: xcodebuild -project ignores the override. On Android use ./gradlew :app:installDebug or Android Studio.
  • With the dev variant, start the server with nuib dev in the React project. Fast Refresh keeps the React state, and r in the server’s terminal reloads.
  • Your app must already depend on the artifact. Otherwise nuib link refuses.
  • On an Android emulator, a server on your computer is reached through adb reverse tcp:<port> tcp:<port>. nuib dev android runs it for you.

The dev variant loads from the server address recorded when it was built, http://localhost:<port>. To use another address for one installed app, write a pairing file with one line, the URL, and restart the app.

iOS simulator:

Terminal window
container="$(xcrun simctl get_app_container <udid> <bundle id> data)"
mkdir -p "$container/Library/Application Support/nitro-ui-brownfield"
echo http://192.168.1.20:8090 \
> "$container/Library/Application Support/nitro-ui-brownfield/dev-server"
xcrun simctl terminate <udid> <bundle id>
xcrun simctl launch <udid> <bundle id>
  • On iOS, the launch argument -NitroBrownfieldDevServer <url> wins over the pairing file, and the pairing file wins over the recorded address.
  • A pairing file that contains off means no server: the app starts from its embedded content.
  • Delete the file to go back to the recorded address.
  • The pairing file does not support a physical iOS device.

The binaries in the artifact are stripped, so a crash report shows addresses instead of names. Each published version carries its symbols next to the artifact, in a symbols directory. The version’s manifest.json ties each symbol file to its binary by UUID (iOS) or build id (Android).

Always use the symbols of the exact artifact version that crashed. Check the id first, then resolve the addresses.

Compare the UUID of the dSYM with the UUID that the crash report lists for the framework under “Binary Images”. Then resolve each address with the load address from the report.

Terminal window
dwarfdump --uuid <version>/symbols/ios/ios-arm64/ShopReact.framework.dSYM
Terminal window
atos -arch arm64 \
-o <version>/symbols/ios/ios-arm64/ShopReact.framework.dSYM \
-l <load address> <address> ...

Xcode and crash reporting services find the dSYM by its UUID once you add it to their symbol store.

Compare the build id of the unstripped library with the BuildId in the tombstone. Then symbolicate the whole tombstone, or one address.

Terminal window
llvm-readelf -n <version>/symbols/android/arm64-v8a/<library>.so | grep "Build ID"
Terminal window
ndk-stack -sym <version>/symbols/android/arm64-v8a -i tombstone.txt
Terminal window
llvm-addr2line -f -C -i -e <version>/symbols/android/arm64-v8a/<library>.so <pc>

<library> is the name of the library in the crashing frame of the tombstone. The three tools are part of the Android NDK, under $ANDROID_HOME/ndk/<version>.

The React project’s own code ships as compiled bytecode, so a JavaScript stack trace shows offsets, for example at Crasher (address at main.hbc:1:4711). The symbols directory has a source map, main.hbc.map, and the bundled main.js beside it. The map turns an offset (line 1, column = the offset) into a line and column of main.js. The //#region <file> comment above that line names the source file.

Send the trace and the artifact version to the React team: they own that code.