Skip to content

Commit

Permalink
Highlight Android workarounds more
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Oct 24, 2023
1 parent ca84c19 commit 8077c56
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/assets/path_provider/lib/path_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ import 'dart:io';
Future<Directory> getApplicationDocumentsDirectory() {
throw UnsupportedError('stub!');
}

Future<Directory> getTemporaryDirectory() {
throw UnsupportedError('stub!');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Future<void> applyWorkaroundToOpenSqlite3OnOldAndroidVersions() async {
throw 'stub!';
}
6 changes: 6 additions & 0 deletions docs/assets/sqlite3_flutter_libs/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: sqlite3_flutter_libs
publish_to: none
description: Fake "sqlite3_flutter_libs" package so that we can import it in snippets without depending on Flutter.

environment:
sdk: ^2.16.0
14 changes: 14 additions & 0 deletions docs/lib/snippets/setup/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import 'dart:io';
import 'package:drift/native.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;
import 'package:sqlite3/sqlite3.dart';
import 'package:sqlite3_flutter_libs/sqlite3_flutter_libs.dart';

// ... the TodoItems table definition stays the same
// #enddocregion open
Expand Down Expand Up @@ -47,6 +49,18 @@ LazyDatabase _openConnection() {
// for your app.
final dbFolder = await getApplicationDocumentsDirectory();
final file = File(p.join(dbFolder.path, 'db.sqlite'));

// Also work around limitations on old Android versions
if (Platform.isAndroid) {
await applyWorkaroundToOpenSqlite3OnOldAndroidVersions();

final cachebase = (await getTemporaryDirectory()).path;

// We can't access /tmp on Android, which sqlite3 would try by default.
// Explicitly tell it about the correct temporary directory.
sqlite3.tempDirectory = cachebase;
}

return NativeDatabase.createInBackground(file);
});
}
Expand Down
5 changes: 5 additions & 0 deletions docs/pages/docs/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ now looks like this:

{% include "blocks/snippet" snippets = snippets name = 'open' %}

The Android-specific workarounds are necessary because sqlite3 attempts to use `/tmp` to store
private data on unix-like systems, which is forbidden on Android. We also use this opportunity
to work around a problem some older Android devices have with loading custom libraries through
`dart:ffi`.

## Next steps

Congratulations! With this setup complete, your project is ready to use drift.
Expand Down
4 changes: 3 additions & 1 deletion docs/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ dependencies:
# used in snippets
http: ^1.1.0
sqlite3: ^2.0.0
# Fake path_provider for snippets
# Fake flutter packages for snippets
path_provider:
path: assets/path_provider
sqlite3_flutter_libs:
path: assets/sqlite3_flutter_libs
# Used in examples
rxdart: ^0.27.3
yaml: ^3.1.1
Expand Down
4 changes: 4 additions & 0 deletions examples/app/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ android {
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName

ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86_64'
}
}

buildTypes {
Expand Down
16 changes: 15 additions & 1 deletion examples/app/lib/database/connection/native.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'package:drift_dev/api/migrations.dart';
import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;
import 'package:sqlite3/sqlite3.dart';
import 'package:sqlite3_flutter_libs/sqlite3_flutter_libs.dart';

Future<File> get databaseFile async {
// We use `path_provider` to find a suitable path to store our data in.
Expand All @@ -17,7 +19,19 @@ Future<File> get databaseFile async {
/// Obtains a database connection for running drift in a Dart VM.
DatabaseConnection connect() {
return DatabaseConnection.delayed(Future(() async {
return NativeDatabase.createBackgroundConnection(await databaseFile);
if (Platform.isAndroid) {
await applyWorkaroundToOpenSqlite3OnOldAndroidVersions();

final cachebase = (await getTemporaryDirectory()).path;

// We can't access /tmp on Android, which sqlite3 would try by default.
// Explicitly tell it about the correct temporary directory.
sqlite3.tempDirectory = cachebase;
}

return NativeDatabase.createBackgroundConnection(
await databaseFile,
);
}));
}

Expand Down

0 comments on commit 8077c56

Please sign in to comment.