flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
166.16k stars 27.48k forks source link

[in_app_purchase][android] launchMode="singleInstance", opening Play Store bottom sheet, and navigating away from app and back kills the Flutter app #118752

Open msisinni opened 1 year ago

msisinni commented 1 year ago

Steps to Reproduce

  1. With any Flutter application using the in_app_purchase package, open your main / release AndroidManifest.xml file (by default in /android/app/src/main/)
  2. Set android:launchMode="singleInstance" (either by changing the existing value for launchMode or adding that line if you don't have it.
  3. Execute flutter run --release while a physical Android device is plugged in (Note: this also happens on versions of apps installed from the Play Store if you build the app and upload it there)
  4. Navigate to wherever you can make purchases in your app, and tap to make a purchase. It does not matter what type of purchase it is - subscription, consumable, etc. This should open the Play Store bottom sheet.
  5. Navigate away from your app by hitting the home button or recents button or by swiping (whichever you have enabled).
  6. Navigate back to your app from the recents list*. The Play Store bottom sheet should be visible, but your app will not be visible underneath it. If you either continue making a purchase or dismiss the bottom sheet, trying to open the app again from your recent apps won't work. You'll have to swipe it away from your recent apps and relaunch it.

*If you navigate back to your app by tapping on its icon from your list of installed apps, it'll open your Flutter app again as if the Play Store bottom sheet wasn't opened. It's almost like the Play Store bottom sheet is switched to another Activity than the one the Flutter app is running on.

Expected results: Having a Play Store bottom sheet open, navigating away from the app, and navigating back to the app not having any impact on the app while launchMode="singleInstance"

Actual results: Having a Play Store bottom sheet open, navigating away from the app, and navigating back to the app from recents appears to replace the Activity that the Flutter app is running from with a new Activity that is only running the Play Store's bottom sheet. All traces of Flutter seem to just get detached or killed without respecting the Flutter lifecycle. This can also affect the next launch of the app, because the app got killed while a purchase was pending. If the user navigates back to the app while it's in the error state and make a purchase, further attempts to make a purchase might run into an IAPError with message "BillingResponse.itemAlreadyOwned". Restoring purchases will work for subscriptions if the user subscribes in that Play Store bottom sheet only screen, but hitting restore purchases for consumables seems like it gives a new consumable every time the restore purchase button is tapped, regardless of how many were purchased. Trying to buy a consumable in that unlimited-restore-purchase state runs into the "itemAlreadyOwned" message. That might not happen in an app that verifies purchases with the app's server though - I only tested consumables with the example app.

I'm not going to attach any code samples because of how much setup an app requires to be ready for in-app purchases. If you just add that android:launchMode="singleInstance" line to any project that is already set up for in-app purchases, you should be able to run into this issue.

Logs ``` Analyzing example... No issues found! (ran in 1.6s) ``` ``` [✓] Flutter (Channel stable, 3.3.10, on macOS 12.6 21G115 darwin-arm, locale en-US) • Flutter version 3.3.10 on channel stable at /Users/matthewsisinni/Documents/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 135454af32 (5 weeks ago), 2022-12-15 07:36:55 -0800 • Engine revision 3316dd8728 • Dart version 2.18.6 • DevTools version 2.15.0 [✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0-rc1) • Android SDK at /Users/matthewsisinni/Library/Android/sdk • Platform android-33, build-tools 32.0.0-rc1 • ANDROID_HOME = /Users/matthewsisinni/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 14.0.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 14A400 • CocoaPods version 1.11.3 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2021.2) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840) [✓] IntelliJ IDEA Ultimate Edition (version 2022.1) • IntelliJ at /Applications/IntelliJ IDEA.app • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin version 221.5588 [✓] Connected device (3 available) • Pixel 6 Pro (mobile) • 1A271FDEE007AT • android-arm64 • Android 13 (API 33) • macOS (desktop) • macos • darwin-arm64 • macOS 12.6 21G115 darwin-arm • Chrome (web) • chrome • web-javascript • Google Chrome 109.0.5414.87 [✓] HTTP Host Availability • All required HTTP hosts are available • No issues found! ```
Screenshots This shows the example app from the in_app_purchases repo, configured for my developer account. ![Screenshot_20230118-172855](https://user-images.githubusercontent.com/6568129/213310317-4392a726-dee4-4516-87d7-2fc5687eba80.png) Opening the Play Store bottom sheet by hitting the Silver option's button ![Screenshot_20230118-172908](https://user-images.githubusercontent.com/6568129/213310407-086afa7b-68c5-4043-bbfd-fdeb93210274.png) Hitting the recents button before then hitting the home button ![Screenshot_20230118-172951](https://user-images.githubusercontent.com/6568129/213310418-45247678-59c5-4037-88b0-8be49279ff13.png) Hitting the recents button after having gone to the home screen. Note that it stopped showing the Flutter app in the background and is instead showing my home screen behind the Play Store bottom sheet. ![Screenshot_20230118-172958](https://user-images.githubusercontent.com/6568129/213310427-53829754-c97a-41e6-bd7a-bcc7e7cef9b5.png) Dismissing the Play Store bottom sheet and hitting the recents button again. Tapping on the app does not open it back up or close recents. ![Screenshot_20230118-173021](https://user-images.githubusercontent.com/6568129/213310482-8dff9e56-9901-45ea-b0de-4d143527567d.png) Image showing what happens if you go through all those steps, but, instead of dismissing the bottom sheet, you subscribe to the app, clear the app from recents (because it can't be opened), relaunch it, and attempt to buy the same product. Note that the previous images all involved the "Silver" option. This image focuses on going through the whole process with the "Gold" one. It doesn't make a difference which one is used. ![Screenshot_20230118-173126](https://user-images.githubusercontent.com/6568129/213310491-52fe0b1d-6487-4c89-962a-b47c675a45d8.png)

thanks to @eliasteeny for figuring out it was singleInstance

danagbemava-nc commented 1 year ago

Issue is reproducible using the code sample from the plugin example https://pub.dev/packages/in_app_purchase/example.

I see an exception thrown when I try to start a new purchase after returning from the background in the recording below. The exception is shared in the logs below.

Labeling for further investigation.

recording | singleInstance | singleTop | | -- | -- | |
logs ``` Launching lib/main.dart on sdk gphone64 arm64 in debug mode... ✓ Built build/app/outputs/flutter-apk/app-debug.apk. Connecting to VM Service at ws://127.0.0.1:57778/xlDl9AVl_UE=/ws E/SurfaceSyncer( 4778): Failed to find sync for id=0 W/Parcel ( 4778): Expecting binder but got null! D/EGL_emulation( 4778): app_time_stats: avg=110.10ms min=8.07ms max=1451.45ms count=19 D/EGL_emulation( 4778): app_time_stats: avg=3885.80ms min=62.52ms max=7709.07ms count=2 W/Parcel ( 4778): Expecting binder but got null! I/gic.iap_android( 4778): Background concurrent copying GC freed 17360(1314KB) AllocSpace objects, 3(60KB) LOS objects, 49% free, 2546KB/5092KB, paused 216us,49us total 279.092ms E/libEGL ( 4778): call to OpenGL ES API with no current context (logged once per thread) D/CompatibilityChangeReporter( 4778): Compat change id reported: 78294732; UID 10158; state: ENABLED W/ProxyBillingActivity( 4778): Activity finished with resultCode 0 and billing's responseCode: 1 W/ProxyBillingActivity( 4778): Got exception while trying to start a purchase flow. W/ProxyBillingActivity( 4778): android.content.IntentSender$SendIntentException W/ProxyBillingActivity( 4778): at android.app.Activity.startIntentSenderForResultInner(Activity.java:5857) W/ProxyBillingActivity( 4778): at android.app.Activity.startIntentSenderForResult(Activity.java:5822) W/ProxyBillingActivity( 4778): at android.app.Activity.startIntentSenderForResult(Activity.java:5777) W/ProxyBillingActivity( 4778): at com.android.billingclient.api.ProxyBillingActivity.onCreate(com.android.billingclient:billing@@5.0.0:12) W/ProxyBillingActivity( 4778): at android.app.Activity.performCreate(Activity.java:8290) W/ProxyBillingActivity( 4778): at android.app.Activity.performCreate(Activity.java:8269) W/ProxyBillingActivity( 4778): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1384) W/ProxyBillingActivity( 4778): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3657) W/ProxyBillingActivity( 4778): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3813) W/ProxyBillingActivity( 4778): at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101) W/ProxyBillingActivity( 4778): at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) W/ProxyBillingActivity( 4778): at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) W/ProxyBillingActivity( 4778): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2308) W/ProxyBillingActivity( 4778): at android.os.Handler.dispatchMessage(Handler.java:106) W/ProxyBillingActivity( 4778): at android.os.Looper.loopOnce(Looper.java:201) W/ProxyBillingActivity( 4778): at android.os.Looper.loop(Looper.java:288) W/ProxyBillingActivity( 4778): at android.app.ActivityThread.main(ActivityThread.java:7898) W/ProxyBillingActivity( 4778): at java.lang.reflect.Method.invoke(Native Method) W/ProxyBillingActivity( 4778): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) W/ProxyBillingActivity( 4778): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) W/ProxyBillingActivity( 4778): Got exception while trying to start a purchase flow. W/ProxyBillingActivity( 4778): android.content.IntentSender$SendIntentException W/ProxyBillingActivity( 4778): at android.app.Activity.startIntentSenderForResultInner(Activity.java:5857) W/ProxyBillingActivity( 4778): at android.app.Activity.startIntentSenderForResult(Activity.java:5822) W/ProxyBillingActivity( 4778): at android.app.Activity.startIntentSenderForResult(Activity.java:5777) W/ProxyBillingActivity( 4778): at com.android.billingclient.api.ProxyBillingActivity.onCreate(com.android.billingclient:billing@@5.0.0:12) W/ProxyBillingActivity( 4778): at android.app.Activity.performCreate(Activity.java:8290) W/ProxyBillingActivity( 4778): at android.app.Activity.performCreate(Activity.java:8269) W/ProxyBillingActivity( 4778): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1384) W/ProxyBillingActivity( 4778): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3657) W/ProxyBillingActivity( 4778): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3813) W/ProxyBillingActivity( 4778): at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101) W/ProxyBillingActivity( 4778): at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) W/ProxyBillingActivity( 4778): at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) W/ProxyBillingActivity( 4778): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2308) W/ProxyBillingActivity( 4778): at android.os.Handler.dispatchMessage(Handler.java:106) W/ProxyBillingActivity( 4778): at android.os.Looper.loopOnce(Looper.java:201) W/ProxyBillingActivity( 4778): at android.os.Looper.loop(Looper.java:288) W/ProxyBillingActivity( 4778): at android.app.ActivityThread.main(ActivityThread.java:7898) W/ProxyBillingActivity( 4778): at java.lang.reflect.Method.invoke(Native Method) W/ProxyBillingActivity( 4778): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) W/ProxyBillingActivity( 4778): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) Application finished. Exited ```
flutter doctor -v ``` [✓] Flutter (Channel stable, 3.3.10, on macOS 13.1 22C65 darwin-arm, locale en-GB) • Flutter version 3.3.10 on channel stable at /Users/nexus/dev/sdks/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 135454af32 (5 weeks ago), 2022-12-15 07:36:55 -0800 • Engine revision 3316dd8728 • Dart version 2.18.6 • DevTools version 2.15.0 [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0) • Android SDK at /Users/nexus/Library/Android/sdk • Platform android-33, build-tools 33.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 14.2) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 14C18 • CocoaPods version 1.11.3 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [!] Android Studio (version 2022.1) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart ✗ Unable to find bundled Java version. • Try updating or re-installing Android Studio. [!] Android Studio (version 2022.1) • Android Studio at /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/221.6008.13.2211.9477386/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart ✗ Unable to find bundled Java version. • Try updating or re-installing Android Studio. [✓] IntelliJ IDEA Community Edition (version 2022.2) • IntelliJ at /Applications/IntelliJ IDEA CE.app • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart [✓] VS Code (version 1.74.3) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.56.0 [✓] Connected device (3 available) • sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64 • Android 13 (API 33) (emulator) • macOS (desktop) • macos • darwin-arm64 • macOS 13.1 22C65 darwin-arm • Chrome (web) • chrome • web-javascript • Google Chrome 109.0.5414.87 [✓] HTTP Host Availability • All required HTTP hosts are available ! Doctor found issues in 2 categories. ``` ``` [!] Flutter (Channel master, 3.7.0-23.0.pre.21, on macOS 13.1 22C65 darwin-arm64, locale en-GB) • Flutter version 3.7.0-23.0.pre.21 on channel master at /Users/nexus/dev/sdks/flutters ! Warning: `flutter` on your path resolves to /Users/nexus/dev/sdks/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/nexus/dev/sdks/flutters. Consider adding /Users/nexus/dev/sdks/flutters/bin to the front of your path. ! Warning: `dart` on your path resolves to /Users/nexus/dev/sdks/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/nexus/dev/sdks/flutters. Consider adding /Users/nexus/dev/sdks/flutters/bin to the front of your path. • Upstream repository https://github.com/flutter/flutter.git • Framework revision 6a9b2db4ac (8 hours ago), 2023-01-18 21:03:37 -0500 • Engine revision 95b0c151f7 • Dart version 3.0.0 (build 3.0.0-136.0.dev) • DevTools version 2.20.0 • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades. [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0) • Android SDK at /Users/nexus/Library/Android/sdk • Platform android-33, build-tools 33.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 14.2) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 14C18 • CocoaPods version 1.11.3 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [!] Android Studio (version 2022.1) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart ✗ Unable to find bundled Java version. • Try updating or re-installing Android Studio. [!] Android Studio (version 2022.1) • Android Studio at /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/221.6008.13.2211.9477386/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart ✗ Unable to find bundled Java version. • Try updating or re-installing Android Studio. [✓] IntelliJ IDEA Community Edition (version 2022.2) • IntelliJ at /Applications/IntelliJ IDEA CE.app • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart [✓] VS Code (version 1.74.3) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.56.0 [✓] Connected device (3 available) • sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64 • Android 13 (API 33) (emulator) • macOS (desktop) • macos • darwin-arm64 • macOS 13.1 22C65 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 109.0.5414.87 [✓] HTTP Host Availability • All required HTTP hosts are available ! Doctor found issues in 3 categories. ```
msisinni commented 1 year ago

It looks like a similar thing was happening with the image_picker package. https://github.com/flutter/flutter/issues/76856

That team added a note about using singleInstance to their docs. https://github.com/flutter/packages/commit/528aa2dd1c722d77f03db1adf4ed798714e605bf