Skip to content

Commit 2ef3e55

Browse files
committed
NativeLibraryLoader: use System.getProperty("os.arch") to detect CPU arch more accurately and prepare for ARM
1 parent 74a2b36 commit 2ef3e55

1 file changed

Lines changed: 42 additions & 4 deletions

File tree

objectbox-java/src/main/java/io/objectbox/internal/NativeLibraryLoader.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public class NativeLibraryLoader {
4545

4646
final String vendor = System.getProperty("java.vendor");
4747
final String osName = System.getProperty("os.name").toLowerCase();
48-
final String sunArch = System.getProperty("sun.arch.data.model");
4948

5049
// Some Android devices are detected as neither Android or Linux below,
5150
// so assume Linux by default to always fallback to Android
@@ -56,7 +55,7 @@ public class NativeLibraryLoader {
5655
// may provide them on non-Android devices
5756
final boolean android = vendor.contains("Android");
5857
if (!android) {
59-
String cpuArchPostfix = "32".equals(sunArch) ? "-x86" : "-x64";
58+
String cpuArchPostfix = "-" + getCpuArch();
6059
if (osName.contains("windows")) {
6160
isLinux = false;
6261
libname += "-windows" + cpuArchPostfix;
@@ -101,14 +100,53 @@ public class NativeLibraryLoader {
101100
}
102101
}
103102
} catch (UnsatisfiedLinkError e) {
103+
String osArch = System.getProperty("os.arch");
104+
String sunArch = System.getProperty("sun.arch.data.model");
104105
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
106+
"Loading ObjectBox native library failed: vendor=%s,os=%s,os.arch=%s,sun.arch=%s,android=%s,linux=%s",
107+
vendor, osName, osArch, sunArch, android, isLinux
107108
);
108109
throw new LinkageError(message, e); // UnsatisfiedLinkError does not allow a cause; use its super class
109110
}
110111
}
111112

113+
private static String getCpuArch() {
114+
String osArch = System.getProperty("os.arch");
115+
String cpuArch = null;
116+
if (osArch != null) {
117+
osArch = osArch.toLowerCase();
118+
if (osArch.equalsIgnoreCase("amd64") || osArch.equalsIgnoreCase("x86_64")) {
119+
cpuArch = "x64";
120+
} else if (osArch.equalsIgnoreCase("x86")) {
121+
cpuArch = "x86";
122+
} else if (osArch.startsWith("arm")) {
123+
switch (osArch) {
124+
case "armv7":
125+
case "armv7l":
126+
case "armeabi-v7a": // os.arch "armeabi-v7a" might be Android only, but let's try anyway...
127+
cpuArch = "armv7";
128+
break;
129+
case "arm64-v8a":
130+
cpuArch = "arm64";
131+
break;
132+
case "armv6":
133+
cpuArch = "armv6";
134+
break;
135+
default:
136+
cpuArch = "armv6"; // Lowest version we support
137+
System.err.println("Unknown os.arch \"" + osArch + "\" - ObjectBox is defaulting to " + cpuArch);
138+
break;
139+
}
140+
}
141+
}
142+
if (cpuArch == null) {
143+
String sunArch = System.getProperty("sun.arch.data.model");
144+
cpuArch = "32".equals(sunArch) ? "x86" : "x64";
145+
System.err.println("Unknown os.arch \"" + osArch + "\" - ObjectBox is defaulting to " + cpuArch);
146+
}
147+
return cpuArch;
148+
}
149+
112150
private static void checkUnpackLib(String filename) {
113151
String path = "/native/" + filename;
114152
URL resource = NativeLibraryLoader.class.getResource(path);

0 commit comments

Comments
 (0)