[SOLVED] Android Google Billing Client – onSkuDetailsResponse Always Empty List


This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under
CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

Issue

I was wondering could you help. I followed the instructions at https://developer.android.com/google/play/billing/integrate, but I cannot seem to get the purchase flow working. The billing seems to setup ok, but when I try to query for my in-app products, the list is always returning empty. Can someone please help?

In my app level build.gradle file, I have included the Google Billing SDK:

implementation 'com.android.billingclient:billing:3.0.0'

Then I have created an activity to test out the code. It first initialises the BillingClient and starts the connection. The connection seems to finish the setup correctly. Once setup correctly, I then try to query the products that I have available in my Google Play Console under ‘Store presence’ > ‘In-app products’ > ‘Manage products’

enter image description here

The following is then the code in the Activity that should kick off the process and return the SkuDetails list, but unfortunately it is returning back empty.

private BillingClient billingClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_billing);

    this.billingClient = BillingClient.newBuilder(this)
            .enablePendingPurchases()
            .setListener(this.purchaseUpdateListener)
            .build();
    this.billingClient.startConnection(billingClientStateListener);
}

private PurchasesUpdatedListener purchaseUpdateListener = new PurchasesUpdatedListener() {
    @Override
    public void onPurchasesUpdated(@NonNull BillingResult billingResult, @Nullable List<Purchase> list) {
        Log.d("Billing", "onPurchasesUpdated - List Size: " + list.size());
    }
};

private BillingClientStateListener billingClientStateListener = new BillingClientStateListener() {
    @Override
    public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
        if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
            Log.d("Billing", "onBillingSetupFinished - OK");
            queryProducts();
        } else {
            Log.d("Billing", "onBillingSetupFinished - Something wrong response Code: " + billingResult.getResponseCode());
        }
    }

    @Override
    public void onBillingServiceDisconnected() {
        Log.d("Billing", "Service disconnected");
    }
};

private void queryProducts() {
    List<String> productIdsList = new ArrayList<>();
    productIdsList.add("test.billing.001");

    SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
    params.setSkusList(productIdsList).setType(BillingClient.SkuType.INAPP);
    this.billingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() {
        @Override
        public void onSkuDetailsResponse(@NonNull BillingResult billingResult, @Nullable List< SkuDetails > list) {
            Log.d("Billing", "onSkuDetailsResponse - List Size: " + list.size());
        }
    });
}

Solution

So for anyone who is having similar issues, it seems that (well in my case anyways) that my app needed to be successfully published before I could retrieve the in-app products from the app. Once my app was published, I was then able to query and use the in-app products.

Answered By – Graham Baitson

people found this article helpful. What about you?