Android SDK (beta)

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:[email protected]"
...
}

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;

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

Java
Kotlin
Java
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Qonversion.initialize(this, "projectKey", "yourSideUserID");
}
}
Kotlin
public class App : Application {
override fun onCreate() {
super.onCreate();
Qonversion.initialize(this, "projectKey", "yourSideUserID");
}
}

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 method purchase is after the method onPurchasesUpdated of BillingClient is called.

For more information, check the example app in the repo ManualTrackingActivity and ManualTrackingActivityKt classes.

Java
Kotlin
Java
@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);
}
Kotlin
override fun onCreate(
savedInstanceState: Bundle?,
persistentState: PersistableBundle?
) {
super.onCreate(savedInstanceState, persistentState)
client = BillingClient
.newBuilder(this)
.enablePendingPurchases()
.setListener { billingResult, list ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
if (list != null && list.isNotEmpty()) {
trackPurchase(
skuDetails[SKU_ID]!!,
list[0]
)
}
}
}
.build()
launchBilling()
}
private fun trackPurchase(
details: SkuDetails,
purchase: Purchase
) {
Qonversion.instance!!.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):

Java
Kotlin
Java
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
}
});
}
Kotlin
private fun buildBilling(): QonversionBillingBuilder {
return QonversionBillingBuilder()
.enablePendingPurchases()
.setChildDirected(BillingClient.ChildDirected.CHILD_DIRECTED)
.setUnderAgeOfConsent(BillingClient.UnderAgeOfConsent.UNSPECIFIED)
.setListener { billingResult, 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

Java
Kotlin
Java
public class App extends Application {
​
@Override
public void onCreate() {
super.onCreate();
​
QonversionBillingBuilder billingBuilder = buildBilling();
Qonversion.initialize(this, BuildConfig.QONVERSION_API_KEY, "yourSideUserID", billingBuilder, true);
}
Kotlin
class App : Application() {
​
override fun onCreate() {
super.onCreate()
val 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

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

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

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

14. You are almost done! Now you need go to your Qonversion account and past the credentials had downloaded on the step 12.

Note

It takes up to 24 hours for your Google service credentials to work properly with the Android Developer API.

90% Done!

From now on, Qonversion will track subscription events independent from a user's device (e.g., if the app was not opened or even deleted).

Next Steps