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

Flutter应用启动时DynamiteModule报错及Firestore加载异常求助

Flutter应用启动DynamiteModule报错及Firestore初始加载异常解决方法

问题重现

每次重启应用时,日志中持续出现以下警告:

W/DynamiteModule( 6570): Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
I/DynamiteModule( 6570): Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0
W/ProviderInstaller( 6570): Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0.

同时首次启动时无法从Firestore获取数据,切换页面返回后数据加载恢复正常,上述警告也随之消失。已尝试网络权限配置、flutter clean/flutter pub get、更新依赖、多DEX配置等方案均未解决。

解决方案

1. 统一Firebase与Google Play Services版本约束

在项目根目录的android/build.gradle中,添加明确的版本变量并统一管理:

buildscript {
    ext {
        googleServicesVersion = "4.3.15" // 可替换为最新稳定版本
        firebaseBomVersion = "32.7.0"
    }
    repositories {
        // 原有仓库配置...
    }
    dependencies {
        classpath "com.google.gms:google-services:$googleServicesVersion"
        // 其他依赖...
    }
}

在android/app/build.gradle中使用Firebase BOM管理依赖,避免版本冲突:

dependencies {
    implementation platform("com.google.firebase:firebase-bom:$firebaseBomVersion")
    implementation "com.google.firebase:firebase-firestore"
    // 其他Firebase相关依赖...
}

2. 强制初始化ProviderInstaller

在Android原生Application类中提前初始化ProviderInstaller,确保Google Play Services组件就绪:

  1. 创建MyApplication.java文件(路径:android/app/src/main/java/[你的包名]/MyApplication.java):
import com.google.android.gms.common.ProviderInstaller;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import android.app.Application;

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        try {
            ProviderInstaller.installIfNeeded(this);
        } catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }
    }
}
  1. 在AndroidManifest.xml中指定自定义Application类:
<application
    android:name=".MyApplication"
    // 原有配置...
>

3. 确保Firestore初始化完成后再请求数据

在Flutter的main函数中等待Firebase初始化完成,避免提前发起数据请求:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(const MyApp());
}

同时,首次加载数据的逻辑需确保在Firestore实例就绪后执行,可使用FutureBuilder或异步等待包裹请求代码。

4. 配置Android网络安全策略(针对Android 9+)

在res/xml目录下创建network_security_config.xml:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

在AndroidManifest.xml的application标签中引用该配置:

android:networkSecurityConfig="@xml/network_security_config"

5. 确保设备Google Play Services为最新版本

  • 真机:通过Google Play商店更新Google Play Services
  • 模拟器:使用带有Google Play的系统镜像,并在AVD管理器中更新镜像至最新版本

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 11:55:35