You need to enable JavaScript to run this app. Flutter集成huawei_push被Google Play拒 如何同源码适配双应用商店
优惠活动
大模型
产品
解决方案
定价
更多

Flutter集成huawei_push被Google Play拒 如何同源码适配双应用商店

Flutter双应用商店接入推送服务实现方案

你可以根据项目规模选择以下两种实现方案,均可实现一套源码复用、仅华为应用市场版本引入华为Push Kit:

方案1:基于构建风味(Flavor)实现(推荐)

该方案为Android官方原生支持的多渠道打包方案,配置一次后长期维护成本更低。

1. Android端配置

在android/app/build.gradle文件中添加风味配置:

android {
    ...
    flavorDimensions "channel"
    productFlavors {
        google {
            dimension "channel"
            applicationIdSuffix ".google" // 可根据需求自定义
            resValue "string", "app_name", "你的应用名(谷歌版)"
        }
        huawei {
            dimension "channel"
            applicationIdSuffix ".huawei" // 可根据需求自定义
            resValue "string", "app_name", "你的应用名(华为版)"
        }
    }
}

dependencies {
    // 仅华为风味引入华为Push Kit原生依赖
    huaweiImplementation 'com.huawei.hms:push:6.12.0.300'
}

2. Flutter端配置

依赖配置

在pubspec.yaml中正常引入华为Push Kit依赖:

dependencies:
  flutter:
    sdk: flutter
  huawei_push: ^6.12.0+300
  # 其他公共依赖

代码逻辑隔离

通过条件导入+环境变量实现代码隔离,谷歌版本编译时不会引入任何华为Push相关代码:

  1. 抽象推送服务统一接口push_service.dart
  2. 分别实现华为推送逻辑huawei_push_impl.dart和谷歌推送逻辑fcm_push_impl.dart
  3. 新增入口导出文件push_service_manager.dart:
export 'push_service.dart'
  if (dart.library.io && const String.fromEnvironment('FLAVOR') == 'huawei') 'huawei_push_impl.dart'
  if (dart.library.io && const String.fromEnvironment('FLAVOR') == 'google') 'fcm_push_impl.dart';

打包命令

# 打谷歌渠道包
flutter build apk --flavor google --dart-define=FLAVOR=google
# 打华为渠道包
flutter build apk --flavor huawei --dart-define=FLAVOR=huawei

方案2:多pubspec.yaml实现

适合小型项目快速实现,无需修改原生配置。

1. 配置多套pubspec

在项目根目录创建两套pubspec文件:

  • pubspec_huawei.yaml:包含huawei_push依赖
  • pubspec_google.yaml:不包含huawei_push依赖

2. 代码逻辑隔离

和方案1一致,通过dart-define传入渠道标识,对推送逻辑做条件判断导入即可。

3. 打包命令

# 打谷歌渠道包
flutter build apk --pubspec=pubspec_google.yaml --dart-define=FLAVOR=google
# 打华为渠道包
flutter build apk --pubspec=pubspec_huawei.yaml --dart-define=FLAVOR=huawei

注意事项

  • 所有调用华为Push Kit API的位置都要做渠道判断,避免谷歌版本出现未定义类的报错
  • 谷歌版本打包完成后可通过APK Analyzer检查APK内容,确认无华为Push SDK相关字节码
  • 除移除华为Push SDK外,需检查其余所有第三方SDK的PendingIntent配置,必须显式指定目标组件,且明确设置PendingIntent.FLAG_IMMUTABLE或PendingIntent.FLAG_MUTABLE标记,避免再次被谷歌Play驳回

内容的提问来源于stack exchange,提问作者Krzysztof Mamak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 17:51:01