【问题标题】:Using flavors for publishing an app on AppGallery and Google Play使用风味在 AppGallery 和 Google Play 上发布应用
【发布时间】:2021-01-23 15:27:55
【问题描述】:

我想弄清楚如何在 AppGallery 和 Google Play 上发布我的应用程序。 (我的应用目前在 Google Play 上可用)

我研究了三个小时,最好的选择似乎是使用口味。因为我想为不同的商店使用相同的代码库。为此,我决定添加如下风味:

productFlavors {
    gms {
        dimension "services"
        buildConfigField "String", "SERVICE_USED", '"g"'

    }
    hms {
        dimension "services"
        buildConfigField "String", "SERVICE_USED", '"h"'
    }
}
gmsImplementation 'com.google.firebase:firebase-analytics:17.2.0'
gmsImplementation 'com.google.firebase:firebase-messaging:20.0.0'
gmsImplementation 'com.google.firebase:firebase-ads:18.2.0'
gmsImplementation 'com.google.firebase:firebase-crashlytics:17.2.2'
hmsImplementation 'com.huawei.hms:ads-installreferrer:3.4.34.301'
hmsImplementation 'com.huawei.hms:ads-identifier:3.4.34.301'
hmsImplementation 'com.huawei.hms:hianalytics:5.0.5.301'
hmsImplementation 'com.huawei.hms:iap:5.0.4.301'
hmsImplementation 'com.huawei.hms:push:5.0.4.302'

现在我有一个问题:

如果我错了,请纠正我,我需要为公共服务使用抽象层,对吗?例如,如果我需要使用 IAP,我需要一个包装类来决定使用 GMS 或 HMS,这取决于设备类型。如果我采用这种方式,我需要这样的东西:

  1. 需要一个接口,需要在HmsIAP和GmsIAP类上实现requestProduct方法、purchaseMethod等常用方法。等等

  2. 创建这些类并管理要使用的类的父类。这将称为“AppIAP”类。逻辑层将使用该类进行不依赖于平台的 IAP 操作。

这种方法对我来说很有意义,两个平台将有一个代码库。它看起来很干净,将来易于管理。但问题是我为平台依赖项添加了风味。因此,如果我尝试构建代码的 hms 变体,它将无法编译,因为 Gms 库将丢失。尽管我使用的是 Hms 变体,但我仍然需要构建一个 GmsIAP 类。

为了解决我可以尝试不使用这些风格的问题,通过这种方法,我需要将我的应用程序与两个平台的库一起打包,并且我的应用程序可以正常编译。但正如以下链接所示,由于 Hms 库,Google Play 将拒绝我的应用。

Is Huawei HMS tolerated on Google Play Store?

我该如何解决?

【问题讨论】:

  • 请问您是如何处理服务插件的?你把你的服务 json 文件放在哪里了
  • 我刚刚创建了两个代码库:(

标签: android google-play google-play-services huawei-mobile-services


【解决方案1】:

我认为最可维护的解决方案是使用 different source sets for different flavors 的依赖注入。您可以将您的 GMS 依赖代码放在 src/gms/java 中,将您的 HMS 依赖代码放在 src/hms/java 中。只会编译选定的产品风味源集。 Hilt 的基本示例如下所示:

在你的主要资源集中,你将拥有

src/main/java/your/package/AppMobileServices.kt

interface AppMobileServices {
    val isAvailable: Boolean
}

那么对于 GMS 源集

src/gms/java/your/package/GmsModule.kt:

@Module
@InstallIn(SingletonComponent::class)
class GmsModule {
    @Provides
    fun googleMobileServices(@ApplicationContext context: Context): AppMobileServices {
        return GmsServices(context)
    }
}

src/gms/java/your/package/GmsServices.kt:

@Singleton
class GmsServices(@ApplicationContext private val context: Context) : AppMobileServices {
    override val isAvailable: Boolean
        get() = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS
}

然后是HMS源集

src/hms/java/your/package/HmsModule.kt:

@Module
@InstallIn(SingletonComponent::class)
class HmsModule {
    @Provides
    fun huaweiMobileServices(@ApplicationContext context: Context): AppMobileServices {
        return HmsServices(context)
    }
}

src/hms/java/your/package/HmsServices.kt:

@Singleton
class HmsServices(@ApplicationContext private val context: Context) : AppMobileServices {
    override val isAvailable: Boolean
        get() = HuaweiApiAvailability.getInstance().isHuaweiMobileServicesAvailable(context) == ConnectionResult.SUCCESS
}

然后在您的主源代码集中,您只需注入AppMobileServices,就会提供正确的源代码。此外,所有依赖于 GMS 或 HMS 的代码都将放在它们的风味源集中。

@Inject
lateinit var appMobileServices: AppMobileServices

【讨论】:

  • 我可以用匕首做到这一点吗?因为我已经在使用 dagger,并且不想现在将我的代码迁移到 hilt。
  • 嗯,在 Dagger 中应该非常相似,您需要重命名模块(HmsModuleGmsModule),使它们具有相同的名称(例如 AppMobileServicesModule)并且在同一个包,然后将此模块包含在您的组件中。由于该模块将以两种形式存在,因此不应出现任何编译错误。
猜你喜欢
  • 1970-01-01
  • 2018-09-18
  • 1970-01-01
  • 2016-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-23
  • 1970-01-01
相关资源
最近更新 更多