Android SDK

Guide for installing the Qonversion SDK for Android

Install the library

The recommended way to install the Qonversion library for Android is with a build system like Gradle. The library is distributed via Maven Central.

Add Qonversion library to the dependencies section in your app build.gradle

dependencies {    
     ...
     implementation 'com.qonversion.android.sdk:sdk:1.0.6'    
     ...
}

Setup

Qonversion SDK can work in two modes depending on your settings:

  1. Manual tracking mode.

    In this mode, you need to call SDK methods manually to assign purchases to Qonversion.

  2. Auto-tracking mode.

    In this mode, you do not have to worry about assigning purchases to Qonversion. Everything is handled inside the SDK.

See the steps below for manual or auto-tracking modes.

1. Manual mode

1.1. Initializing Qonversion SDK

To import the Qonversion SDK, add the following code:

import com.qonversion.android.sdk.Qonversion;

2. Get your application key and configure Qonversion in your app. To get the key, navigate to the settings in your Qonversion account, and copy the Application Access Key:

The SDK initialization has to be called in your Application in the onCreate method.

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Qonversion.initialize(this, "ApplicationKey", "yourSideUserID");
    }
}

Where yourSideUserID is user id in your backend. If you don't have that, just create UUID using UUID.randomUUID().toString() and save locally.

1.2. Set up Google Play Billing Library.

You need to set up and initialize Google Play Billing Library, see official Android Developer documentation here.

To track purchase data with the SDK, you need to call the method purchase. This method takes two parameters. These parameters are objects from Google Play Billing Library:

  • Details with type SkuDetails docs.

  • Purchase with type Purchase docs.

The best time to call purchase method is after the onPurchasesUpdated method of BillingClient is called.

@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
     client = BillingClient
              .newBuilder(this)
              .enablePendingPurchases()
              .setListener(new PurchasesUpdatedListener() {
                    @Override
                    public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> list) {
                         if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                              if (list != null && !list.isEmpty()) {
                                   trackPurchase(skuDetails.get(SKU_ID), list.get(0));
                              }
                         }
                    }                
               })
               .build();
         
}

private void trackPurchase(@NonNull SkuDetails details, @NonNull Purchase purchase) {
    Qonversion.getInstance().purchase(details, purchase);
}

2. Auto Tracking mode

2.1 Initializing Qonversion SDK

The SDK initialization should be called in your Application in the onCreate method following the next steps:

Step 1. Add Qonversion SDK import:

import com.qonversion.android.sdk.Qonversion;

Step 2. Initialize QonversionBillingBuilder

Create an instance of QonversionBillingBuilder(precisely like in Android BillingClient):

private QonversionBillingBuilder buildBilling() {
        return new QonversionBillingBuilder()
                .enablePendingPurchases()
                .setChildDirected(BillingClient.ChildDirected.CHILD_DIRECTED)
                .setUnderAgeOfConsent(BillingClient.UnderAgeOfConsent.UNDER_AGE_OF_CONSENT)
                .setListener(new PurchasesUpdatedListener() {
                    @Override
                    public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) {
                        // your purchases update logic
                    }
                });
    }

Step 3. Initializing Qonversion SDK.

To enable the auto-tracking mode put these parameters to the Qonversion initialize method:

  1. ApplicationContext

  2. Your Qonversion API key.

  3. Your side UserID

  4. Instance of QonversionBillingBuilder from the Step 2.

  5. Auto-tracking - Boolean parameter put it to TRUE

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        QonversionBillingBuilder billingBuilder = buildBilling();
        Qonversion.initialize(this, BuildConfig.QONVERSION_API_KEY, "yourSideUserID", billingBuilder, true);
    }

2.2. Use Qonversion Billing instead of Google BillingClient

For further work with SDK in the auto-tracking mode, you need to use the Qonversion Billing instance. It is similar to the interface and methods of Google BillingClient.

Just call Qonversion.instance?.billingClient and use the Qonversion Billing in the same way as the Google BillingClient. Below is the simplified example of usage:

For more information please see example app in this repo MainActivity

class MainActivity : AppCompatActivity() {

    private var billingClient : Billing? = null
    private val skuDetailsMap = mutableMapOf<String, SkuDetails>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        billingClient = Qonversion.instance?.billingClient
        billingClient?.startConnection(object : BillingClientStateListener {
            override fun onBillingServiceDisconnected() {
                // billing connection failed
            }

            override fun onBillingSetupFinished(billingResult: BillingResult?) {
                if (billingResult?.responseCode == BillingClient.BillingResponseCode.OK) {
                    launchBilling("your_purchase_id", "sku_type")
                }
            }
        })
    }

    private fun launchBilling(purchaseId: String, type: String) {
        var params = SkuDetailsParams.newBuilder()
            .setType(type)
            .setSkusList(listOf(purchaseId))
            .build()

        billingClient?.querySkuDetailsAsync(params, object: SkuDetailsResponseListener {
            override fun onSkuDetailsResponse(
                billingResult: BillingResult?,
                skuDetailsList: MutableList<SkuDetails>?
            ) {
                if (billingResult!!.responseCode == BillingClient.BillingResponseCode.OK) {
                    for (skuDetails in skuDetailsList!!) {
                         skuDetailsMap[skuDetails.sku] = skuDetails
                    }
                    launchBillingFlow(purchaseId)
                }
            }
        })
    }

    private fun launchBillingFlow(purchaseId: String) {
        val billingFlowParams = BillingFlowParams.newBuilder()
            .setSkuDetails(skuDetailsMap[purchaseId])
            .build()
        billingClient?.launchBillingFlow(this, billingFlowParams)
    }
}

License - Qonversion SDK is available under the MIT license.

Provide Credentials

Get JSON from Google Play Console

1. Go to Google Play Developer Console by clicking this link: https://play.google.com/apps/publish/ and login using your Google Developer Account credentials

2. Click the Settings icon in the left menu bar

3. Select API Access

4. Press the Create new project button - see the screenshot below

If you don't see the button, go directly to step 5

5. Select Link to connect your Play account to a Google Play Android Developer Project

6. Scroll to the bottom of the page and click Create Service Account

7. Click the link "Google API Console

8. Сlick the +CREATE SERVICE ACCOUNT button at the top of the page

9. Set the Service account name

10. In the Role dropdown choose Owner and click the Continue button

11. Click the Create key button

12. And download your JSON file clicking the Create button

13.Go back to your Google Console Account and click Grant Access

14. Set the role to Finance and add Manage orders, save it and copy

Configure Qonversion

You are almost done! Navigate to your Qonversion account and pass the credentials that you had downloaded on step 12.

Note

Next Steps

Last updated

Was this helpful?