Skip to content

Commit b93ac43

Browse files
Android 6: call compat native lib loading for all Flutter apps #27
Note: Flutter plugin method is only called on Android, also is a no-op on devices newer than Android 6 (API level M).
1 parent 54d8c61 commit b93ac43

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

generator/lib/src/code_chunks.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class CodeChunks {
4646
/// Note: for desktop apps it is recommended to specify a unique [directory].
4747
///
4848
/// See [Store.new] for an explanation of all parameters.
49+
///
50+
/// For Flutter apps, also calls `loadObjectBoxLibraryAndroidCompat()` from
51+
/// the ObjectBox Flutter library to fix loading the native ObjectBox library
52+
/// on Android 6 and older.
4953
${openStore(model, pubspec)}
5054
5155
/// Returns the ObjectBox model definition for this project for use with
@@ -63,6 +67,11 @@ class CodeChunks {
6367
${model.entities.mapIndexed(_metaClass).join("\n")}
6468
""";
6569

70+
/// Builds openStore method code string wrapping the [Store] constructor.
71+
///
72+
/// If the ObjectBox Flutter dependency is detected in [pubspec], will
73+
/// add its compat loading for the Android library and use its default
74+
/// directory detection. Also the method will become async.
6675
static String openStore(ModelInfo model, Pubspec? pubspec) {
6776
final obxFlutter = pubspec?.hasObxFlutterDependency ?? false;
6877
return '''${obxFlutter ? 'Future<$obx.Store>' : '$obx.Store'} openStore(
@@ -71,15 +80,16 @@ class CodeChunks {
7180
int? fileMode,
7281
int? maxReaders,
7382
bool queriesCaseSensitiveDefault = true,
74-
String? macosApplicationGroup})${obxFlutter ? ' async' : ''} =>
75-
$obx.Store(getObjectBoxModel(),
83+
String? macosApplicationGroup})${obxFlutter ? ' async' : ''} {
84+
${obxFlutter ? 'await loadObjectBoxLibraryAndroidCompat();' : ''}
85+
return $obx.Store(getObjectBoxModel(),
7686
directory: directory${obxFlutter ? ' ?? (await defaultStoreDirectory()).path' : ''},
7787
maxDBSizeInKB: maxDBSizeInKB,
7888
fileMode: fileMode,
7989
maxReaders: maxReaders,
8090
queriesCaseSensitiveDefault: queriesCaseSensitiveDefault,
8191
macosApplicationGroup: macosApplicationGroup);
82-
''';
92+
}''';
8393
}
8494

8595
static List<T> sorted<T>(List<T> list) {

objectbox/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
## latest
22

33
* Avoid conflicts with entity class names in generated code [#519](https://github.com/objectbox/objectbox-dart/issues/519)
4+
* For Flutter apps: `loadObjectBoxLibraryAndroidCompat()` is now called by default when using
5+
`openStore()` (effective after re-running `flutter pub run build_runner build`). For devices
6+
running Android 6 or older this will pre-load the ObjectBox library in Java to prevent errors when
7+
loading it in Dart.
8+
9+
If your code was calling the compat method manually, remove the call and re-run above command.
10+
11+
Let us know if there are issues with this change in [#369](https://github.com/objectbox/objectbox-dart/issues/369)!
412

513
## 2.4.0 (2023-12-13)
614

0 commit comments

Comments
 (0)