On Android catching Java exceptions is easy via UncaughtExceptionHandler. Catching NDK crashes is a bit more convoluted. Since the native stack is probably corrupted, you want the crash handler to run on a separate process. Also, since the system might be in an unstable shape, don’t send the crash report to your web server or do anything fancy. Just write the crash report to a file, and on the next restart of the app, send to your web server and delete it from the disk. I ended up using jndcrash package for this.

  1. Create a new Service that extends ru.ivanarh.jndcrash.NDCrashService

  2. Run this as a sticky service in a separate process by adding the following to the AndroidManifest.xml

    XML
    1
    2
    3
    
    <!-- Create a new process to handle native crashes
             https://github.com/ivanarh/jndcrash#out-of-process -->
        <service android:name=".NdkCrashService" android:process=":ndkCrashReportProcess"/>
  3. Override onCrash to read the logcat logs and write a report to a new location. I didn’t care about it overwriting an existing native crash report, but if your app has a million+ installs, you should generate filename patterns for crash reports.

    Java
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    
    @Override
      public void onCrash(String reportPath) {
        String logcatLogs = getLogcatLogs(NUM_LOGCAT_LINES);
        String ndkLogcatLogsReportPath = getNdkCrashLogcatLogsPath(this);
        Log.i(TAG, "onCrash, stack trace in " + reportPath);
        Log.i(TAG, "onCrash, logcat logs are in " + ndkLogcatLogsReportPath);
        Log.d(
          TAG,
          "Logcat logs for the native error (from last " +
          NUM_LOGCAT_LINES +
          " lines): \"" +
          logcatLogs +
          "\""
        );
        try (FileWriter fileWriter = new FileWriter(ndkLogcatLogsReportPath, false/* append */)) {
          for (String line : logcatLogs.split("\n")) {
            // Build fingerprint marks the beginning of native crash dump which is already
            // present in the reportPath file.
            if (line.contains("Build fingerprint")) {
              break;
            }
            fileWriter.write(line);
            fileWriter.write("\n");
          }
          fileWriter.flush();
        } catch (IOException e) {
          Log.e(TAG, "Error writing more logs to native crash report " + reportPath);
        }
      }
  4. In the app’s onCreate, initialize the crash reporter.

    Java
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    
    private void initNdkCrashHandler() {
        final String reportPath = NdkCrashService.getNdkCrashLogReportPath(this);
        final NDCrashError error = NDCrash.initializeOutOfProcess(
          this,
          reportPath,
          NDCrashUnwinder.libunwind,
          NdkCrashService.class
        );
        if (error == NDCrashError.ok) {
          Log.i("MainApplication@initJndcrash", "NDK crash handler init successful");
        } else {
          Log.e("MainApplication@initJndcrash", "NDK crash handler init failed: " + error);
        }
      }
  5. It is probably best to read and submit any existing reports in the same initNdkCrashHandler method. Since we were using React Native, we ended up doing it a bit differently. We used Sentry to wrap a native crash and report it as a Java Exception; you can do this for your crash reporting mechanism as well.

    Js
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    const uploadNdkCrashesIfAny = async () => {
      // This file path should be same here and in MainApplication.java
      const ndkCrashLogsFilePath = RNFS.CachesDirectoryPath + '/ndk_crash_logs.txt'
      const ndkCrashLogcatLogsFilePath = RNFS.CachesDirectoryPath + '/ndk_crash_logcat_logs.txt'
    
      if (!(await RNFS.exists(ndkCrashLogsFilePath))) {
        Logger.debug(
          'Sentry@uploadNdkCrashesIfAny',
          `crash log file ${ndkCrashLogsFilePath} not found, no native crashes recorded`
        )
        return
      }
    
      const fileSize = parseInt((await RNFS.stat(ndkCrashLogsFilePath)).size, 10)
      Logger.info(
        'Sentry@uploadNdkCrashesIfAny',
        `crash log file ${ndkCrashLogsFilePath} found (${fileSize} bytes), capturing it via Sentry`
      )
      const msg1 = (await RNFS.exists(ndkCrashLogcatLogsFilePath))
        ? await RNFS.readFile(ndkCrashLogcatLogsFilePath)
        : 'Logcat logs not available'
      const msg2 = await RNFS.readFile(ndkCrashLogsFilePath)
    
      Sentry.captureMessage(`NDK crash\n${msg1}\n${msg2}`)
      await RNFS.unlink(ndkCrashLogsFilePath)
    
      if (!(await RNFS.exists(ndkCrashLogcatLogsFilePath))) {
        await RNFS.unlink(ndkCrashLogcatLogsFilePath)
      }
    }

    And a simple mechanism to handle native crashes would be ready. The best part is that this approach is not tied to your crash reporter, so, you can choose Sentry or Firebase Crashlytics or any other mechanism.