Skip to content

Commit

Permalink
[NDK] Add loadNativeLibraries method to allow pre-loading .so files (#…
Browse files Browse the repository at this point in the history
…1082)

* Add loadNativeLibraries method to allow pre-loading .so files

* Update Changelog
  • Loading branch information
markushi authored Nov 19, 2024
1 parent d8230a8 commit 2ad5f0d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

**Features**:

- Android NDK: Add `.loadNativeLibraries()` method to allow pre-loading .so files ([#1082](https://github.com/getsentry/sentry-native/pull/1082))

## 0.7.13

**Features**:
Expand Down
1 change: 1 addition & 0 deletions ndk/lib/api/sentry-native-ndk.api
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@ public final class io/sentry/ndk/NdkOptions {
public final class io/sentry/ndk/SentryNdk {
public static fun close ()V
public static fun init (Lio/sentry/ndk/NdkOptions;)V
public static fun loadNativeLibraries ()V
}

31 changes: 21 additions & 10 deletions ndk/lib/src/main/java/io/sentry/ndk/SentryNdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,7 @@
@ApiStatus.Internal
public final class SentryNdk {

static {
// On older Android versions, it was necessary to manually call "`System.loadLibrary` on all
// transitive dependencies before loading [the] main library."
// The dependencies of `libsentry.so` are currently `lib{c,m,dl,log}.so`.
// See
// https://android.googlesource.com/platform/bionic/+/master/android-changes-for-ndk-developers.md#changes-to-library-dependency-resolution
System.loadLibrary("log");
System.loadLibrary("sentry");
System.loadLibrary("sentry-android");
}
private static volatile boolean nativeLibrariesLoaded;

private SentryNdk() {}

Expand All @@ -29,11 +20,31 @@ private SentryNdk() {}
* @param options the SentryAndroidOptions
*/
public static void init(@NotNull final NdkOptions options) {
loadNativeLibraries();
initSentryNative(options);
}

/** Closes the NDK integration */
public static void close() {
loadNativeLibraries();
shutdown();
}

/**
* Loads all required native libraries. This is automatically done by {@link #init(NdkOptions)},
* but can be called manually in case you want to preload the libraries before calling #init.
*/
public static synchronized void loadNativeLibraries() {
if (!nativeLibrariesLoaded) {
// On older Android versions, it was necessary to manually call "`System.loadLibrary` on all
// transitive dependencies before loading [the] main library."
// The dependencies of `libsentry.so` are currently `lib{c,m,dl,log}.so`.
// See
// https://android.googlesource.com/platform/bionic/+/master/android-changes-for-ndk-developers.md#changes-to-library-dependency-resolution
System.loadLibrary("log");
System.loadLibrary("sentry");
System.loadLibrary("sentry-android");
nativeLibrariesLoaded = true;
}
}
}

0 comments on commit 2ad5f0d

Please sign in to comment.