Skip to content

Commit 2a8b410

Browse files
bindings.dart: always try loading from 'lib' folder first.
Also print message with potential solutions if loading fails.
1 parent 9ec91f9 commit 2a8b410

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

objectbox/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## latest
22

3+
* The native ObjectBox library is also searched for in the `lib` subfolder on desktop OS (macOS,
4+
Linux, Windows). This is where the [`install.sh`](/install.sh) script downloads it by default.
5+
E.g. it is no longer necessary to install the library globally to run `dart test` or `flutter test`.
6+
37
## 1.4.1 (2022-03-01)
48

59
* Resolve "another store is still open" issue after Flutter hot restart (hot reload continues to work). #387

objectbox/lib/src/native/bindings/bindings.dart

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import 'dart:ffi';
2-
import 'dart:io' show Platform;
2+
import 'dart:io' show Directory, Platform;
3+
4+
import 'package:path/path.dart';
35

46
import 'helpers.dart';
57
import 'objectbox_c.dart';
@@ -31,29 +33,55 @@ ObjectBoxC? _tryObjectBoxLibProcess() {
3133

3234
ObjectBoxC? _tryObjectBoxLibFile() {
3335
_lib = null;
34-
var libName = 'objectbox';
36+
final String libName;
3537
if (Platform.isWindows) {
36-
libName += '.dll';
37-
try {
38-
_lib = DynamicLibrary.open(libName);
39-
} on ArgumentError {
40-
libName = 'lib/' + libName;
41-
}
38+
libName = 'objectbox.dll';
4239
} else if (Platform.isMacOS) {
43-
libName = 'lib' + libName + '.dylib';
44-
try {
45-
_lib = DynamicLibrary.open(libName);
46-
} on ArgumentError {
47-
libName = '/usr/local/lib/' + libName;
48-
}
40+
libName = 'libobjectbox.dylib';
4941
} else if (Platform.isAndroid) {
50-
libName = 'lib' + libName + '-jni.so';
42+
libName = 'libobjectbox-jni.so';
5143
} else if (Platform.isLinux) {
52-
libName = 'lib' + libName + '.so';
44+
libName = 'libobjectbox.so';
5345
} else {
46+
// Other platforms not supported (for iOS see _tryObjectBoxLibProcess).
5447
return null;
5548
}
56-
_lib ??= DynamicLibrary.open(libName);
49+
// For desktop OS prefer version in 'lib' subfolder as this is where
50+
// install.sh (which calls objectbox-c download.sh) puts the library.
51+
if (Platform.isWindows || Platform.isMacOS || Platform.isLinux) {
52+
// Must use absolute directory as relative directory fails on macOS
53+
// due to security restrictions ("file system relative paths not allowed in
54+
// hardened programs").
55+
String libPath = join(Directory.current.path, "lib", libName);
56+
try {
57+
_lib = DynamicLibrary.open(libPath);
58+
} on ArgumentError {
59+
// On macOS also try /usr/local/lib, this is where the objectbox-c
60+
// download script installs to as well.
61+
if (Platform.isMacOS) {
62+
try {
63+
_lib ??= DynamicLibrary.open('/usr/local/lib/' + libName);
64+
} on ArgumentError {
65+
// Ignore.
66+
}
67+
}
68+
// Try default path, see below.
69+
}
70+
}
71+
try {
72+
// This will look in some standard places for shared libraries:
73+
// - on Android in the JNI lib folder for the architecture
74+
// - on Linux in /lib and /usr/lib
75+
// - on macOS?
76+
// - on Windows in the working directory and System32
77+
_lib ??= DynamicLibrary.open(libName);
78+
} catch (e) {
79+
print("Failed to load ObjectBox library. For Flutter apps, check if "
80+
"objectbox_flutter_libs is added to dependencies. "
81+
"For unit tests and Dart apps, check if the ObjectBox library was "
82+
"downloaded (https://docs.objectbox.io/getting-started).");
83+
rethrow;
84+
}
5785
return ObjectBoxC(_lib!);
5886
}
5987

0 commit comments

Comments
 (0)