1
1
package io .sentry .android .ndk ;
2
2
3
3
import io .sentry .android .core .SentryAndroidOptions ;
4
+ import java .util .concurrent .CountDownLatch ;
5
+ import java .util .concurrent .TimeUnit ;
4
6
import org .jetbrains .annotations .ApiStatus ;
5
7
import org .jetbrains .annotations .NotNull ;
6
8
7
9
@ ApiStatus .Internal
8
10
public final class SentryNdk {
9
11
12
+ private static final @ NotNull CountDownLatch loadLibraryLatch = new CountDownLatch (1 );
13
+
10
14
private SentryNdk () {}
11
15
12
16
static {
13
- // On older Android versions, it was necessary to manually call "`System.loadLibrary` on all
14
- // transitive dependencies before loading [the] main library."
15
- // The dependencies of `libsentry.so` are currently `lib{c,m,dl,log}.so`.
16
- // See
17
- // https://android.googlesource.com/platform/bionic/+/master/android-changes-for-ndk-developers.md#changes-to-library-dependency-resolution
18
- System .loadLibrary ("log" );
19
- System .loadLibrary ("sentry" );
20
- System .loadLibrary ("sentry-android" );
17
+ new Thread (
18
+ () -> {
19
+ // On older Android versions, it was necessary to manually call "`System.loadLibrary`
20
+ // on all
21
+ // transitive dependencies before loading [the] main library."
22
+ // The dependencies of `libsentry.so` are currently `lib{c,m,dl,log}.so`.
23
+ // See
24
+ // https://android.googlesource.com/platform/bionic/+/master/android-changes-for-ndk-developers.md#changes-to-library-dependency-resolution
25
+ try {
26
+ System .loadLibrary ("log" );
27
+ System .loadLibrary ("sentry" );
28
+ System .loadLibrary ("sentry-android" );
29
+ } catch (Throwable t ) {
30
+ // ignored
31
+ // if loadLibrary() fails, the later init() will throw an exception anyway
32
+ } finally {
33
+ loadLibraryLatch .countDown ();
34
+ }
35
+ },
36
+ "SentryNdkLoadLibs" )
37
+ .start ();
21
38
}
22
39
23
40
private static native void initSentryNative (@ NotNull final SentryAndroidOptions options );
@@ -31,14 +48,23 @@ private SentryNdk() {}
31
48
*/
32
49
public static void init (@ NotNull final SentryAndroidOptions options ) {
33
50
SentryNdkUtil .addPackage (options .getSdkVersion ());
34
- initSentryNative (options );
51
+ try {
52
+ if (loadLibraryLatch .await (2000 , TimeUnit .MILLISECONDS )) {
53
+ initSentryNative (options );
35
54
36
- // only add scope sync observer if the scope sync is enabled.
37
- if (options .isEnableScopeSync ()) {
38
- options .addScopeObserver (new NdkScopeObserver (options ));
39
- }
55
+ // only add scope sync observer if the scope sync is enabled.
56
+ if (options .isEnableScopeSync ()) {
57
+ options .addScopeObserver (new NdkScopeObserver (options ));
58
+ }
40
59
41
- options .setDebugImagesLoader (new DebugImagesLoader (options , new NativeModuleListLoader ()));
60
+ options .setDebugImagesLoader (new DebugImagesLoader (options , new NativeModuleListLoader ()));
61
+ } else {
62
+ throw new IllegalStateException ("Timeout waiting for Sentry NDK library to load" );
63
+ }
64
+ } catch (InterruptedException e ) {
65
+ throw new IllegalStateException (
66
+ "Thread interrupted while waiting for NDK libs to be loaded" , e );
67
+ }
42
68
}
43
69
44
70
/** Closes the NDK integration */
0 commit comments