Skip to content

[NPE] io.flutter.util.PathUtils.getCacheDirectory()  #74294

Description

@gzjaychen

Our app received some crash report:

W/ContextImpl( 2303): Unable to create files subdir /data/user/0/myappname/code_cache
E/FlutterLoader( 2303): Flutter initialization failed.
E/FlutterLoader(2303): java.util.concurrent.ExecutionException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.File.getPath()' on a null object reference
E/FlutterLoader( 2303): at io.flutter.embedding.engine.loader.FlutterLoader.ensureInitializationComplete(FlutterLoader.java:193)
E/FlutterLoader( 2303): at io.flutter.view.FlutterMain.ensureInitializationComplete(FlutterMain.java:85)
...
E/FlutterLoader( 2303): at io.flutter.util.PathUtils.getCacheDirectory(PathUtils.java:21)
E/FlutterLoader( 2303): at io.flutter.embedding.engine.loader.FlutterLoader$1.call(FlutterLoader.java:164)
E/FlutterLoader( 2303): at io.flutter.embedding.engine.loader.FlutterLoader$1.call(FlutterLoader.java:140)

io.flutter.util.PathUtils.java

public static String getCacheDirectory(Context applicationContext) {
    return VERSION.SDK_INT >= 21 ? applicationContext.getCodeCacheDir().getPath() : applicationContext.getCacheDir().getPath();
}

NPE on Android 6.x and below, because Context.getCodeCacheDir() will return null.
http://androidxref.com/6.0.1_r10/xref/frameworks/base/core/java/android/app/ContextImpl.java#508

 * The system will delete any files stored in this location both when your
 * specific application is upgraded, and when the entire platform is
 * upgraded.
getCodeCacheDir() {
    //...some code
    return createFilesDirLocked(mCodeCacheDir); // Return null **_if no space left on device._**
}

All flutter versions have this problem.

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work lista: productionIssues experienced in live production appsc: crashStack traces logged to the consoleengineflutter/engine related. See also e: labels.platform-androidAndroid applications specifically

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions