-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[path_provider_android] Changes internal implementation to use JNI #9770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request refactors the Android implementation of path_provider to use jnigen and JNI instead of Pigeon for platform communication. This is a significant architectural change. While the implementation looks mostly correct, there are several potential memory leaks due to unreleased JNI objects. I've added specific comments on how to fix them.
A major concern is the removal of all existing tests without adding new ones to cover the new JNI-based implementation. The repository's style guide states that "Code should be tested"[^1]. Given the low-level nature of JNI interop, having a solid test suite is critical to ensure correctness and prevent regressions. Please add tests for the new implementation.
| final JArray<File?>? files = _applicationContext.getExternalCacheDirs(); | ||
| if (files != null) { | ||
| return _toStringList(files); | ||
| } | ||
|
|
||
| return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JArray<File?> object returned by getExternalCacheDirs() is a JNI object that needs to be released to avoid a memory leak. The current implementation does not release it.
final JArray<File?>? files = _applicationContext.getExternalCacheDirs();
if (files == null) {
return null;
}
try {
return _toStringList(files);
} finally {
files.release();
}
| final JArray<File?>? files = _applicationContext.getExternalFilesDirs( | ||
| type != null ? _toNativeStorageDirectory(type) : null, | ||
| ); | ||
| if (files != null) { | ||
| return _toStringList(files); | ||
| } | ||
|
|
||
| return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JArray<File?> object returned by getExternalFilesDirs() is a JNI object that needs to be released to avoid a memory leak. The current implementation does not release it.
final JArray<File?>? files = _applicationContext.getExternalFilesDirs(
type != null ? _toNativeStorageDirectory(type) : null,
);
if (files == null) {
return null;
}
try {
return _toStringList(files);
} finally {
files.release();
}
| final Iterator<File?> filesIterator = files.iterator; | ||
| while (filesIterator.moveNext()) { | ||
| final File? file = filesIterator.current; | ||
| if (file != null) { | ||
| final String? path = | ||
| file.getAbsolutePath()?.toDartString(releaseOriginal: true); | ||
| if (path != null) { | ||
| paths.add(path); | ||
| } | ||
|
|
||
| file.release(); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two potential memory leaks here:
- The iterator returned by
files.iteratoris aJIterator, which is a JNI object that must be released. - If
file.getAbsolutePath()ortoDartString()throws an exception,file.release()will not be called.
Both can be fixed by using try...finally blocks to ensure release() is always called.
final Iterator<File?> filesIterator = files.iterator;
try {
while (filesIterator.moveNext()) {
final File? file = filesIterator.current;
if (file != null) {
try {
final String? path =
file.getAbsolutePath()?.toDartString(releaseOriginal: true);
if (path != null) {
paths.add(path);
}
} finally {
file.release();
}
}
}
} finally {
(filesIterator as JObject).release();
}
| final File? file = _applicationContext.getCacheDir(); | ||
| final String? path = file?.getPath()?.toDartString(releaseOriginal: true); | ||
| file?.release(); | ||
| return path; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To ensure that the File object is always released, even if getPath() or toDartString() throws an exception, it's safer to use a try...finally block.
final File? file = _applicationContext.getCacheDir();
if (file == null) {
return null;
}
try {
return file.getPath()?.toDartString(releaseOriginal: true);
} finally {
file.release();
}
| final File? dir = _applicationContext.getExternalFilesDir(null); | ||
| if (dir != null) { | ||
| final String? path = | ||
| dir.getAbsolutePath()?.toDartString(releaseOriginal: true); | ||
| dir.release(); | ||
| return path; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To ensure that the File object (dir) is always released, even if an exception occurs, it's best practice to use a try...finally block.
final File? dir = _applicationContext.getExternalFilesDir(null);
if (dir == null) {
return null;
}
try {
return dir.getAbsolutePath()?.toDartString(releaseOriginal: true);
} finally {
dir.release();
}
|
cc @stuartmorgan-g for early review |
|
@HosseinYousefi Have you seen this error on Windows for JNI: |
|
This is similar to getsentry/sentry-dart#3033 So I write what I wrote there here as well:
Originally posted by @HosseinYousefi in #3033 I can fix this by making the windows build show a warning instead of an error if it couldn't find JDK. |
...ovider_android/android/src/main/java/io/flutter/plugins/pathprovider/PathProviderPlugin.java
Outdated
Show resolved
Hide resolved
packages/path_provider/path_provider_android/lib/src/path_provider.g.dart
Show resolved
Hide resolved
packages/path_provider/path_provider_android/lib/path_provider_android.dart
Outdated
Show resolved
Hide resolved
| path: 'lib/src/third_party/path_provider.g.dart' | ||
| structure: 'single_file' | ||
|
|
||
| ## Classes / packages for which bindings need to be generated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does jnigen support include-based filtering yet? It was added to jnigen, and instead of a generated file that was huge, like the 12k lines in this PR, it's only about 600.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be blocked on dart-lang/native#2962.
|
Oops, I lost a comment because GitHub won't let me save a comment on the deleted file for reason. I had this on the deleted Dart unit tests: I would expect these tests to be moved to the integration test runner rather than removed, so that we are still unit testing how we handle specific responses to from the system layer. |
|
Greetings from stale PR triage! 👋 |
|
The PR was blocked by |
|
I pushed a bunch of changes:
Currently we're gated on |
https://developer.android.com/guide/components/processes-and-threads
Pre-Review Checklist
[shared_preferences]pubspec.yamlwith an appropriate new version according to the pub versioning philosophy, or I have commented below to indicate which version change exemption this PR falls under1.CHANGELOG.mdto add a description of the change, following repository CHANGELOG style, or I have commented below to indicate which CHANGELOG exemption this PR falls under1.///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.Footnotes
Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2 ↩3