Skip to content

Commit f52df41

Browse files
[path_provider] Remove dependency on engine PathUtils (#11467)
Removes the calls to the Flutter engine's `PathUtils`, and replaces them with direct `Context` calls that have the same logic as the expected codepath for those methods so that there will be continuity with previous releases of `path_provider`. This avoids reliance on `PathUtils` not being removed by R8, and also means that engine changes can't unexpectedly change the paths returned by `path_provider`, since we want to make sure any such changes in this package's return values follow semver. This does not include the fallback logic from flutter/engine#30367 because unlike the engine callers, `path_provider` doesn't try to assert a non-null return value, and if the paths aren't available (e.g., due to the device being full) we should return null rather than silently return a different directory than was previously returned. Fixes flutter/flutter#184750 ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 8c3019e commit f52df41

5 files changed

Lines changed: 19 additions & 153 deletions

File tree

packages/path_provider/path_provider_android/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.3.1
2+
3+
* Removes dependency on `PathUtils` to avoid a potential
4+
`ClassNotFoundException` when running in release mode.
5+
16
## 2.3.0
27

38
* Changes internal implementation to use JNI.

packages/path_provider/path_provider_android/lib/src/path_provider.g.dart

Lines changed: 0 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -7279,151 +7279,6 @@ final class $Context$Type$ extends jni$_.JType<Context> {
72797279
String get signature => r'Landroid/content/Context;';
72807280
}
72817281

7282-
/// from: `io.flutter.util.PathUtils`
7283-
extension type PathUtils._(jni$_.JObject _$this) implements jni$_.JObject {
7284-
static final _class = jni$_.JClass.forName(r'io/flutter/util/PathUtils');
7285-
7286-
/// The type which includes information such as the signature of this class.
7287-
static const jni$_.JType<PathUtils> type = $PathUtils$Type$();
7288-
static final _id_new$ = _class.constructorId(r'()V');
7289-
7290-
static final _new$ =
7291-
jni$_.ProtectedJniExtensions.lookup<
7292-
jni$_.NativeFunction<
7293-
jni$_.JniResult Function(
7294-
jni$_.Pointer<jni$_.Void>,
7295-
jni$_.JMethodIDPtr,
7296-
)
7297-
>
7298-
>('globalEnv_NewObject')
7299-
.asFunction<
7300-
jni$_.JniResult Function(
7301-
jni$_.Pointer<jni$_.Void>,
7302-
jni$_.JMethodIDPtr,
7303-
)
7304-
>();
7305-
7306-
/// from: `public void <init>()`
7307-
/// The returned object must be released after use, by calling the [release] method.
7308-
factory PathUtils() {
7309-
return _new$(
7310-
_class.reference.pointer,
7311-
_id_new$.pointer,
7312-
).object<PathUtils>();
7313-
}
7314-
7315-
static final _id_getFilesDir = _class.staticMethodId(
7316-
r'getFilesDir',
7317-
r'(Landroid/content/Context;)Ljava/lang/String;',
7318-
);
7319-
7320-
static final _getFilesDir =
7321-
jni$_.ProtectedJniExtensions.lookup<
7322-
jni$_.NativeFunction<
7323-
jni$_.JniResult Function(
7324-
jni$_.Pointer<jni$_.Void>,
7325-
jni$_.JMethodIDPtr,
7326-
jni$_.VarArgs<(jni$_.Pointer<jni$_.Void>,)>,
7327-
)
7328-
>
7329-
>('globalEnv_CallStaticObjectMethod')
7330-
.asFunction<
7331-
jni$_.JniResult Function(
7332-
jni$_.Pointer<jni$_.Void>,
7333-
jni$_.JMethodIDPtr,
7334-
jni$_.Pointer<jni$_.Void>,
7335-
)
7336-
>();
7337-
7338-
/// from: `static public java.lang.String getFilesDir(android.content.Context context)`
7339-
/// The returned object must be released after use, by calling the [release] method.
7340-
static jni$_.JString getFilesDir(Context context) {
7341-
final _$context = context.reference;
7342-
return _getFilesDir(
7343-
_class.reference.pointer,
7344-
_id_getFilesDir.pointer,
7345-
_$context.pointer,
7346-
).object<jni$_.JString>();
7347-
}
7348-
7349-
static final _id_getDataDirectory = _class.staticMethodId(
7350-
r'getDataDirectory',
7351-
r'(Landroid/content/Context;)Ljava/lang/String;',
7352-
);
7353-
7354-
static final _getDataDirectory =
7355-
jni$_.ProtectedJniExtensions.lookup<
7356-
jni$_.NativeFunction<
7357-
jni$_.JniResult Function(
7358-
jni$_.Pointer<jni$_.Void>,
7359-
jni$_.JMethodIDPtr,
7360-
jni$_.VarArgs<(jni$_.Pointer<jni$_.Void>,)>,
7361-
)
7362-
>
7363-
>('globalEnv_CallStaticObjectMethod')
7364-
.asFunction<
7365-
jni$_.JniResult Function(
7366-
jni$_.Pointer<jni$_.Void>,
7367-
jni$_.JMethodIDPtr,
7368-
jni$_.Pointer<jni$_.Void>,
7369-
)
7370-
>();
7371-
7372-
/// from: `static public java.lang.String getDataDirectory(android.content.Context context)`
7373-
/// The returned object must be released after use, by calling the [release] method.
7374-
static jni$_.JString getDataDirectory(Context context) {
7375-
final _$context = context.reference;
7376-
return _getDataDirectory(
7377-
_class.reference.pointer,
7378-
_id_getDataDirectory.pointer,
7379-
_$context.pointer,
7380-
).object<jni$_.JString>();
7381-
}
7382-
7383-
static final _id_getCacheDirectory = _class.staticMethodId(
7384-
r'getCacheDirectory',
7385-
r'(Landroid/content/Context;)Ljava/lang/String;',
7386-
);
7387-
7388-
static final _getCacheDirectory =
7389-
jni$_.ProtectedJniExtensions.lookup<
7390-
jni$_.NativeFunction<
7391-
jni$_.JniResult Function(
7392-
jni$_.Pointer<jni$_.Void>,
7393-
jni$_.JMethodIDPtr,
7394-
jni$_.VarArgs<(jni$_.Pointer<jni$_.Void>,)>,
7395-
)
7396-
>
7397-
>('globalEnv_CallStaticObjectMethod')
7398-
.asFunction<
7399-
jni$_.JniResult Function(
7400-
jni$_.Pointer<jni$_.Void>,
7401-
jni$_.JMethodIDPtr,
7402-
jni$_.Pointer<jni$_.Void>,
7403-
)
7404-
>();
7405-
7406-
/// from: `static public java.lang.String getCacheDirectory(android.content.Context context)`
7407-
/// The returned object must be released after use, by calling the [release] method.
7408-
static jni$_.JString getCacheDirectory(Context context) {
7409-
final _$context = context.reference;
7410-
return _getCacheDirectory(
7411-
_class.reference.pointer,
7412-
_id_getCacheDirectory.pointer,
7413-
_$context.pointer,
7414-
).object<jni$_.JString>();
7415-
}
7416-
}
7417-
7418-
final class $PathUtils$Type$ extends jni$_.JType<PathUtils> {
7419-
@jni$_.internal
7420-
const $PathUtils$Type$();
7421-
7422-
@jni$_.internal
7423-
@core$_.override
7424-
String get signature => r'Lio/flutter/util/PathUtils;';
7425-
}
7426-
74277282
/// from: `java.io.File`
74287283
extension type File._(jni$_.JObject _$this) implements jni$_.JObject {
74297284
static final _class = jni$_.JClass.forName(r'java/io/File');

packages/path_provider/path_provider_android/lib/src/path_provider_android_real.dart

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,23 @@ class PathProviderAndroid extends PathProviderPlatform {
2626

2727
@override
2828
Future<String?> getApplicationSupportPath() async {
29-
return PathUtils.getFilesDir(
30-
_applicationContext,
31-
).toDartString(releaseOriginal: true);
29+
final File? file = _applicationContext.filesDir;
30+
final String? path = file?.path?.toDartString(releaseOriginal: true);
31+
file?.release();
32+
return path;
3233
}
3334

3435
@override
3536
Future<String?> getApplicationDocumentsPath() async {
36-
return PathUtils.getDataDirectory(
37-
_applicationContext,
38-
).toDartString(releaseOriginal: true);
37+
final JString directory = 'flutter'.toJString();
38+
final File? file = _applicationContext.getDir(
39+
directory,
40+
Context.MODE_PRIVATE,
41+
);
42+
final String? path = file?.path?.toDartString(releaseOriginal: true);
43+
file?.release();
44+
directory.release();
45+
return path;
3946
}
4047

4148
@override

packages/path_provider/path_provider_android/pubspec.yaml

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

77
environment:
88
sdk: ^3.9.0

packages/path_provider/path_provider_android/tool/jnigen.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ void main() {
2222
),
2323
classes: <String>[
2424
'android.content.Context',
25-
'io.flutter.util.PathUtils',
2625
'java.io.File',
2726
'android.os.Environment',
2827
],

0 commit comments

Comments
 (0)