11package io .sentry .android .ndk ;
22
33import io .sentry .android .core .SentryAndroidOptions ;
4+ import java .util .concurrent .CountDownLatch ;
5+ import java .util .concurrent .TimeUnit ;
46import org .jetbrains .annotations .ApiStatus ;
57import org .jetbrains .annotations .NotNull ;
68
79@ ApiStatus .Internal
810public final class SentryNdk {
911
12+ private static final @ NotNull CountDownLatch loadLibraryLatch = new CountDownLatch (1 );
13+
1014 private SentryNdk () {}
1115
1216 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 ();
2138 }
2239
2340 private static native void initSentryNative (@ NotNull final SentryAndroidOptions options );
@@ -31,14 +48,23 @@ private SentryNdk() {}
3148 */
3249 public static void init (@ NotNull final SentryAndroidOptions options ) {
3350 SentryNdkUtil .addPackage (options .getSdkVersion ());
34- initSentryNative (options );
51+ try {
52+ if (loadLibraryLatch .await (2000 , TimeUnit .MILLISECONDS )) {
53+ initSentryNative (options );
3554
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+ }
4059
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+ }
4268 }
4369
4470 /** Closes the NDK integration */
0 commit comments