Android Native Development Kit (NDK)
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, 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 NDK libraries into your project so that the compiler can link the libraries during the build.
To do so, use the AndroidNativeBundle Gradle plugin that copies the native libraries from the Sentry NDK into the location that can provide links via the CmakeLists.txt
configuration file.
First, we need to declare the dependency in the project build.gradle file:
build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
// Add the line below, the plugin that copies the binaries
// https://github.com/howardpang/androidNativeBundle/releases
classpath 'io.github.howardpang:androidNativeBundle:{version}'
}
}
Once the dependency is declared, you can use it in the application build.gradle
:
apply plugin: 'com.ydq.android.gradle.native-aar.import'
Then update the CmakeLists.txt
configuration to link the Sentry NDK libraries:
# include paths generated by androidNativeBundle
include (${ANDROID_GRADLE_NATIVE_BUNDLE_PLUGIN_MK})
# change native-lib to your native lib's name
target_link_libraries(native-lib ${ANDROID_GRADLE_NATIVE_MODULES})
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);
}
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>
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-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
:
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:
// load a new module
sentry_clear_modulecache();
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").
- Package:
- maven:io.sentry:sentry-android
- Version:
- 7.8.0
- Repository:
- https://github.com/getsentry/sentry-java
- API Documentation:
- https://javadoc.io/doc/io.sentry