You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter Firebase Analytics Android端网络上传失败问题求助

Flutter Firebase Analytics Android端无法记录事件(iOS正常)

问题现象

iOS端Firebase Analytics事件记录功能正常,但Android端无法上传事件,adb logcat输出以下错误:

05-27 14:55:08.562 V/FA-SVC ( 6814): Network upload failed. Will retry later. code, error: 0, java.net.ConnectException: Failed to connect to app-measurement.com/[::]:443: btlq.run(:com.google.android.gms@242013038@24.20.13 (190400-633713831):154)

已完成的排查步骤

  • Firebase配置验证:确认iOS的GoogleService-Info.plist和Android的google-services.json放置路径正确,Firebase项目已注册且Analytics服务已启用。
  • 网络环境测试:在不同网络下验证连接稳定性,排除基础网络故障。
  • Google Play Services兼容性:检查应用与设备上Google Play Services的版本匹配度。
  • 依赖冲突处理:通过flutter pub outdated和flutter pub upgrade更新并解决依赖冲突。
  • 清理构建:执行flutter clean和flutter build apk清理缓存并重新构建应用。
  • 防火墙/代理排查:检查是否存在阻止连接的防火墙或代理限制。
  • 隔离测试:创建全新Firebase项目和仅包含Analytics的Flutter项目进行验证。

环境信息

  • Flutter版本:3.19.6 / 3.22.1
  • Google Play Services:4.3.15
  • 核心依赖包:
    • firebase_analytics: ^10.10.6
    • firebase_auth: ^4.19.6
    • firebase_core: ^2.31.1

相关配置文件

project/build.gradle

buildscript {
    ext.kotlin_version = '1.9.22'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.4.2'
        // START: FlutterFire Configuration
        classpath 'com.google.gms:google-services:4.4.1'
        // END: FlutterFire Configuration
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

app/build.gradle

plugins {
    id "com.android.application"
    id("com.google.gms.google-services")
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
    
}

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    namespace "ro.test.test"
    compileSdkVersion 34
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        applicationId "ro.daredigital.wishmo"
        minSdkVersion 24
        targetSdkVersion 34
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true
    }

     signingConfigs {
       release {
           keyAlias keystoreProperties['keyAlias']
           keyPassword keystoreProperties['keyPassword']
           storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
           storePassword keystoreProperties['storePassword']
       }
   }
   buildTypes {
       release {
           signingConfig signingConfigs.release
       }
   }
}

flutter {
    source '../..'
}

dependencies {
    // Import the BoM for the Firebase platform
    implementation(platform("com.google.firebase:firebase-bom:33.0.0"))
    // Add the dependency for the Analytics library
    implementation("com.google.firebase:firebase-analytics")
}

针对性解决方案

1. 升级Firebase相关依赖版本

当前使用的Firebase BoM版本33.0.0和Google Services插件版本4.4.1较旧,建议升级到最新稳定版:

  • 在project/build.gradle中,将classpath 'com.google.gms:google-services:4.4.1'替换为classpath 'com.google.gms:google-services:4.4.10'
  • 在app/build.gradle中,将implementation(platform("com.google.firebase:firebase-bom:33.0.0"))替换为implementation(platform("com.google.firebase:firebase-bom:33.4.0"))

升级后执行flutter clean、flutter pub get、flutter build apk重新构建应用。

2. 强制使用IPv4连接

错误日志显示尝试连接IPv6地址失败,部分网络环境可能不支持IPv6,可通过配置强制应用使用IPv4:
在android/app/src/main/AndroidManifest.xml的<application>标签内添加:

<uses-library
    android:name="org.apache.http.legacy"
    android:required="false" />
<meta-data
    android:name="firebase_analytics_collection_ipv6_enabled"
    android:value="false" />

3. 验证应用签名与Firebase配置匹配

Android端google-services.json中的签名信息需与应用实际签名一致:

  • 执行keytool -list -v -keystore <你的keystore文件路径> -alias <别名>生成签名SHA-1/SHA-256
  • 登录Firebase控制台,在项目设置的Android应用页面检查是否已添加该签名值,未添加则补充后重新下载google-services.json替换到项目中。

4. 启用调试模式查看详细日志

在Android设备上执行以下命令启用Firebase Analytics调试模式:

adb shell setprop debug.firebase.analytics.app ro.daredigital.wishmo

重新运行应用后,查看logcat中的FA相关日志,定位更具体的上传失败原因。

内容的提问来源于stack exchange,提问作者Asim Bhadra

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 06:37:03