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.
Restore after Android process death
Section titled “Restore after Android process death”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:
- Start the runtime in
Application.onCreate. Android re-creates aSurfaceFragmentinsideActivity.onCreate, before your screen code runs. The surface mounts once the runtime is started, sostartmust not depend on a screen. - Give every embedded
SurfaceHostViewa 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. ASurfaceFragmentneeds nothing: theFragmentManagerrestores it. - Attach your listener and fallback again. The
listenerandfallbackViewFactoryproperties 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 state restoration
Section titled “iOS state restoration”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.
// Savelet data: Data = screen.descriptor.encoded()
// Restorelet descriptor = try SurfaceDescriptor.decode(data)let screen = SurfaceViewController(descriptor: descriptor)// or, for an embedded host: SurfaceHostView(descriptor: descriptor)What Debug builds get
Section titled “What Debug builds get”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.
Develop with the React project live
Section titled “Develop with the React project live”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.
Set your app as the host, once
Section titled “Set your app as the host, once”In the React project’s package.json, the brownfield key points to your native project:
"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.
Run one command
Section titled “Run one command”From the React project, with a simulator booted or an emulator running:
# builds your app with the dev variant, installs it on the booted simulator,# starts the dev server and launches the appbun run nuib dev ios# the same on the running emulatorbun run nuib dev androidYour 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.
Other setups
Section titled “Other setups”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.
# an unpublished release build of the React projectbun run nuib link ../native-app# the dev variant, for live loadingbun run nuib link --dev ../native-app# undobun run nuib unlink ../native-app- Build and run your app as usual. On iOS build through the project’s workspace:
xcodebuild -projectignores the override. On Android use./gradlew :app:installDebugor Android Studio. - With the dev variant, start the server with
nuib devin the React project. Fast Refresh keeps the React state, andrin the server’s terminal reloads. - Your app must already depend on the artifact. Otherwise
nuib linkrefuses. - On an Android emulator, a server on your computer is reached through
adb reverse tcp:<port> tcp:<port>.nuib dev androidruns it for you.
A server on another port or machine
Section titled “A server on another port or machine”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:
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>Android, a debuggable app:
adb shell run-as <application id> \ sh -c "'mkdir -p files && echo http://192.168.1.20:8090 > files/dev-server'"# then start the app againadb shell am force-stop <application 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
offmeans 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.
Symbolicate a crash
Section titled “Symbolicate a crash”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.
iOS native frames
Section titled “iOS native frames”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.
dwarfdump --uuid <version>/symbols/ios/ios-arm64/ShopReact.framework.dSYMatos -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.
Android native frames
Section titled “Android native frames”Compare the build id of the unstripped library with the BuildId in the tombstone. Then symbolicate the whole tombstone, or one address.
llvm-readelf -n <version>/symbols/android/arm64-v8a/<library>.so | grep "Build ID"ndk-stack -sym <version>/symbols/android/arm64-v8a -i tombstone.txtllvm-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>.
JavaScript frames
Section titled “JavaScript frames”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.