Android SDK

Guide for integrating Purchasely SDK inside your Android app

Installation

Maven

Add our maven repository url to your project build.gradle file

allprojects {
repositories {
google()
jcenter()
maven { url "https://dl.bintray.com/purchasely/Purchasely" } //Add this line
}
}

Add the sdk to your dependencies

implementation 'io.purchasely:purchasely:1.1.0'

Initialising the SDK

Iniatialize sdk when you want to have access to products, purchases or restoration. We advise you to do it as soon as your application start, our sdk is very light and all initialization process is done on another thread.

You will need an API Key that you can find in your application settings in the Purchasely Console.

Kotlin
Kotlin
import io.purchasely.ext.Purchasely
Purchasely.start(applicationContext, "API_KEY", "USER_ID", eventListener, uiListener)

The userId parameter is optional and allows you to associate the purchase to a user instead of a device (see next)

The eventListener parameter is optional and allows you to listen to all purchase events. You should implement it at least to know when the purchase is successfull.

The uiListener parameter is optional and allows you to override UI dialog presented to user in case of error or success.

Setting-up the User Id

Once your user is logged in and you can send us a userId, please do it otherwise the purchase will be tied to the device and your user won't be able to enjoy from another device. Setting it will allow you to tie a purchase to a user to use it on other devices.

Kotlin
Java
Kotlin
Purchasely.userId = "123456789"
Java
Purchasey.setUserId("123456789");

To remove the user (logged out) you can perform a :

Kotlin
Java
Kotlin
Purchasely.userId = null
Java
Purchasey.setUserId("null");

Presenting products

Purchasely handles all the presentation logic of your products configured in the back office. You can ask for the SDK to give you the androidx.fragment.app.Fragment presenting the purchase by calling the following :

Kotlin
Java
Kotlin
Purchasely.displayProduct(
productId = /*Your Product id*/ "",
presentationId = "default" //change to set the presentation you want to display
success = { fragment ->
supportFragmentManager.beginTransaction()
.addToBackStack(null)
.replace(R.id.inappFragment, fragment, "InAppFragment")
.commitAllowingStateLoss()
progressBar.isVisible = false
},
failure = { error ->
Log.e("Product", "Error", error)
Snackbar.make(window.decorView, error.message ?: "error", Snackbar.LENGTH_SHORT).show()
}
)
Java
Purchasely.displayProduct("YOUR_PRODUCT_ID", "default", new DisplayProductListener() {
@Override
public void onFailure(@NotNull Throwable throwable) {
Log.e("Product", "Error", throwable);
String message = "error";
if(throwable.getMessage() != null) {
message = throwable.getMessage();
}
Snackbar.make(getWindow().getDecorView(), message, Snackbar.LENGTH_SHORT).show();
}
@Override
public void onSuccess(@NotNull Fragment fragment) {
getSupportFragmentManager().beginTransaction()
.addToBackStack(null)
.replace(R.id.inappFragment, fragment, "InAppFragment")
.commitAllowingStateLoss();
findViewById(R.id.progressBar).setVisibility(View.GONE);
}
});

A coroutine version also exists suspend fun displayProduct(productId: String, presentationId: String) : Fragment?

You can place the fragment wherever your want in another fragment or activity.

You can be alerted if the purchase was made by listening to the LiveData livePurchase()