@@ -42,52 +42,70 @@ public class NativeLibraryLoader {
42
42
static {
43
43
String libname = OBJECTBOX_JNI ;
44
44
String filename = libname + ".so" ;
45
- boolean isLinux = false ;
45
+
46
+ final String vendor = System .getProperty ("java.vendor" );
47
+ final String osName = System .getProperty ("os.name" ).toLowerCase ();
48
+ final String sunArch = System .getProperty ("sun.arch.data.model" );
49
+
50
+ // Some Android devices are detected as neither Android or Linux below,
51
+ // so assume Linux by default to always fallback to Android
52
+ boolean isLinux = true ;
46
53
// For Android, os.name is also "Linux", so we need an extra check
47
54
// Is not completely reliable (e.g. Vivo devices), see workaround on load failure
48
55
// Note: can not use check for Android classes as testing frameworks (Robolectric)
49
56
// may provide them on non-Android devices
50
- boolean android = System . getProperty ( "java. vendor" ) .contains ("Android" );
57
+ final boolean android = vendor .contains ("Android" );
51
58
if (!android ) {
52
- String osName = System .getProperty ("os.name" ).toLowerCase ();
53
- String sunArch = System .getProperty ("sun.arch.data.model" );
54
59
String cpuArchPostfix = "32" .equals (sunArch ) ? "-x86" : "-x64" ;
55
60
if (osName .contains ("windows" )) {
61
+ isLinux = false ;
56
62
libname += "-windows" + cpuArchPostfix ;
57
63
filename = libname + ".dll" ;
58
64
checkUnpackLib (filename );
59
65
} else if (osName .contains ("linux" )) {
60
- isLinux = true ;
61
66
libname += "-linux" + cpuArchPostfix ;
62
67
filename = "lib" + libname + ".so" ;
63
68
checkUnpackLib (filename );
64
69
} else if (osName .contains ("mac" )) {
70
+ isLinux = false ;
65
71
libname += "-macos" + cpuArchPostfix ;
66
72
filename = "lib" + libname + ".dylib" ;
67
73
checkUnpackLib (filename );
68
74
}
69
75
}
70
- File file = new File (filename );
71
- if (file .exists ()) {
72
- System .load (file .getAbsolutePath ());
73
- } else {
74
- if (!android ) {
75
- System .err .println ("File not available: " + file .getAbsolutePath ());
76
- }
77
- try {
78
- if (!android || !loadLibraryAndroid (libname )) {
79
- System .loadLibrary (libname );
80
- }
81
- } catch (UnsatisfiedLinkError e ) {
82
- if (!android && isLinux ) {
83
- // maybe is Android, but check failed: try loading Android lib
84
- if (!loadLibraryAndroid (OBJECTBOX_JNI )) {
85
- System .loadLibrary (OBJECTBOX_JNI );
76
+ try {
77
+ File file = new File (filename );
78
+ if (file .exists ()) {
79
+ System .load (file .getAbsolutePath ());
80
+ } else {
81
+ try {
82
+ if (android ) {
83
+ boolean success = loadLibraryAndroid ();
84
+ if (!success ) {
85
+ System .loadLibrary (libname );
86
+ }
87
+ } else {
88
+ System .err .println ("File not available: " + file .getAbsolutePath ());
89
+ System .loadLibrary (libname );
90
+ }
91
+ } catch (UnsatisfiedLinkError e ) {
92
+ if (!android && isLinux ) {
93
+ // maybe is Android, but check failed: try loading Android lib
94
+ boolean success = loadLibraryAndroid ();
95
+ if (!success ) {
96
+ System .loadLibrary (OBJECTBOX_JNI );
97
+ }
98
+ } else {
99
+ throw e ;
86
100
}
87
- } else {
88
- throw e ;
89
101
}
90
102
}
103
+ } catch (UnsatisfiedLinkError e ) {
104
+ String message = String .format (
105
+ "Loading ObjectBox native library failed: vendor=%s,os=%s,arch=%s,android=%s,linux=%s" ,
106
+ vendor , osName , sunArch , android , isLinux
107
+ );
108
+ throw new LinkageError (message , e ); // UnsatisfiedLinkError does not allow a cause; use its super class
91
109
}
92
110
}
93
111
@@ -124,8 +142,7 @@ private static void checkUnpackLib(String filename) {
124
142
}
125
143
}
126
144
127
- @ SuppressWarnings ("BooleanMethodIsAlwaysInverted" ) // more readable
128
- private static boolean loadLibraryAndroid (String libname ) {
145
+ private static boolean loadLibraryAndroid () {
129
146
if (BoxStore .context == null ) {
130
147
return false ;
131
148
}
@@ -137,11 +154,11 @@ private static boolean loadLibraryAndroid(String libname) {
137
154
// use default ReLinker
138
155
Class <?> relinker = Class .forName ("com.getkeepsafe.relinker.ReLinker" );
139
156
Method loadLibrary = relinker .getMethod ("loadLibrary" , context , String .class , String .class );
140
- loadLibrary .invoke (null , BoxStore .context , libname , BoxStore .JNI_VERSION );
157
+ loadLibrary .invoke (null , BoxStore .context , OBJECTBOX_JNI , BoxStore .JNI_VERSION );
141
158
} else {
142
159
// use custom ReLinkerInstance
143
160
Method loadLibrary = BoxStore .relinker .getClass ().getMethod ("loadLibrary" , context , String .class , String .class );
144
- loadLibrary .invoke (BoxStore .relinker , BoxStore .context , libname , BoxStore .JNI_VERSION );
161
+ loadLibrary .invoke (BoxStore .relinker , BoxStore .context , OBJECTBOX_JNI , BoxStore .JNI_VERSION );
145
162
}
146
163
} catch (NoSuchMethodException e ) {
147
164
return false ;
0 commit comments