applyWorkaroundToOpenSqlCipherOnOldAndroidVersions function

Future<void> applyWorkaroundToOpenSqlCipherOnOldAndroidVersions()

Workaround to open sqlcipher on old Android versions.

On old Android versions, this method can help if you're having issues opening sqlite3 (e.g. if you're seeing crashes about libsqlcipher.so not being available). To be safe, call this method before using apis from package:sqlite3 or package:moor/ffi.dart.

Big thanks to @knaeckeKami for finding this workaround!!

Implementation

Future<void> applyWorkaroundToOpenSqlCipherOnOldAndroidVersions() async {
  if (!Platform.isAndroid) return;

  try {
    DynamicLibrary.open('libsqlcipher.so');
  } on ArgumentError {
    // Ok, the regular approach failed. Try to open sqlite3 in Java, which seems
    // to fix the problem.
    await _platform.invokeMethod('doesnt_matter');

    // Try again. If it still fails we're out of luck.
    DynamicLibrary.open('libsqlcipher.so');
  }
}