Skip to content

Commit 11841c1

Browse files
Support setting a custom ReLinkerInstance (e.g. to enable logging).
1 parent 5f52de2 commit 11841c1

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@
5959
@ThreadSafe
6060
public class BoxStore implements Closeable {
6161

62-
/** Android Context used for native library loading. */
62+
/** On Android used for native library loading. */
6363
@Nullable public static Object context;
64+
@Nullable public static Object relinker;
6465

6566
private static final String VERSION = "2.4.0-2019-01-08";
6667
private static BoxStore defaultStore;
@@ -190,6 +191,7 @@ public static boolean isObjectBrowserAvailable() {
190191

191192
BoxStore(BoxStoreBuilder builder) {
192193
context = builder.context;
194+
relinker = builder.relinker;
193195
NativeLibraryLoader.ensureLoaded();
194196

195197
directory = builder.directory;

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ public class BoxStoreBuilder {
6666
/** BoxStore uses this */
6767
File directory;
6868

69-
/** Android Context used for native library loading. */
70-
@Nullable
71-
Object context;
69+
/** On Android used for native library loading. */
70+
@Nullable Object context;
71+
@Nullable Object relinker;
7272

7373
/** Ignored by BoxStore */
7474
private File baseDirectory;
@@ -195,6 +195,18 @@ public BoxStoreBuilder androidContext(Object context) {
195195
return this;
196196
}
197197

198+
/**
199+
* Pass a custom ReLinkerInstance, for example {@code ReLinker.log(logger)} to use for loading the native library
200+
* on Android devices. Note that setting {@link #androidContext(Object)} is required for ReLinker to work.
201+
*/
202+
public BoxStoreBuilder androidReLinker(Object reLinkerInstance) {
203+
if (reLinkerInstance == null) {
204+
throw new NullPointerException("ReLinkerInstance may not be null");
205+
}
206+
this.relinker = reLinkerInstance;
207+
return this;
208+
}
209+
198210
static File getAndroidDbDir(Object context, @Nullable String dbName) {
199211
File baseDir = getAndroidBaseDir(context);
200212
return new File(baseDir, dbName(dbName));

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,16 @@ private static boolean loadLibraryAndroid(String libname) {
131131

132132
try {
133133
Class<?> context = Class.forName("android.content.Context");
134-
Class<?> relinker = Class.forName("com.getkeepsafe.relinker.ReLinker");
135-
// ReLinker.loadLibrary(Context context, String library)
136-
Method loadLibrary = relinker.getMethod("loadLibrary", context, String.class);
137-
138-
loadLibrary.invoke(null, BoxStore.context, libname);
134+
if (BoxStore.relinker == null) {
135+
// use default ReLinker
136+
Class<?> relinker = Class.forName("com.getkeepsafe.relinker.ReLinker");
137+
Method loadLibrary = relinker.getMethod("loadLibrary", context, String.class);
138+
loadLibrary.invoke(null, BoxStore.context, libname);
139+
} else {
140+
// use custom ReLinkerInstance
141+
Method loadLibrary = BoxStore.relinker.getClass().getMethod("loadLibrary", context, String.class);
142+
loadLibrary.invoke(BoxStore.relinker, BoxStore.context, libname);
143+
}
139144
} catch (ReflectiveOperationException e) {
140145
// note: do not catch Exception as it will swallow ReLinker exceptions useful for debugging
141146
return false;

0 commit comments

Comments
 (0)