Skip to content

Commit 2f581bd

Browse files
Print supported ABIs when loading native library fails on Android.
1 parent 3d6f7e4 commit 2f581bd

File tree

1 file changed

+45
-6
lines changed

1 file changed

+45
-6
lines changed

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

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.objectbox.BoxStore;
2020
import org.greenrobot.essentials.io.IoUtils;
2121

22+
import javax.annotation.Nonnull;
2223
import javax.annotation.Nullable;
2324
import java.io.BufferedInputStream;
2425
import java.io.BufferedOutputStream;
@@ -29,10 +30,12 @@
2930
import java.io.InputStream;
3031
import java.io.InputStreamReader;
3132
import java.io.OutputStream;
33+
import java.lang.reflect.Field;
3234
import java.lang.reflect.InvocationTargetException;
3335
import java.lang.reflect.Method;
3436
import java.net.URL;
3537
import java.net.URLConnection;
38+
import java.util.Arrays;
3639

3740
/**
3841
* Separate class, so we can mock BoxStore.
@@ -106,12 +109,22 @@ public class NativeLibraryLoader {
106109
}
107110
} catch (UnsatisfiedLinkError e) {
108111
String osArch = System.getProperty("os.arch");
109-
String sunArch = System.getProperty("sun.arch.data.model");
110-
String message = String.format(
111-
"[ObjectBox] Loading native library failed, please report this to us: " +
112-
"vendor=%s,os=%s,os.arch=%s,model=%s,android=%s,linux=%s,machine=%s",
113-
vendor, osName, osArch, sunArch, android, isLinux, getCpuArchOSOrNull()
114-
);
112+
String message;
113+
if (android) {
114+
message = String.format(
115+
"[ObjectBox] Android failed to load native library," +
116+
" check your APK/App Bundle includes a supported ABI or use ReLinker" +
117+
" (vendor=%s,os=%s,os.arch=%s,SUPPORTED_ABIS=%s)",
118+
vendor, osName, osArch, getSupportedABIsAndroid()
119+
);
120+
} else {
121+
String sunArch = System.getProperty("sun.arch.data.model");
122+
message = String.format(
123+
"[ObjectBox] Loading native library failed, please report this to us: " +
124+
"vendor=%s,os=%s,os.arch=%s,model=%s,linux=%s,machine=%s",
125+
vendor, osName, osArch, sunArch, isLinux, getCpuArchOSOrNull()
126+
);
127+
}
115128
throw new LinkageError(message, e); // UnsatisfiedLinkError does not allow a cause; use its super class
116129
}
117130
}
@@ -262,6 +275,32 @@ private static boolean loadLibraryAndroid() {
262275
return true;
263276
}
264277

278+
/**
279+
* Return a string containing a list of ABIs that are supported by the current Android device.
280+
* If the Android device is below API level 21 (Android 5) or if looking up the value fails,
281+
* returns an empty string.
282+
*/
283+
@Nonnull
284+
private static String getSupportedABIsAndroid() {
285+
String[] supportedAbis = null;
286+
//noinspection TryWithIdenticalCatches
287+
try {
288+
Class<?> build = Class.forName("android.os.Build");
289+
Field supportedAbisField = build.getField("SUPPORTED_ABIS");
290+
supportedAbis = (String[]) supportedAbisField.get(null);
291+
} catch (NoSuchFieldException ignored) {
292+
} catch (IllegalAccessException ignored) {
293+
} catch (ClassNotFoundException ignored) {
294+
}
295+
// note: can't use multi-catch (would compile to ReflectiveOperationException, is K+ (19+) on Android)
296+
297+
if (supportedAbis != null) {
298+
return Arrays.toString(supportedAbis);
299+
} else {
300+
return "";
301+
}
302+
}
303+
265304
public static void ensureLoaded() {
266305
}
267306
}

0 commit comments

Comments
 (0)