In-App billing with BillingClient. ResponceCode = -1. Service connection is disconnected

Related searches

In my application, a donation function was made. (The application with permission to purchase inside the application is available in google play and in the downloaded version, this function works) I did it using the easy library from anjlab. I started to update the application, and rewrote it at the same time on Kotlin. Donat stopped working for some reason. I decided to redo the code from the anjlab library to the new billing library from google. When I try to make a purchase, I get

responseCode = -1 (service connection is disconnected).

I tried to resume the connection using startConnection (this), tried to change the version in the gradle, loaded the new version into the library apk. Neither the old nor the new does not work (if you download from google play, the one that is published, it works). Code wrote on the guideline from Google. I test the application signed on a real device

SettingsFramgent.kt (only sacrament code)

package *

import android.content.Intent
import androidx.preference.PreferenceFragmentCompat
import android.os.Bundle
import android.widget.Toast
import androidx.preference.Preference

import com.starikov.datecalc.R

class SettingsFragment : PreferenceFragmentCompat(), SettingsContract.View {

    private lateinit var presenter: SettingsContract.Presenter

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.preferences, rootKey)

        presenter = SettingsPresenter(activity!!)
        presenter.attachView(this)

val makeDonatePref: Preference = findPreference(MAKE_DONATE_KEY)!!

        makeDonatePref.setOnPreferenceClickListener { presenter.makeDonateClicked(); true }

        presenter.viewIsReady()
    }

    override fun showToast(resId: Int) {
        Toast.makeText(activity, resId, Toast.LENGTH_LONG).show()
    }

    override fun startActivity(intent: Intent) {
        activity!!.startActivity(intent)
    }

    override fun onDestroy() {
        super.onDestroy()
        presenter.detachView()
        if (activity!!.isFinishing) {
            presenter.destroy()
        }
    }

    companion object {
        private const val MAKE_DONATE_KEY = "make_donate"
    }
}

SettingsPresenter.kt

package *

import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import com.android.billingclient.api.*

import com.starikov.datecalc.common.PresenterBase
import com.android.billingclient.api.BillingClient
import com.android.billingclient.api.SkuDetailsParams
import com.android.billingclient.api.BillingFlowParams
import com.starikov.datecalc.R
import java.util.*


internal class SettingsPresenter internal constructor(private val activity: Activity)
    : PresenterBase<SettingsContract.View>(), SettingsContract.Presenter {
    private lateinit var billingClient: BillingClient
    private var skuDetailsMap: HashMap<String, SkuDetails> = HashMap()

    override fun makeDonateClicked() {
        initBillingProcessor()
        launchBilling(DONATE_PRODUCT_ID)
    }

    override fun destroy() {
        billingClient.endConnection()
    }

    override fun viewIsReady() {}

    private fun initBillingProcessor() {
        billingClient = BillingClient.newBuilder(activity)
                .enablePendingPurchases()
                .setListener {
            billingResult, purchases ->
            if (billingResult?.responseCode == BillingClient.BillingResponseCode.OK && purchases != null) {
                //purchase done
                view!!.showToast(R.string.thanks_for_donate)
            }
        }.build()

        billingClient.startConnection(object : BillingClientStateListener {
            override fun onBillingSetupFinished(billingResult: BillingResult?) {
                if (billingResult?.responseCode == BillingClient.BillingResponseCode.OK) {
                     // **place of logs**
                    querySkuDetails() 
                }
            }

            override fun onBillingServiceDisconnected() {
                //if wrong
            }
        })
    }

    private fun launchBilling(skuId: String) {
        val billingFlowParams = BillingFlowParams.newBuilder()
                .setSkuDetails(skuDetailsMap[skuId])
                .build()
        billingClient.launchBillingFlow(activity, billingFlowParams)
    }

    private fun querySkuDetails() {
        val skuDetailsParamsBuilder = SkuDetailsParams.newBuilder()
        val skuList = ArrayList<String>()
        skuList.add(DONATE_PRODUCT_ID)
        skuDetailsParamsBuilder.setSkusList(skuList).setType(BillingClient.SkuType.INAPP)
        billingClient.querySkuDetailsAsync(skuDetailsParamsBuilder.build()) { billingResult, skuDetailsList ->
            if (billingResult?.responseCode ==  BillingClient.BillingResponseCode.OK) {
                for (skuDetails in skuDetailsList!!) {
                    skuDetailsMap[skuDetails.sku] = skuDetails
                }
            }
        }
    }

    companion object {
        private const val DONATE_PRODUCT_ID = "donate"
    }
}

In "place of logs", i checked billingResult.responseCode = -1; debugMessage = "Service connection is disconnected"

I have no idea what to do. Thanks in advance for your help


Not sure if you are still having this problem, but I had it and debugging showed that the billing client was still using some old AIDL stuff I had not yet removed in the old com.android.vending package. Once I removed all that code and gutted my app's support for the old stuff, I was able to proceed with the purchase without error.

Android In-App Billing — Xamarin Community Forums, InAppBilling component to make in-app purchases. Ensure that the connection to the Google Play Services API hasn't WriteLine("BILLING | onBillingServiceDisconnected RESULT: $billingResponseCode"); } } // Response code 7 in QueryPurchases(BillingClient.SkuType.Inapp). PurchasesList� int SERVICE_DISCONNECTED. Play Store service is not connected now - potentially transient state. E.g. Play Store could have been updated in the background while your app was still running. So feel free to introduce your retry policy for such use case.


In my case I was using a lazy initialization.

That way, the Google Billing setup started immediately before the launchBillingFlow method call.

That caused the service to be disconnected and not ready yet for the precise moment the billing flow was started.

I now start the BillingClient connection since the activity is started, and the flow works as expected when requested by the user.

Synchronous billing client query in Google play, The setup of BillingClient is asynchronous (in Android and Google Play). " Billing client disconnected") Log.v(tag, "onBillingServiceDisconnected() INAPP ) billingClient.querySkuDetailsAsync(params.build()) { responseCode, skuDetailsList I want to connect multiple Android devices via Network Service Discovery. We want to help all developers to implement in-app billing easily. For C/C++ you can use the AIDL interface to communicate with the Google Play service, similar to what you can do for any service. For all methods on AIDL service, please check In-app Billing Reference. If you want to see the AIDL file, please check IInAppBillingService.aidl.


Faced such a problem. my decision is adding into application xml node

<!-- GOOGLE PLAY BILLING -->
    <activity
        android:name="com.android.billingclient.api.ProxyBillingActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />

Android, private BillingClient mBillingClient; . @Override public void onBillingServiceDisconnected() { } }); INAPP) for (purchase in purchases. firstable I recommend you provide more information about you issue (code, version billing library and type of billing, it can be in-app or subscription). Secondly, if you are using Google's library v3, probably you are not calling the correct methods.


queryPurchases() method doesn't show that android.test.purchased , onCreate(icicle); mBillingClient = BillingClient. INAPP); mBillingClient. @ Override public void onSkuDetailsResponse(int responseCode, List<SkuDetails> public void onBillingServiceDisconnected() { } }); checkPurchase(); By clicking “Post Your Answer”, you agree to our terms of service, privacy� Note the distinction between BillingClient.endConnection and disconnected: Disconnected means it's okay to try reconnecting. endConnection means that the current Google Play BillingClient is now invalid. To reconnect, you must re-instantiate the BillingClient and then start a new connection.


App monetisation and In-App Billing in Android., Android apps and monetisation. App monetisation and In-App Billing in Android. to charge your users with a subscription model i.e. something a media service would charge its Now, we're Ok to go ahead and connect up our mBillingClient , we've responseCode) { if (responseCode == BillingClient. Stage 1 – Implement Play Billing & Connect to Google Play Service. add implementation ‘com.android.billingclient:billing:1.0’ to “build.gradle(Module : app)” and click ‘Sync Now’ Define the Main Layout, activity_main.xml


Make sure you maintain the BillingClient connection when executing any methods. Show products available to buy. After you have established a connection to Google Play, you are ready to query for your available products and display them to your users. To query Google Play for in-app product details, call querySkuDetailsAsync(). Querying for SKU