Skip to content

Commit ea7e031

Browse files
authored
[shared_preferences_android] Fix web compilation of apps that import the package (flutter#13380)
Since 2.5.0 (flutter/packages#12329), `package:shared_preferences_android/shared_preferences_android.dart` transitively imports `package:jni`, which needs `dart:ffi`. So any app that imports it fails to compile for web, even if it only uses `SharedPreferencesAsyncAndroidOptions` the way the `shared_preferences` README shows. This currently breaks `shared_preferences/example` in the web unit test shards on every Flutter roll PR: ``` jni-1.1.0/lib/src/jni.dart:5:8: Error: Dart library 'dart:ffi' is not available on this platform. ``` Example CI failures: - https://ci.chromium.org/b/8667231073363438673 - https://ci.chromium.org/b/8667231073314809361 - https://ci.chromium.org/b/8667308711483341649 This uses the same approach as `path_provider_android`: the library exports a stub of the plugin classes when `dart.library.ffi` isn't available, and the option types move to their own file that doesn't depend on FFI. The package's own tests now import `src/shared_preferences_android_real.dart` directly, since the analyzer resolves the conditional export to the stub. Test coverage comes from the existing `shared_preferences/example` web tests, which import this library and fail to compile without this change.
1 parent a40053c commit ea7e031

10 files changed

Lines changed: 109 additions & 46 deletions

‎packages/shared_preferences/shared_preferences_android/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.5.3
2+
3+
* Fixes web compilation of apps that import `package:shared_preferences_android`, for example to use `SharedPreferencesAsyncAndroidOptions`.
4+
15
## 2.5.2
26

37
* Fixes a `NoSuchMethodError` at startup in Android release builds by adding consumer R8 rules that keep the Kotlin classes looked up through JNI.

‎packages/shared_preferences/shared_preferences_android/example/integration_test/shared_preferences_test.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import 'package:flutter/services.dart';
66
import 'package:flutter_test/flutter_test.dart';
77
import 'package:integration_test/integration_test.dart';
8-
import 'package:shared_preferences_android/shared_preferences_android.dart';
98
import 'package:shared_preferences_android/src/messages_async.g.dart';
9+
import 'package:shared_preferences_android/src/shared_preferences_android_real.dart';
1010
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
1111
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
1212
import 'package:shared_preferences_platform_interface/types.dart';

‎packages/shared_preferences/shared_preferences_android/lib/shared_preferences_android.dart‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
export 'src/shared_preferences_android.dart';
6-
export 'src/shared_preferences_async_android.dart';
5+
// shared_preferences_android is implemented using JNI, which requires FFI;
6+
// export a stub for platforms that don't support FFI (e.g., web) so that
7+
// multi-platform apps importing this library for its options still compile.
8+
export 'src/shared_preferences_android_stub.dart'
9+
if (dart.library.ffi) 'src/shared_preferences_android_real.dart';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
export 'shared_preferences_android.dart';
6+
export 'shared_preferences_async_android.dart';
7+
export 'shared_preferences_async_android_options.dart';
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
6+
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
7+
8+
export 'shared_preferences_async_android_options.dart';
9+
10+
// Stub of the public API surface for platforms without FFI (e.g., web), where
11+
// the real JNI-based implementation can't be compiled. The plugin classes are
12+
// never registered on those platforms, so their methods should never actually
13+
// be called.
14+
15+
/// The Android implementation of [SharedPreferencesStorePlatform].
16+
///
17+
/// This class implements the `package:shared_preferences` functionality for Android.
18+
class SharedPreferencesAndroid extends SharedPreferencesStorePlatform {
19+
/// Creates a new plugin implementation instance.
20+
SharedPreferencesAndroid();
21+
22+
/// Registers this class as the default instance of [SharedPreferencesStorePlatform].
23+
static void registerWith() {}
24+
25+
@override
26+
dynamic noSuchMethod(Invocation invocation) => throw UnsupportedError(_unsupportedMessage);
27+
}
28+
29+
/// The Android implementation of [SharedPreferencesAsyncPlatform].
30+
///
31+
/// This class implements the `package:shared_preferences` functionality for Android.
32+
base class SharedPreferencesAsyncAndroid extends SharedPreferencesAsyncPlatform {
33+
/// Creates a new plugin implementation instance.
34+
SharedPreferencesAsyncAndroid();
35+
36+
/// Registers this class as the default instance of [SharedPreferencesAsyncPlatform].
37+
static void registerWith() {}
38+
39+
@override
40+
dynamic noSuchMethod(Invocation invocation) => throw UnsupportedError(_unsupportedMessage);
41+
}
42+
43+
const String _unsupportedMessage = 'shared_preferences_android is only supported on Android.';

‎packages/shared_preferences/shared_preferences_android/lib/src/shared_preferences_async_android.dart‎

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:shared_preferences_platform_interface/shared_preferences_async_p
1010
import 'package:shared_preferences_platform_interface/types.dart';
1111

1212
import 'messages_async.g.dart';
13+
import 'shared_preferences_async_android_options.dart';
1314
import 'strings.dart';
1415

1516
const String _listPrefix = 'VGhpcyBpcyB0aGUgcHJlZml4IGZvciBhIGxpc3Qu';
@@ -222,43 +223,3 @@ base class SharedPreferencesAsyncAndroid extends SharedPreferencesAsyncPlatform
222223
return data.cast<String, Object>();
223224
}
224225
}
225-
226-
/// Used to identify which Android library should be used on the backend of this call.
227-
enum SharedPreferencesAndroidBackendLibrary {
228-
/// Represents the newer DataStore Preferences library.
229-
DataStore,
230-
231-
/// Represents the older SharedPreferences library.
232-
SharedPreferences,
233-
}
234-
235-
/// Options for the Android specific SharedPreferences plugin.
236-
class SharedPreferencesAsyncAndroidOptions extends SharedPreferencesOptions {
237-
/// Constructor for SharedPreferencesAsyncAndroidOptions.
238-
const SharedPreferencesAsyncAndroidOptions({
239-
this.backend = SharedPreferencesAndroidBackendLibrary.DataStore,
240-
this.originalSharedPreferencesOptions,
241-
});
242-
243-
/// Which backend should be used for this method call.
244-
final SharedPreferencesAndroidBackendLibrary backend;
245-
246-
/// These options define how the `SharedPreferences` backend should behave.
247-
///
248-
/// Any options in this field will be ignored unless the backend that is selected
249-
/// is `SharedPreferences`.
250-
final AndroidSharedPreferencesStoreOptions? originalSharedPreferencesOptions;
251-
}
252-
253-
/// Options necessary for defining the use of the original `SharedPreferences`
254-
/// library.
255-
///
256-
/// These options are only ever used with the original `SharedPreferences` and
257-
/// have no purpose when using the default DataStore Preferences.
258-
class AndroidSharedPreferencesStoreOptions {
259-
/// Constructor for AndroidSharedPreferencesStoreOptions.
260-
const AndroidSharedPreferencesStoreOptions({this.fileName});
261-
262-
/// The name of the file in which the preferences are stored.
263-
final String? fileName;
264-
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:shared_preferences_platform_interface/types.dart';
6+
7+
/// Used to identify which Android library should be used on the backend of this call.
8+
enum SharedPreferencesAndroidBackendLibrary {
9+
/// Represents the newer DataStore Preferences library.
10+
DataStore,
11+
12+
/// Represents the older SharedPreferences library.
13+
SharedPreferences,
14+
}
15+
16+
/// Options for the Android specific SharedPreferences plugin.
17+
class SharedPreferencesAsyncAndroidOptions extends SharedPreferencesOptions {
18+
/// Constructor for SharedPreferencesAsyncAndroidOptions.
19+
const SharedPreferencesAsyncAndroidOptions({
20+
this.backend = SharedPreferencesAndroidBackendLibrary.DataStore,
21+
this.originalSharedPreferencesOptions,
22+
});
23+
24+
/// Which backend should be used for this method call.
25+
final SharedPreferencesAndroidBackendLibrary backend;
26+
27+
/// These options define how the `SharedPreferences` backend should behave.
28+
///
29+
/// Any options in this field will be ignored unless the backend that is selected
30+
/// is `SharedPreferences`.
31+
final AndroidSharedPreferencesStoreOptions? originalSharedPreferencesOptions;
32+
}
33+
34+
/// Options necessary for defining the use of the original `SharedPreferences`
35+
/// library.
36+
///
37+
/// These options are only ever used with the original `SharedPreferences` and
38+
/// have no purpose when using the default DataStore Preferences.
39+
class AndroidSharedPreferencesStoreOptions {
40+
/// Constructor for AndroidSharedPreferencesStoreOptions.
41+
const AndroidSharedPreferencesStoreOptions({this.fileName});
42+
43+
/// The name of the file in which the preferences are stored.
44+
final String? fileName;
45+
}

‎packages/shared_preferences/shared_preferences_android/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: shared_preferences_android
22
description: Android implementation of the shared_preferences plugin
33
repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences_android
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
5-
version: 2.5.2
5+
version: 2.5.3
66

77
environment:
88
sdk: ^3.12.0

‎packages/shared_preferences/shared_preferences_android/test/shared_preferences_android_test.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import 'dart:convert';
66

77
import 'package:flutter/services.dart';
88
import 'package:flutter_test/flutter_test.dart';
9-
import 'package:shared_preferences_android/shared_preferences_android.dart';
109
import 'package:shared_preferences_android/src/messages.g.dart';
10+
import 'package:shared_preferences_android/src/shared_preferences_android_real.dart';
1111
import 'package:shared_preferences_android/src/strings.dart';
1212
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
1313
import 'package:shared_preferences_platform_interface/types.dart';

‎packages/shared_preferences/shared_preferences_android/test/shared_preferences_async_test.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import 'dart:convert';
88

99
import 'package:flutter/services.dart';
1010
import 'package:flutter_test/flutter_test.dart';
11-
import 'package:shared_preferences_android/shared_preferences_android.dart';
1211
import 'package:shared_preferences_android/src/messages_async.g.dart';
12+
import 'package:shared_preferences_android/src/shared_preferences_android_real.dart';
1313
import 'package:shared_preferences_android/src/strings.dart';
1414
import 'package:shared_preferences_platform_interface/types.dart';
1515

0 commit comments

Comments
 (0)