如何解决使用风味在 AppGallery 和 Google Play 上发布应用
我想弄清楚如何在 AppGallery 和 Google Play 上发布我的应用。 (我的应用目前已在 Google Play 上架)
我研究了三个小时,最好的选择是使用口味。因为我想为不同的商店使用相同的代码库。为此,我决定添加如下口味:
productFlavors {
gms {
dimension "services"
buildConfigField "String","SERVICE_USED",'"g"'
}
hms {
dimension "services"
buildConfigField "String",'"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,这取决于设备类型。如果我采用这种方式,我需要类似的东西:
-
需要一个需要在 HmsIAP 和 GmsIAP 类上实现的接口,用于 requestProduct 方法、purchaseMethod 等常用方法。等
-
创建这些类并管理要使用的类的父类。这将称为“AppIAP”类。逻辑层将使用该类进行不依赖于平台的 IAP 操作。
这种方法对我来说很有意义,两个平台将有一个代码库。它看起来干净,将来易于管理。但问题是我为平台依赖项添加了风味。因此,如果我尝试构建我的代码的 hms 变体,它将无法编译,因为 Gms 库将丢失。尽管我使用的是 Hms 变体,但我仍然需要构建一个 GmsIAP 类。
为了解决我可以尝试不使用这些风格的问题,通过这种方法,我需要将我的应用程序与两个平台的库打包在一起,我的应用程序将可以正常编译。但正如以下链接所示,由于 Hms 库,Google Play 将拒绝我的应用。
Is Huawei HMS tolerated on Google Play Store?
我该如何解决?
解决方法
我认为最易于维护的解决方案是使用带有 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。