Android Native Development Kit (NDK)

Learn how to configure the NDK integration.

The Android Native Development Kit (NDK) allows you to implement parts of your app in native code, using languages such as C and C++.

NDK integration is packed with the SDK. The package sentry-android-ndk works by bundling Sentry's native SDK, sentry-native. As a result, even if a native library in your app causes the crash, Sentry is able to capture it.

You can disable the NDK integration, or use our Sentry Android SDK without the NDK.

To symbolicate the stack trace from native code, we need to have access to the debug symbols of your application.

Use the Sentry Android Gradle Plugin to upload the debug symbols and sources automatically.

Alternatively, in case you're not using Gradle, you can upload your .so files manually via sentry-cli. Please check the full documentation on uploading files to learn more about the upload of the debug symbols.

To use the Android NDK in your native code, include the sentry-native NDK libraries so the compiler can link them during the build. Use Android prefab to consume Sentry's prebuilt packages and link them in your CMakeLists.txt.

Enable prefab and add the sentry-native ndk dependency directly to your module:

app/build.gradle
Copied
android {
    buildFeatures {
        prefab = true
    }
}

dependencies {
    // The version MUST match with the version the Sentry Android SDK is using.
    // See https://github.com/getsentry/sentry-java/blob/8.53.0/gradle/libs.versions.toml
    implementation("io.sentry:sentry-native-ndk:<version>")
}

Link the pre-built packages with your native code

app/CMakeLists.txt
Copied
find_package(sentry-native-ndk REQUIRED CONFIG)

target_link_libraries(<app> PRIVATE
    sentry-native-ndk::sentry-android
    sentry-native-ndk::sentry
)

Now you can use the Sentry NDK API just by including the sentry.h in your code:

Copied
#include <jni.h>
#include <android/log.h>
#include <sentry.h>

#define TAG "sentry-android-demo"
extern "C" JNIEXPORT jstring JNICALL

Java_io_sentry_demo_NativeDemo_crash(JNIEnv *env, jclass cls) {
    __android_log_print(ANDROID_LOG_WARN, "", "Capture a message.");
    sentry_value_t event = sentry_value_new_message_event(
            /*   level */ SENTRY_LEVEL_INFO,
            /*  logger */ "custom",
            /* message */ "Sample message!"
    );
    sentry_capture_event(event);
}

The NDK integration can detect when a native or game thread stops responding, even when the app does not crash. Unlike Android ANR detection, which monitors the Android UI thread, app hang detection watches the thread that sends native heartbeats. This is useful for engines or native code with their own main loop.

Enable detection when you initialize the SDK, then call sentry_app_hang_heartbeat() regularly from the thread you want to monitor:

Copied
SentryAndroid.init(this) { options ->
    options.isEnableNdkAppHangTracking = true
    options.ndkAppHangTimeoutIntervalMillis = 2_000
}

The first heartbeat chooses the monitored thread. If it misses heartbeats for the configured timeout, the NDK integration captures an app hang event with that thread's stack trace. The timeout defaults to 5000 ms and has a minimum of 1000 ms.

Android ANR detection remains independent. If the same freeze blocks both the Android UI thread and your monitored native thread, Sentry can report both an ANR and an app hang.

You can also configure these options in AndroidManifest.xml, which is useful when the SDK initializes automatically:

AndroidManifest.xml
Copied
<application>
    <meta-data
    android:name="io.sentry.ndk.app-hang.enable"
    android:value="true"
  />
    <meta-data
    android:name="io.sentry.ndk.app-hang.timeout-interval-millis"
    android:value="2000"
  />
</application>

For details about the native watchdog and heartbeat behavior, see the Native SDK app hang documentation.

You can disable the NDK integration by adding the following to your AndroidManifest.xml:

AndroidManifest.xml
Copied
<application>
    <meta-data android:name="io.sentry.ndk.enable" android:value="false" />
</application>

You can use Sentry's Android SDK without the Android Native Development Kit (NDK) by either:

  • Disabling the NDK
  • Using sentry-android-core, which doesn't contain the NDK and can be used separately instead of sentry-android, when adding the dependency. The minimal required API level for sentry-android-core is 14.

If you're using our Gradle plugin, it'll still pull the NDK integration in as part of the auto-installation feature. To disable it, remove the sentry-android-ndk dependency from the app configurations in app/build.gradle:

Copied
configurations.configureEach {
  exclude group: "io.sentry", module: "sentry-android-ndk"
}

Unsymbolicated stack traces may be caused by module-caching on the SDK side. Be sure to clear any existing module cache if your modules are loaded dynamically:

Copied
// 1. Load a new module
load_my_module();

// 2. Clear the module cache
sentry_clear_modulecache();
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").