Skip to content

Commit 0b294a0

Browse files
committed
Merge branch 'fix-verifyerror-android' into 'dev'
Fix BoxStoreBuilder crash on Android pre-K Closes #13 See merge request objectbox/objectbox-java!13
2 parents 6053394 + f84ced6 commit 0b294a0

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

objectbox-java/src/main/java/io/objectbox/BoxStoreBuilder.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.FileOutputStream;
2727
import java.io.InputStream;
2828
import java.io.OutputStream;
29+
import java.lang.reflect.InvocationTargetException;
2930
import java.lang.reflect.Method;
3031
import java.util.ArrayList;
3132
import java.util.List;
@@ -198,7 +199,8 @@ public BoxStoreBuilder androidContext(Object context) {
198199
private Object getApplicationContext(Object context) {
199200
try {
200201
return context.getClass().getMethod("getApplicationContext").invoke(context);
201-
} catch (ReflectiveOperationException e) {
202+
} catch (Exception e) {
203+
// note: can't catch ReflectiveOperationException, is K+ (19+) on Android
202204
throw new RuntimeException("context must be a valid Android Context", e);
203205
}
204206
}

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.IOException;
2626
import java.io.InputStream;
2727
import java.io.OutputStream;
28+
import java.lang.reflect.InvocationTargetException;
2829
import java.lang.reflect.Method;
2930
import java.net.URL;
3031
import java.net.URLConnection;
@@ -129,6 +130,7 @@ private static boolean loadLibraryAndroid(String libname) {
129130
return false;
130131
}
131132

133+
//noinspection TryWithIdenticalCatches
132134
try {
133135
Class<?> context = Class.forName("android.content.Context");
134136
if (BoxStore.relinker == null) {
@@ -141,10 +143,17 @@ private static boolean loadLibraryAndroid(String libname) {
141143
Method loadLibrary = BoxStore.relinker.getClass().getMethod("loadLibrary", context, String.class, String.class);
142144
loadLibrary.invoke(BoxStore.relinker, BoxStore.context, libname, BoxStore.JNI_VERSION);
143145
}
144-
} catch (ReflectiveOperationException e) {
145-
// note: do not catch Exception as it will swallow ReLinker exceptions useful for debugging
146+
} catch (NoSuchMethodException e) {
147+
return false;
148+
} catch (IllegalAccessException e) {
149+
return false;
150+
} catch (InvocationTargetException e) {
151+
return false;
152+
} catch (ClassNotFoundException e) {
146153
return false;
147154
}
155+
// note: do not catch Exception as it will swallow ReLinker exceptions useful for debugging
156+
// note: can't catch ReflectiveOperationException, is K+ (19+) on Android
148157

149158
return true;
150159
}

0 commit comments

Comments
 (0)