Skip to content
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

java.lang.IllegalStateException upon ending billing connection. #106

Closed
dipinarora9 opened this issue Oct 3, 2019 · 23 comments · Fixed by #109, #111 or #126
Closed

java.lang.IllegalStateException upon ending billing connection. #106

dipinarora9 opened this issue Oct 3, 2019 · 23 comments · Fixed by #109, #111 or #126
Labels
android android related issue help wanted Extra attention is needed

Comments

@dipinarora9
Copy link
Contributor

Version of flutter_inapp_purchase : 2.0.0+12

Problem faced on a Android device - Vivo 1818

Hi @hyochan once again!

Today, I got a crash report from my play console the stack trace is shown below.

java.lang.IllegalStateException: 
  at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply (DartMessenger.java:35)
  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.error (MethodChannel.java:14)
  at com.dooboolab.flutterinapppurchase.AndroidInappPurchasePlugin$1.onBillingServiceDisconnected (AndroidInappPurchasePlugin.java:10)
  at com.android.billingclient.api.BillingClientImpl$BillingServiceConnection.onServiceDisconnected (BillingClientImpl.java:28)
  at android.app.LoadedApk$ServiceDispatcher.doDeath (LoadedApk.java:1789)
  at android.app.LoadedApk$ServiceDispatcher$RunConnection.run (LoadedApk.java:1804)
  at android.os.Handler.handleCallback (Handler.java:873)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loop (Looper.java:224)
  at android.app.ActivityThread.main (ActivityThread.java:7157)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:537)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:876)

My guess is that, this might have occured when the plugin called upon end connection of billing services.

Is this stacktrace of any help for making the plugin more stable.
Thanks in advance!

@hyochan hyochan added android android related issue help wanted Extra attention is needed labels Oct 4, 2019
@hyochan
Copy link
Owner

hyochan commented Oct 4, 2019

I think it looks expected. Try to wrap your initConnection code with try and catch statement and see if this is still crashing.

@dipinarora9
Copy link
Contributor Author

The published app is using the following statement, as of now. I will wrap this statement within the try and catch block in my next update. Moreover, I didn't personally encountered this issue on any of my devices, so it will be hard for me to check if it still crashes after the change.

await FlutterInappPurchase.instance.initConnection.catchError((e) {
      Fluttertoast.showToast(
        msg: "Error Connecting Google Play Services, $e",
        textColor: Colors.black,
      );
    });

Again thanks for replying, I think the issue doesn't have any more depth into it so I will close it, will report you if I encounter the error again.

@xPutnikx
Copy link
Contributor

xPutnikx commented Oct 5, 2019

Began getting the same issue after updating the library version to v2.

Fatal Exception: java.lang.IllegalStateException: Reply already submitted
       at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:35)
       at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.error(MethodChannel.java:14)
       at com.dooboolab.flutterinapppurchase.AndroidInappPurchasePlugin$1.a(AndroidInappPurchasePlugin.java:10)
       at com.android.billingclient.api.BillingClientImpl$BillingServiceConnection.onServiceDisconnected(BillingClientImpl.java:28)

I'm not sure if it's related to a missing google play services, but the problem appears on many different devices: Samsungs, LG, Huawei, Motorolas.

Seems like catching errors on the dart side doesn't help here.

@dipinarora9
Copy link
Contributor Author

I think it looks expected. Try to wrap your initConnection code with try and catch statement and see if this is still crashing.

@xPutnikx have you tried the above step and when did you faced this error? Upon initiating connection or on ending connection.

@dipinarora9 dipinarora9 reopened this Oct 6, 2019
@hyochan
Copy link
Owner

hyochan commented Oct 6, 2019

Could you guys try 2.0.0+14 and come back?

@xPutnikx
Copy link
Contributor

xPutnikx commented Oct 8, 2019

@dipinarora9 The problem is I can't reproduce it myself on any of my 5 phones. But i'm getting a about a hundreds crash reports daily.
@hyochan going to try that, thanks.

@xPutnikx
Copy link
Contributor

@hyochan The issue still there.
it's android api returns
at com.android.billingclient.api.BillingClientImpl$BillingServiceConnection.onServiceDisconnected(BillingClientImpl.java) do you think this callback might happens multiple times somehow?

One more thing from crashlitycs which might be helpful:

  1. It mostly happens when app on a background.

Screenshot 2019-10-10 at 13 16 47

@xPutnikx
Copy link
Contributor

I checked how the callback onServiceDisconnected done in the official plugin.
https://github.com/flutter/plugins/blob/master/packages/in_app_purchase/android/src/main/java/io/flutter/plugins/inapppurchase/InAppPurchasePlugin.java
Instead of returning an error to result as a single callback, they post a new value into the channel...

@hyochan hyochan reopened this Oct 12, 2019
hyochan added a commit that referenced this issue Oct 12, 2019
@hyochan
Copy link
Owner

hyochan commented Oct 12, 2019

I've updated this in 2.0.1. Please try this and come back. Thank you!

@hyochan hyochan closed this as completed Oct 12, 2019
@hyochan hyochan mentioned this issue Oct 12, 2019
hyochan added a commit that referenced this issue Oct 12, 2019
@psipaylo
Copy link
Contributor

psipaylo commented Nov 5, 2019

Hey @hyochan

We experience a similar issue in the app running in prod. I wasn't able to reproduce this issue on my own devices but observe hundreds of crash reports coming in google console. Stack trace looks almost identical to those posted above.

Version of flutter_inapp_purchase : 2.0.4

Problem faced on Android devices


java.lang.IllegalStateException: 
  at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply (DartMessenger.java:151)
  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success (MethodChannel.java:225)
  at com.dooboolab.flutterinapppurchase.AndroidInappPurchasePlugin$1.onBillingSetupFinished (AndroidInappPurchasePlugin.java:84)
  at com.android.billingclient.api.BillingClientImpl$BillingServiceConnection$1.run (BillingClientImpl.java:1521)
  at android.os.Handler.handleCallback (Handler.java:795)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loop (Looper.java:166)
  at android.app.ActivityThread.main (ActivityThread.java:6861)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:450)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:936)

Flutter doctor output:

[✓] Flutter (Channel stable, v1.9.1+hotfix.6, on Mac OS X 10.15.1 19B88, locale en-US)
    • Flutter version 1.9.1+hotfix.6
    • Framework revision 68587a0916 (7 weeks ago), 2019-09-13 19:46:58 -0700
    • Engine revision b863200c37
    • Dart version 2.5.0

[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at /Users/pavel/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
    • All Android licenses accepted.

@dipinarora9
Copy link
Contributor Author

Hey there @psipaylo !

StreamSubscription _conectionSubscription;
_conectionSubscription = FlutterInappPurchase.connectionUpdated.listen((connected) {
      print('connected: $connected');
});

Does your production app includes in snippet?

@psipaylo
Copy link
Contributor

psipaylo commented Nov 5, 2019

Hey @dipinarora9
Thanks for following up.

No, there isn't a listener for connection state in our app. And seems like that's the issue, right? On Android, this is crucial to wait for the connection to billing services being established?

@dipinarora9
Copy link
Contributor Author

Yes, it is necessary.

@psipaylo
Copy link
Contributor

psipaylo commented Nov 6, 2019

Thanks for the help.

@dipinarora9 I am curious, do you think having the below statement on the very start of the app might be not enough?

await FlutterInappPurchase.instance.initConnection

We do initialize connection as part of the initial setup, and as far as I can say following the code of Android native part it should do the trick, shouldn't it?

@uzumaki258
Copy link

uzumaki258 commented Nov 13, 2019

Why this issue closed, it still produce on ^2.0.4. I see huge of crashs on firebase dashboard.

@hyochan
Copy link
Owner

hyochan commented Nov 13, 2019

@uzumaki258 Have you ensured the connection as others described?

@dipinarora9
Copy link
Contributor Author

@psipaylo This snippet only initialises the in-app purchase billing services.
Moreover, the plugin flutter_inapp_purchase is made in such a way that all connection updates (like billing services connected, billing services disconnected) are passed to a stream called FlutterInappPurchase.connectionUpdated.

As you said the error that this issue concern was not faced by you but by your users. That's why the plugin recommends using connectionUpdated stream only to take an extra step while the app deals with billing services and allowing you to monitor the connection more thoroughly.

@arkanmgerges
Copy link

Hi, I also have the same error and my code is wrapped inside try..catch. And the errors I can see in google play console.
All the 10 errors are the same

Screenshot_20191202-121747_Play Console

@dipinarora9
Copy link
Contributor Author

Hey there @arkanmgerges !

StreamSubscription _conectionSubscription;
_conectionSubscription = FlutterInappPurchase.connectionUpdated.listen((connected) {
      print('connected: $connected');
});

Does your production app includes this snippet?

And I suppose you are using the latest version of the flutter_inapp_purchase in your app?

@arkanmgerges
Copy link

Hi @dipinarora9,
I'm using flutter_inapp_purchase: ^2.0.5
and I attached in-app purcharse file service.
InAppPurchaseManagerService.dart
A dialog will be called for payment by calling AppDi().inAppPurchaseManagerService().showPaymentDialog(context);
AppDi is an app dependency injection class,
The code inside the AppDi for InAppPurchase is as follows:
For Di I'm using injector: ^1.0.8

// Define
_injector
        .registerSingleton<InAppPurchaseManagerService>((_) => InAppPurchaseManagerService());
// Init service
_injector.getDependency<InAppPurchaseManagerService>();

// Getter
 InAppPurchaseManagerService inAppPurchaseManagerService() => _injector.getDependency<InAppPurchaseManagerService>();

InAppPurchaseManagerService.dart.zip

@arkanmgerges
Copy link

Also I attached an error for today

image

@dipinarora9
Copy link
Contributor Author

Hello again @arkanmgerges ,
I just went through the zip file you provided. The reason why you are getting this error is your code does not have the following snippet.

StreamSubscription _conectionSubscription;
_conectionSubscription = FlutterInappPurchase.connectionUpdated.listen((connected) {
      print('connected: $connected');
});

In your case, just make the following changes and you will be good to go.


StreamSubscription _purchaseUpdatedSubscription;
StreamSubscription _purchaseErrorSubscription;
StreamSubscription _connectionSubscription;     // Declare this variable as well

void _showPaymentDialog(BuildContext context) async {
    await initPaymentConnection();

/*
APPEND THE FOLLOWING LINE
*/
    _connectionSubscription =
        FlutterInappPurchase.connectionUpdated.listen((productItem) async {
      await this.checkSubscription();
    });

    _purchaseUpdatedSubscription =
        FlutterInappPurchase.purchaseUpdated.listen((productItem) async {
      await this.checkSubscription();
    });

    _purchaseErrorSubscription =
        FlutterInappPurchase.purchaseError.listen((purchaseError) async {
      await this.checkSubscription();
    });
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return PaymentWidget();
        }).then((_) async {
      _purchaseUpdatedSubscription.cancel();
      _purchaseUpdatedSubscription = null;
      _purchaseErrorSubscription.cancel();
      _purchaseErrorSubscription = null;
      await closePaymentConnection();
    });
  }

@arkanmgerges
Copy link

Hi @dipinarora9,

Thank you, I've modified the code and now I pushed it into google play console for a new release,
I will let you know if I still have problems,
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android android related issue help wanted Extra attention is needed
Projects
None yet
6 participants