作者 / Caren Chang, android Engineer
Google Play Billing 系列內容是專門為中文開發者開闢的系列分享,著重講解中國開發者對 Play Billing 最容易感到疑惑的地方。如果您有任何問題,也歡迎在留言區提出,我們會收集大家的反饋並在後續文章中做出解答。
銷售數字內容是許多 Android 應用的主要營收渠道。具體形式包括銷售應用內的特定商品 (如遊戲金幣) 以及訂閱計劃 (比如允許用戶在限定時間內訪問高級功能)。google Play Billing 作為一個數字內容銷售的工具和服務的集合,可以幫助開發者在 Android 應用中銷售線上商品。
本文將從基礎知識開始,帶大家逐步深入,詳細了解 Google Play Billing 3,及其用例和最佳實踐。
首先,我們來熟悉一下 Google Play Billing 的一些關鍵組件。
Google Play 管理中心https://developer.android.google.cn/distribute/consoleGoogle Play Billing Libraryhttps://developer.android.google.cn/google/play/billing/billing_library_overviewGoogle Play Developer APIhttps://developers.google.cn/android-publisher
了解了 Google Play Billing 的關鍵組件之後,我們將從頭介紹如何設置環境並開始在 Android 應用中銷售商品。
1. 設置 Android 應用以使用 Google Play Billing 開發庫
第一步,也是最重要的一步,是設置 Android 應用以使用 Google Play Billing 開發庫。
向 app/build.gradle 文件中添加以下依賴關係,在應用中實現 Google Play Billing:
implementation 『com.android.billingclient:billing:3.0.0』
添加庫依賴關係後,為應用構建一個發布版 APK,並將其上傳到 Google Play 管理中心。
2. 添加應用內產品
上傳 APK 後,可以使用 Google Play 管理中心開始添加要在應用中銷售的應用內產品。在 &34; 下,有一個設置應用內產品的部分。在這裡可以設置兩種類型的商品:
創建新的託管產品和訂閱時,需要輸入商品的產品 ID (Product ID) 或 SKU。這個產品 ID 後續將在應用代碼中使用,我們稍後會講到。在創建託管產品之前,應慎重規劃產品 ID。產品 ID 在應用中必須唯一,並且在創建後無法更改或重複使用。
為了使測試更快、更簡單,您可以將您的 Google 帳號添加到 Google Play 開發者帳號的 &34; 中。這樣,只要軟體包名稱與 Play Store 中的 APK 匹配,就可以使用調試版本和調試籤名進行測試。
將 Google 帳號添加到 Google Play 開發者帳號的 &34; 中https://developer.android.google.cn/google/play/billing/billing_testing34;Billing client successfully set up&34;Billing service disconnected&34;coins_5&39;managed products&34;onSkuDetailsResponse ${result?.responseCode}&34;No skus found from query&startconnection
在實際環境中連接是有可能中斷的,所以您的應用還必須重寫 onBillingServiceDisconnected() 回調來處理重新連接,確保應用在發出任何進一步請求之前已與 Google Play 連接。
onBillingServiceDisconnected()https://developer.android.google.cn/reference/com/android/billingclient/api/BillingClientStateListeneronBillingServiceDisconnected()
2. 獲取用戶的既有購買記錄 - 成功設置 BillingClient 後,您的應用現在可以調用queryPurchases() 來查詢用戶先前的購買記錄。
/** * Query Google Play Billing for existing purchases. * * New purchases will be provided to PurchasesUpdatedListener. */fun queryPurchases() { if (!billingClient.isReady) { Log.e(TAG, &34;) } // Query for existing in app products that have been purchased. This does NOT include subscriptions. val result = billingClient.queryPurchases(BillingClient.SkuType.INAPP) if (result.purchasesList == null) { Log.i(TAG, &34;) } else { Log.i(TAG, &34;) }}
3. 呈現待售產品 - 在本文的前半部分我們談到了如何在 Google Play 管理中心中設置產品以及如何在應用中查詢這些產品。在調用 querySkuDetailsAsync() 獲取每個產品的 SkuDetails 後,即可使用這些信息設置對應的界面。
private fun queryOneTimeProducts() { val skuListToQuery = ArrayList<String>() // sku refers to the product ID that was set in the Play Console skuListToQuery.add(&34;) val params = SkuDetailsParams.newBuilder() params .setSkusList(skuListToQuery) .setType(BillingClient.SkuType.INAPP) // SkuType.INAPP refers to &39; or one time purchases // To query for subscription products, you would use SkuType.SUBS billingClient.querySkuDetailsAsync( params.build(), object : SkuDetailsResponseListener { override fun onSkuDetailsResponse( result: BillingResult, skuDetails: MutableList<SkuDetails>? ) { if (skuDetails != null) { // Store sku and skuDetail to be used later } else { Log.i(TAG, &34;) } } })}
querySkuDetailsAsync()https://developer.android.google.cn/reference/com/android/billingclient/api/BillingClient34;launchPurchaseFlow result ${responseCode}&launchbillingflow
5. 處理購買結果 - 在用戶退出 Google Play 購買界面時 (點擊 &34; 按鈕完成購買,或者點擊 &34; 按鈕取消購買),onPurchaseUpdated() 回調會將購買流程的結果發送回您的應用。然後,根據 BillingResult.responseCode 即可確定用戶是否成功購買產品。如果 responseCode == OK,則表示購買已成功完成。
https://developer.android.google.cn/reference/com/android/billingclient/api/PurchasesUpdatedListener34;User cancelled purchase flow.&34;onPurchaseUpdated error: ${billingResult?.responseCode}&verify
在對購買進行驗證之後,您還需要對其進行確認。
fun handlePurchase(purchase: Purchase) { // If your app has a server component, first verify the purchase by checking that the // purchaseToken hasn&acknowledgepurchaseconsumeAsync()https://developer.android.google.cn/reference/com/android/billingclient/api/BillingClient#consumeasync
7. 授予用戶產品 - 完成上述步驟後,您的應用就可以向用戶授予他們購買的應用內產品了!
如果您想查看 Google Play Billing 開發庫的資源,可以在此處訪問官方文檔。我們還提供了一些示例,演示了實現 Billing 庫的最佳實踐。本文中的代碼示例可以在 GitHub 上獲取。
官方文檔: Google Play Billing 服務概覽https://developer.android.google.cn/google/play/billing/billing_overviewPlay Billing 開發庫示例https://github.com/android/play-billing-samples本文中的代碼示例http://github.com/calren
如果您的應用目前尚未使用 Play Billing Library 3,務必查看我們的遷移指南,將您的應用遷移到最新的 Play Billing Library。
從 AIDL 遷移到 Google Play Billing 開發庫的遷移指南https://developer.android.google.cn/google/play/billing/migrate
文章轉載自:谷歌開發者公眾號原創文章