-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Fix splash screen with theme references crash on Android API 21 #26083
Fix splash screen with theme references crash on Android API 21 #26083
Conversation
Fixes flutter/flutter#73118 by providing theme reference on API 21. Catch NotFoundException that may occur from drawable parsing failure.
Friendly ping @jason-simmons, you suggested against catching the exception in engine#17894. Your thoughts about this? |
I was concerned that catching this exception could hide real errors in applications using FlutterActivity. But if some Lollipop devices are unable to resolve the resources in the splash screen, then I guess it's preferable to catch it here. |
To clarify, NotFoundException won't occur on API 21 anymore. The reason to catch it is to prevent such crashes on pre API 21 devices where theme references in xml are just not supported. |
In the default Flutter app template the https://github.com/flutter/flutter/blob/master/packages/flutter_tools/templates/app/android.tmpl/app/src/main/res/drawable-v21/launch_background.xml Is the |
The main motive is to protect from avoidable crashes. Splash screens are often modified. Using unsupported theme references on older API levels is something that can occur (more so in Flutter projects). And since the crashes don't occur in newer Android versions, this can easily be missed. It happened with flutter/flutter#65182. Then you fixed crashes on API<21 devices with flutter/flutter#69255. This can also happen in other apps. Native android projects are protected from this by a lint warning but not Flutter projects. It is worthwhile to prevent crashes at launch in this case. While testing on older API levels, seeing a blank splash screen and the detailed log warning is enough to alert the dev. |
Verifies app doesn't crash when: 1. Splash screen metadata is not defined. 2. Splash screen drawable is not found.
@jonahwilliams, this is ready. |
Fixes java formatting
@jason-simmons I've tried to change |
@behzodfaiziev As a temporary fix, you can also use a color element to still support dark backgrounds. |
shell/platform/android/io/flutter/embedding/android/FlutterActivity.java
Outdated
Show resolved
Hide resolved
shell/platform/android/io/flutter/embedding/android/FlutterFragmentActivity.java
Outdated
Show resolved
Hide resolved
/cc @blasten |
First, thanks for the contribution.
@itsarjunsinh This sounds like the real bug. The Android API surface is much larger than the property you are looking at, so we can't possibly catch all the exceptions to address similar concerns. Would you be interested in exploring calling the linter for release builds? |
@chinmaygarde I don't think adding the linter is being tracked. In my experiment, the linter makes the build times notably slower. So in the spirit of Flutter's fast iteration times it might be wise to just run it on release builds. Secondly, I had trouble running the linter on newer JDK. It will likely require flutter/flutter#62924. That said, To address crashes on API 21 a fix is still required. return splashScreenId != null
// Previously > Build.VERSION_CODES.LOLLIPOP
? Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
? getResources().getDrawable(splashScreenId, getTheme())
: getResources().getDrawable(splashScreenId)
: null; We could reduce a couple lines and rely on ResourcesCompat (it's already bundled). Under the hood it makes the same call but the AndroidX team have many tests setup (including themed drawables). return splashScreenId != null
? ResourcesCompat.getDrawable(getResources(), splashScreenId, getTheme())
: null; The test coverage for when no splash screen is defined in manifest is straightforward. However, I'm not sure the current test setup allows for loading drawable files. The AndroidX Core ResourcesCompat test uses an actual drawable file. Coincidentally, even the linter recommends calls through ResourcesCompat instead of accessing it directly. Please give me a go ahead to use it, it'll make the code more robust and streamlined. |
shell/platform/android/io/flutter/embedding/android/FlutterActivity.java
Outdated
Show resolved
Hide resolved
shell/platform/android/io/flutter/embedding/android/FlutterFragmentActivity.java
Outdated
Show resolved
Hide resolved
Hi @itsarjunsinh. Please apply the changes, and the formatter ( |
@blasten, I've incorporated @dnfield and your suggestions. Can you take a look again?
|
LGTM per @blasten s last comment which seems to have been addressed. |
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.
LGTM
Fixes flutter/flutter#73118 by providing theme reference on API 21.
What's changed?
writing and running engine tests.
///
).