我正在尝试弄清楚如何在 AppGallery 和 Google Play 上发布我的应用程序。 (我的应用目前在 Google Play 上可用)
我研究了三个小时,最好的选择是使用 flavors。因为我想为不同的商店使用相同的代码库。为此,我决定添加如下口味:
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'
现在我有一个问题:
如果我错了,请纠正我,我需要为公共(public)服务使用抽象层,对吗?例如,如果我需要使用 IAP,我需要一个包装类来决定使用 GMS 还是 HMS,这取决于设备类型。如果我采用这种方式,我需要这样的东西:
需要一个需要在 HmsIAP 和 GmsIAP 类上实现的接口(interface),用于 requestProduct 方法、purchaseMethod 等常用方法。等等
创建这些类并管理使用哪个类的父类。那将称为“AppIAP”类。逻辑层将使用该类进行不依赖于平台的 IAP 操作。
这种方法对我来说很有意义,两个平台将有一个代码库。看起来干净利落,方便日后管理。但问题是我为平台依赖添加了 flavor 。因此,如果我尝试构建我的代码的 hms 变体,它将无法编译,因为 Gms 库将丢失。尽管我使用的是 Hms 变体,但我仍然有一个需要构建的 GmsIAP 类。
为了解决这个问题,我可以尝试不使用这些风格,通过这种方法,我需要将我的应用程序与两个平台的库一起打包,并且我的应用程序可以正常编译。但如下面的链接所示,由于 Hms 库,Google Play 将拒绝我的应用。
Is Huawei HMS tolerated on Google Play Store?
我该如何解决?
最佳答案
我认为最可维护的解决方案是使用依赖注入(inject) different source sets for different flavors .您可以将依赖于 GMS 的代码放在 src/gms/java
中,将依赖于 HMS 的代码放在 src/hms/java
中。只会编译选定的产品 flavor 源集。非常基本的例子 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
}
然后在您的主源集中,您只需注入(inject) AppMobileServices
即可提供正确的服务。此外,所有依赖于 GMS 或 HMS 的代码都将放在它们的风格源集中。
@Inject
lateinit var appMobileServices: AppMobileServices
关于android - 使用 flavor 在 AppGallery 和 Google Play 上发布应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65860957/