使用多风格编译 GMS 和 HMS 包。但是GMS版本编译的时候报错

Posted

技术标签:

【中文标题】使用多风格编译 GMS 和 HMS 包。但是GMS版本编译的时候报错【英文标题】:Using multi-flavor to compile GMS and HMS packages. But an error is reported when the GMS version is compiled 【发布时间】:2021-10-25 10:55:45 【问题描述】:

我有一个应用程序配置了两种编译风格,一种使用 HMS,另一种不使用。编译不使用HMS的flavor时,包名与json文件不一致。结果出现错误。

Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':app:processAppgalleryconnectLatestDebugAGCPlugin'.
> ERROR: Failed to verify AGConnect-Config '/client/package_name', expected: 'com.dise.appge.hms', but was: 'com.dise.appge.gms'

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
==============================================================================

那么当不同风味使用不同的包名时如何解决这个问题呢?

【问题讨论】:

【参考方案1】:

您可以在应用程序的 build.gradle 中配置它。比如HMS和GMS两种口味,可以尝试添加如下代码:

if (getGradle().getStartParameter().getTaskNames().toString().contains("HMS")) 
    apply plugin: 'com.huawei.agconnect'

即GMS编译时不添加AGC插件,避免JSON文件中包名不一致的问题。

【讨论】:

感谢您的解决方案,它为我节省了很多时间:)【参考方案2】:

productFlavors区块中可以应用各个插件,无需比较:

flavorDimensions "vendor"
productFlavors 
    google 
        dimension "vendor"
        apply plugin: "com.google.gms.google-services"
        apply plugin: "com.google.firebase.crashlytics"
        applicationIdSuffix ".gms"
    
    huawei 
        dimension "vendor"
        apply plugin: "com.huawei.agconnect"
        applicationIdSuffix ".hms"
    

【讨论】:

感谢Martin的回复,在我的项目中,由于未来会添加其他口味,我结合了你和shirley的解决方案,使用:if (getGradle().getStartParameter().getTaskNames().toString().contains("HMS")) apply plugin: 'com.huawei.agconnect' else apply plugin: "com.google.gms.google-services" 现在很好用!

以上是关于使用多风格编译 GMS 和 HMS 包。但是GMS版本编译的时候报错的主要内容,如果未能解决你的问题,请参考以下文章

如果华为设备同时支持 GMS 和 HMS 核心,那为啥 HMS 核心不工作?

将代码转换为 (G+H) 后应用程序崩溃。 GMS 和 HMS 支持

Android开发笔记(一百八十三)利用HMS轻松扫描二维码

Android开发笔记(一百八十三)利用HMS轻松扫描二维码

包 com.google.android.gms.samples.wallet 不存在导入 com.google.android.gms.samples.wallet.Constants

Google Play 商店是不是容忍华为 HMS?