|
1 | 1 | import 'dart:ffi'; |
2 | | -import 'dart:io' show Platform; |
| 2 | +import 'dart:io' show Directory, Platform; |
| 3 | + |
| 4 | +import 'package:path/path.dart'; |
3 | 5 |
|
4 | 6 | import 'helpers.dart'; |
5 | 7 | import 'objectbox_c.dart'; |
@@ -31,29 +33,55 @@ ObjectBoxC? _tryObjectBoxLibProcess() { |
31 | 33 |
|
32 | 34 | ObjectBoxC? _tryObjectBoxLibFile() { |
33 | 35 | _lib = null; |
34 | | - var libName = 'objectbox'; |
| 36 | + final String libName; |
35 | 37 | 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'; |
42 | 39 | } 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'; |
49 | 41 | } else if (Platform.isAndroid) { |
50 | | - libName = 'lib' + libName + '-jni.so'; |
| 42 | + libName = 'libobjectbox-jni.so'; |
51 | 43 | } else if (Platform.isLinux) { |
52 | | - libName = 'lib' + libName + '.so'; |
| 44 | + libName = 'libobjectbox.so'; |
53 | 45 | } else { |
| 46 | + // Other platforms not supported (for iOS see _tryObjectBoxLibProcess). |
54 | 47 | return null; |
55 | 48 | } |
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 | + } |
57 | 85 | return ObjectBoxC(_lib!); |
58 | 86 | } |
59 | 87 |
|
|
0 commit comments