Skip to content

Commit 2ad5f0d

Browse files
authored
[NDK] Add loadNativeLibraries method to allow pre-loading .so files (#1082)
* Add loadNativeLibraries method to allow pre-loading .so files * Update Changelog
1 parent d8230a8 commit 2ad5f0d

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
**Features**:
6+
7+
- Android NDK: Add `.loadNativeLibraries()` method to allow pre-loading .so files ([#1082](https://github.com/getsentry/sentry-native/pull/1082))
8+
39
## 0.7.13
410

511
**Features**:

ndk/lib/api/sentry-native-ndk.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,6 @@ public final class io/sentry/ndk/NdkOptions {
7979
public final class io/sentry/ndk/SentryNdk {
8080
public static fun close ()V
8181
public static fun init (Lio/sentry/ndk/NdkOptions;)V
82+
public static fun loadNativeLibraries ()V
8283
}
8384

ndk/lib/src/main/java/io/sentry/ndk/SentryNdk.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,7 @@
66
@ApiStatus.Internal
77
public final class SentryNdk {
88

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

2011
private SentryNdk() {}
2112

@@ -29,11 +20,31 @@ private SentryNdk() {}
2920
* @param options the SentryAndroidOptions
3021
*/
3122
public static void init(@NotNull final NdkOptions options) {
23+
loadNativeLibraries();
3224
initSentryNative(options);
3325
}
3426

3527
/** Closes the NDK integration */
3628
public static void close() {
29+
loadNativeLibraries();
3730
shutdown();
3831
}
32+
33+
/**
34+
* Loads all required native libraries. This is automatically done by {@link #init(NdkOptions)},
35+
* but can be called manually in case you want to preload the libraries before calling #init.
36+
*/
37+
public static synchronized void loadNativeLibraries() {
38+
if (!nativeLibrariesLoaded) {
39+
// On older Android versions, it was necessary to manually call "`System.loadLibrary` on all
40+
// transitive dependencies before loading [the] main library."
41+
// The dependencies of `libsentry.so` are currently `lib{c,m,dl,log}.so`.
42+
// See
43+
// https://android.googlesource.com/platform/bionic/+/master/android-changes-for-ndk-developers.md#changes-to-library-dependency-resolution
44+
System.loadLibrary("log");
45+
System.loadLibrary("sentry");
46+
System.loadLibrary("sentry-android");
47+
nativeLibrariesLoaded = true;
48+
}
49+
}
3950
}

0 commit comments

Comments
 (0)