在项目中同时具有GMS和HMS [英] Have both GMS and HMS in the project

查看:273
本文介绍了在项目中同时具有GMS和HMS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在应用程序中同时包含Google移动服务和华为移动服务?

How does one go about having both Google Mobile Services and Huawei Mobile Services in the app?

由于华为已经失去了GMS的许可,因此我们似乎需要用华为提供的服务替换应用中使用的所有GMS服务.为此,最佳实践"是什么?使用风味并以某种方式单独处理每个类,还是复制粘贴项目并开始替换?还是……更好的是,是否有一种方法可以同时拥有两者…………以某种方式让该应用程序根据所使用的设备来决定要使用哪种服务?显然,最后一个假设会增加apk文件的大小.

Being that Huawei have lost the license over GMS, it seems we need to replace all the GMS services used in the apps with Huawei provided ones. What would a "best practice" be for this? Use flavours and somehow handle each class individually, or copy paste the project and start replacing? Or ... better yet, is there a way to perhaps have both and ... somehow let the app decide which service to use based on the device it's on? Obviously the last one would presume an increase in the apk file size.

有什么想法吗?

推荐答案

因此,我设法做到了:

定义了两种口味

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

    }
    hms {
        dimension "services"
        buildConfigField "String", "SERVICE_USED", '"h"'
    }

每当我需要决定执行以下操作时,我都会在代码中使用"g"和"h":API要求deviceType为"android"或"iOS",并且包含华为构建,定义了另一个常量"huawei".我用SERVICE_USED知道要发送什么常量.

I use the "g" and "h" in the code whenever I need to decide on doing things like: the API requires a deviceType of "android" or "iOS" and with the inclusion of the Huawei build we defined another constant "huawei". I use SERVICE_USED to know what constant to send.

然后我在build.gradle的顶部执行此操作:

I then did this at the top of the build.gradle:

apply plugin: 'com.android.application'
if (getGradle().getStartParameter().getTaskRequests().toString().contains("Hms")) {
    //*meh*
} else {
    apply plugin: 'io.fabric'
}

因为我使用的是Fabric(以及fabric/firebase ...不适用于HMS),所以我也在build.gradle的最底层进行了此操作

because I was using fabric (and fabric / firebase ... don't really work with HMS) and I also did this at the very bottom of the build.gradle

if (getGradle().getStartParameter().getTaskRequests().toString().contains("Hms")) {
    apply plugin: 'com.huawei.agconnect'
} else {
    apply plugin: 'com.google.gms.google-services'
}

仅包含适当的插件.

然后我开始做一个包装,然后将每种口味的代码分开,以处理使用gms的每件事(地图,位置,推送通知,分析).即对于推送通知,我创建了具有getToken方法的HPushNotif.我在两种风格中定义了相同的类和方法,但是我根据服务类型(gms或hms)实现它们.

I then started handling each thing that was using gms (maps, location, push notifications, analytics ) by making a wrapper and separating the code in each flavour. i.e. for push notifications i created a HPushNotif which has an getToken method. I define the same class and method in both flavours but I implement them according to the type of service (gms or hms).

在项目中包含依赖项时,我使用了这种类型的表示法:

I used this type of notation when including dependencies in the project:

//GMS stuff
gmsImplementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
gmsImplementation 'com.google.firebase:firebase-core:16.0.9'
gmsImplementation 'com.google.firebase:firebase-messaging:18.0.0'
gmsImplementation 'com.google.firebase:firebase-crash:16.2.1'
gmsImplementation 'com.google.android.gms:play-services-maps:16.1.0'
gmsImplementation 'com.google.android.gms:play-services-location:16.0.0'
gmsImplementation 'com.google.android.gms:play-services-tagmanager:16.0.8'

//HMS stuff
hmsImplementation 'com.huawei.agconnect:agconnect-core:1.0.0.300'
hmsImplementation 'com.huawei.hms:push:4.0.3.301'
hmsImplementation 'com.huawei.hms:maps:4.0.1.301'
hmsImplementation 'com.huawei.hms:location:4.0.3.303'

Implementation之前的gmshms是指调味料的名称.当选择了适当的BuildVariant那些依赖将仅被加载(即,适当的风味正在建立).

The gms and hms before the Implementation refer to the name of the flavours. Those dependencies will only be loaded when the appropriate BuildVariant is selected (i.e. appropriate flavour is being built).

基本上,我包装了两种情况下的地图,分析,位置和推送通知的逻辑.这就是结构的外观.没什么特别的.

Basically I wrapped the logic for maps, analytics, location and push notifications for both cases. This is how the structure looks. Nothing special.

就是这样.当他们创建HMS时,他们基本上是按类复制GMS类,并按方法复制方法.您会看到确切的方法名称与调用参数甚至返回值完全匹配.他们是99.99%一样.这使事情变得容易.基本上,您只需要复制两个类中的代码并导入适当的内容(在类的顶部).您几乎不需要更改已经为GMS编写的代码.

That's it. When they created HMS they basically copied GMS class by class and methd by method. You'll see that the exact method names match exactly, to the calling parameters even and returning values. They're 99.99% the same. That makes things easier. Basically you just need to copy the code in two classes and import the proper things (at the top of the class). You rarely need to change the code you've already written for GMS.

希望它对某人有帮助.

这篇关于在项目中同时具有GMS和HMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆