Skip to main content

Trivial Drive Java

Introduction

You can use the Android Play Billing Samples project to test IAPs. This guide shows how easy it is to get it working. Note that most of the following steps are needed to bring the samples to be usable with newer versions of Android Studio.

Get the code

Checkout the following repo: https://github.com/android/play-billing-samples

Open the project "TrivialDriveJava" in Android Studio

Update gradle:

Replace the Google Play Billing reference in TrivialDriveJava/app/build.gradle for our library that you can get here https://console.iap.dev

implementation "com.android.billingclient:billing:$version_billing"

for:

releaseImplementation "com.android.billingclient:billing:$version_billing"

debugImplementation "dev.iap.android.billingclient:billing_5_1:1.0.0"

Update the Java code:

Comment out the following block in the file "TrivialDriveJava/app/src/main/java/com/sample/billing/BillingDataSource.java"

/**
* Used internally to get purchases from a requested set of SKUs. This is particularly important
* when changing subscriptions, as onPurchasesUpdated won't update the purchase state of a
* subscription that has been upgraded from.
*
* @param skus skus to get purchase information for
* @param skuType sku type, inapp or subscription, to get purchase information for.
* @return purchases
* // Removed since v4.0.0
*/
// private List<Purchase> getPurchases(String[] skus, String skuType) {
// Purchase.PurchasesResult pr = billingClient.queryPurchases(skuType);
// BillingResult br = pr.getBillingResult();
// List<Purchase> returnPurchasesList = new LinkedList<>();
// if (br.getResponseCode() != BillingClient.BillingResponseCode.OK) {
// Log.e(TAG, "Problem getting purchases: " + br.getDebugMessage());
// } else {
// List<Purchase> purchasesList = pr.getPurchasesList();
// if (null != purchasesList) {
// for (Purchase purchase : purchasesList) {
// for (String sku : skus) {
// for (String purchaseSku : purchase.getSkus()) {
// if (purchaseSku.equals(sku)) {
// if ( !returnPurchasesList.contains(purchase) ) {
// returnPurchasesList.add(purchase);
// }
// }
// }
// }
// }
// }
// }
// return returnPurchasesList;
// }

This method has been deprecated since v4.0.0 In that same file, add the following in line 752:

case 0: // Pull request submitted: https://github.com/android/play-billing-samples/issues/465
br = billingClient.launchBillingFlow(activity,
billingFlowParamsBuilder.build());
if (br.getResponseCode() == BillingClient.BillingResponseCode.OK) {
billingFlowInProcess.postValue(true);
} else {
Log.e(TAG, "Billing failed: + " + br.getDebugMessage());
}

Update the repositories

Replace jcenter() for mavenCentral()

Update the Android Manifest

In the AndroidManifest.xml file,

<activity
android:name="com.sample.android.trivialdrivesample.ui.MainActivity"
android:label="@string/title_game_fragment"
android:exported="true"> <!-- Add this attribute if compiling with newer version of Android -->

Update the Public key

Change the key : base64EncodedPublicKey="null" in local.properties to the value you can download from the console under: /organizations/{your org}/projects/{your project}/sdk-setup make sure you use the Public key. Use the Copy to clipboard icon which will give you the value with no line breaks and no header and footer which is the expected way

Initialize the iap.dev SDK

In the BillingDataSource's constructor just before the BillingClient is being constructed.

private BillingDataSource(@NonNull Application application, String[] knownInappSKUs,
String[] knownSubscriptionSKUs, String[] autoConsumeSKUs) {
//...
// Insert this:
IapDev.setup(new IapDevConfiguration.Builder(BuildConfig.IAP_DEV_CLIENT_KEY, BuildConfig.IAP_DEV_CLIENT_SECRET)
//Optional
.authToken(BuildConfig.IAP_DEV_AUTH_TOKEN)
.refreshToken(BuildConfig.IAP_DEV_AUTH_REFRESH_TOKEN)
.build(), application);
// Just before this:
billingClient = IapDevBillingClientBuilder.newBuilder(application)

// ...
}