我有一个现有的Google Play Android应用程序使用Google Billing Library,我想使用最近发布的实现兼容API的Amazon Appstore Billing Compatibility SDK.

他们的指南建议如下替换Java/Kotlin行,第一次使用时效果很好:

import com.android.billingclient.api.*

使用

import com.amazon.device.iap.billingclient.api.*

我已经使用Gradle flavor机制略微调整了构建(例如,不同的store 和应用程序支持URL),所以在理想情况下,我可以利用它.也就是说,我的app/build.gradle档案里有这样一条:

flavorDimensions = ["appstore"]
productFlavors {
    google {
        dimension "appstore"
        buildConfigField "String", "ENCRYPTED_GOOGLE_IAP_KEY", "<removed>"
    }
    amazon {
        dimension "appstore"
        buildConfigField "String", "ENCRYPTED_GOOGLE_IAP_KEY", "ignored"
    }
}

并且我已经在相同的build.gradle中配置了依赖项,以便只有适当的库可用:

dependencies {
    ...

    googleImplementation 'com.android.billingclient:billing:6.1.0'
    amazonImplementation files('libs/appstore-billing-compatibility-4.1.0.jar')
}

So, what I'd really like to be able to do is to conditionally import the appropriate dependency, i.e. 使用 a preprocessor, it would be something like this:

#if defined(amazon)
import com.android.billingclient.api.*
#endif
#if defined(google)
import com.amazon.device.iap.billingclient.api.*
#endif

当然,这不是一个Java/Kotlin兼容的方法. 我可以将现有的实现复制到两个文件夹中,并只调整导入行,即:

src/
|-- google/
|   |-- java/
|       |-- com/
|           |-- example/
|               |-- MyPurchaseCode.kt
|-- amazon/
|   |-- java/
|       |-- com/
|           |-- example/
|               |-- MyPurchaseCode.kt

但是代码除了这一行之外都是相同的,所以维护起来会很麻烦(and, after all, isn't the point of the Amazon compatibility library that I can keep my existing code:))

Any suggestions on how I might do this? I wondered about creating some sort of "wrapper" interface and then having 2 source files, located as above, that implement it. But they're big 使用 many classes and my understanding is that I'd have to clone the declarations.

也许我想得太多了,有一些简单的答案!(I admit my Java / Kotlin knowledge has faded …)

提前感谢!

推荐答案

例如,您可以定义在主MyPurchaseCode.kt文件中使用的对象的类型别名.将有两个具有这种类型别名的Kotlin文件,一个用于google风格,另一个用于amazon风格.

让我们以Purchase对象为例:google的类型别名定义应该位于这里google/java/com/example/,看起来像这样:

package com.example
import com.android.billingclient.api.Purchase
typealias Purchase = Purchase

amazon的那个应该位于amazon/java/com/example/下面,看起来像:

package com.example
import com.amazon.device.iap.billingclient.api.Purchase

typealias Purchase = Purchase

现在,您可以为所有其他公共对象添加额外的typealias.

你的MyPurchaseCode.kt可以在main/src/java/com/example下面.

希望这有帮助

Response章:太棒了,谢谢! (TIL Kotlin's 102).最后的解决方案有一个问题,但效果足够好. 第typealias章:不总是?允许每个this report的子类型为referenced here,所以对于Amazon文件,我最终得到:

typealias AcknowledgePurchaseParams = com.android.billingclient.api.AcknowledgePurchaseParams
typealias AcknowledgePurchaseResponseListener = com.android.billingclient.api.AcknowledgePurchaseResponseListener
typealias BillingClient = com.android.billingclient.api.BillingClient
typealias BillingClientStateListener = com.android.billingclient.api.BillingClientStateListener
typealias BillingFlowParams = com.android.billingclient.api.BillingFlowParams
typealias BillingResult = com.android.billingclient.api.BillingResult
typealias BillingResponseCode = com.android.billingclient.api.BillingClient.BillingResponseCode
typealias QueryPurchasesParams = com.android.billingclient.api.QueryPurchasesParams
typealias QueryProductDetailsParams = com.android.billingclient.api.QueryProductDetailsParams
typealias Product = com.android.billingclient.api.QueryProductDetailsParams.Product
typealias ProductDetails = com.android.billingclient.api.ProductDetails
typealias ProductDetailsParams = com.android.billingclient.api.BillingFlowParams.ProductDetailsParams
typealias ProductDetailsResponseListener = com.android.billingclient.api.ProductDetailsResponseListener
typealias ProductType = com.android.billingclient.api.BillingClient.ProductType
typealias Purchase = com.android.billingclient.api.Purchase
typealias PurchasesResponseListener = com.android.billingclient.api.PurchasesResponseListener
typealias PurchaseState = com.android.billingclient.api.Purchase.PurchaseState
typealias PurchasesUpdatedListener = com.android.billingclient.api.PurchasesUpdatedListener

Google也是如此,即:

typealias AcknowledgePurchaseParams = com.android.billingclient.api.AcknowledgePurchaseParams
typealias AcknowledgePurchaseResponseListener = com.android.billingclient.api.AcknowledgePurchaseResponseListener
typealias BillingClient = com.android.billingclient.api.BillingClient

// etc

trap 如下所示,其中需要显式对子类型进行别名:

typealias BillingClient = com.android.billingclient.api.BillingClient
typealias BillingResponseCode = com.android.billingclient.api.BillingClient.BillingResponseCode

这意味着我的代码在main必须改变:

BillingClient.BillingResponseCode.OK

致:

BillingResponseCode.OK

这可能不太清楚但我可以接受

Android相关问答推荐

使用不同的Google帐户登录

Android编写动画在发布版本中崩溃

从未设置实时数据值

在Jetpack Compose中从LazyColumn中删除项目时发生IndexOutOf边界异常

为什么柱子的高度不都一样?

为什么我有多个Player实例?

Jetpack Compose:带芯片的Textfield

解决失败:Landroidx/compose/runtime/PrimitiveSnapshotStateKt

升级到 Jetpack Compose 物料 list 2023.08.00 需要我将 targetSdk 更改为 34

在一个函数中组合相同的流 struct

组成不重叠的元素

如何使用 Jetpack Compose 制作两个圆圈

观察软键盘可见性,打开/关闭 Jetpack Compose

缺少类 com.google.android.datatransport.runtime.ForcedSender

喷气背包组成影子奇怪的行为

如何在 Compose 中更改高程 colored颜色 ?

Jetpack 使用 Canvas 组成半圆

Android:在模块 jetified-play-services-measurement 和 jetified-play-services-measurement-impl 中发现重复类

compose :为什么以记住启动的列表触发方式与快照不同

在 compose 屏幕之间传递 uri 会导致:SecurityException: Permission Denial