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.
Starting with Sentry Android SDK version 8.32.0 you can enable the Tombstone Integration as a replacement or extension to the NDK integration.
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.
Android prefab support can only be used with Sentry Android SDK version 8.0.0 and above.
Enable prefab and add the sentry-native ndk dependency directly to your module:
app/build.gradleandroid {
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>")
}
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.txtfind_package(sentry-native-ndk REQUIRED CONFIG)
target_link_libraries(<app> PRIVATE
sentry-native-ndk::sentry-android
sentry-native-ndk::sentry
)
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:
#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);
}
#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.
Experimental
NDK app hang detection is experimental. It requires Sentry Android SDK version 8.48.0 or later and the NDK integration.
Enable detection when you initialize the SDK, then call sentry_app_hang_heartbeat() regularly from the thread you want to monitor:
SentryAndroid.init(this) { options ->
options.isEnableNdkAppHangTracking = true
options.ndkAppHangTimeoutIntervalMillis = 2_000
}
SentryAndroid.init(this) { options ->
options.isEnableNdkAppHangTracking = true
options.ndkAppHangTimeoutIntervalMillis = 2_000
}
SentryAndroid.init(this, options -> {
options.setEnableNdkAppHangTracking(true);
options.setNdkAppHangTimeoutIntervalMillis(2_000);
});
// Call from the thread whose responsiveness represents your app.
void game_loop(void) {
while (is_running()) {
sentry_app_hang_heartbeat();
run_one_frame();
}
}
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<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>
<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<application>
<meta-data android:name="io.sentry.ndk.enable" android:value="false" />
</application>
<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 ofsentry-android, when adding the dependency. The minimal required API level forsentry-android-coreis 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:
configurations.configureEach {
exclude group: "io.sentry", module: "sentry-android-ndk"
}
configurations.configureEach {
exclude group: "io.sentry", module: "sentry-android-ndk"
}
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:
// 1. Load a new module
load_my_module();
// 2. Clear the module cache
sentry_clear_modulecache();
// 1. Load a new module
load_my_module();
// 2. Clear the module cache
sentry_clear_modulecache();
// 1. Manually initialize the SDK,
// in order to keep a reference to the SDK options
var optionsRef: SentryAndroidOptions? = null
SentryAndroid.init(this) { options ->
options.dsn = "___PUBLIC_DSN___"
// ...
optionsRef = options
}
// 2. Load a new module
System.loadLibrary("<library>")
// 3. Clear the cache
optionsRef?.debugImagesLoader?.clearDebugImages()
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").