升级Flutter 3.32.0后huawei_push插件编译失败求助
问题描述
我正尝试将Flutter应用从2.5.x版本迁移至3.32.0版本。解决依赖问题并更新配置后,执行flutter build apk构建APK时失败,报错与huawei_push插件(版本6.12.0+302)相关,错误指向插件Java代码中存在缺失类及废弃API问题。
错误输出
FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':huawei_push:compileReleaseJavaWithJavac'. > Compilation failed; see the compiler output below. warning: [options] To suppress warnings about obsolete options, use -Xlint:-options. Note: Recompile with -Xlint:deprecation for details. error: package io.flutter.embedding.engine.plugins.shim does not exist import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry; ^ error: cannot find symbol import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback; ^ symbol: class PluginRegistrantCallback ... error: cannot find symbol pluginRegistrantCallback.registerWith(new ShimPluginRegistry(flutterEngine)); ^ symbol: class ShimPluginRegistry Note: Some input files use or override a deprecated API. warning: [options] source value 8 is obsolete and will be removed in a future release warning: unknown enum constant Mode.STRICT warning: unknown enum constant Mode.LOCAL
相关配置文件
android/build.gradle
buildscript { ext.kotlin_version = '2.0.20' repositories { google() mavenCentral() maven { url 'https://developer.huawei.com/repo/' } } dependencies { classpath 'com.android.tools.build:gradle:8.1.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.google.gms:google-services:4.4.2' classpath 'com.huawei.agconnect:agcp:1.9.1.302' } } allprojects { repositories { google() mavenCentral() maven { url 'https://developer.huawei.com/repo/' } } }
android/app/build.gradle
plugins { id "com.android.application" id "org.jetbrains.kotlin.android" id "com.google.gms.google-services" id "com.huawei.agconnect" id "dev.flutter.flutter-gradle-plugin" } dependencies { implementation platform('com.google.firebase:firebase-bom:33.14.0') implementation 'com.google.firebase:firebase-analytics-ktx' implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:2.0.20' implementation 'androidx.multidex:multidex:2.0.1' implementation 'com.huawei.agconnect:agconnect-core:1.9.1.301' implementation 'com.huawei.hms:push:6.13.0.300' implementation 'com.google.android.gms:play-services-tagmanager:18.1.0' implementation 'com.facebook.android:facebook-android-sdk:16.1.3' coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.4' }
已尝试方案
- 确认两个Gradle文件中已添加华为Maven仓库。
- 执行
flutter clean和flutter pub get命令。 - 验证
huawei_push: ^6.12.0+302与Flutter 3.32.0的兼容性(相关文档有限)。
环境信息
- Flutter: 3.32.0
- Java: 17
解决方案
这些错误核心原因是huawei_push 6.12.0+302版本依赖的Flutter旧版嵌入API在3.32.0中已被移除或重构,比如ShimPluginRegistry和PluginRegistrantCallback类已不再存在。
1. 升级huawei_push插件到兼容版本
华为官方已更新插件适配新版Flutter,直接在pubspec.yaml中修改依赖:
huawei_push: ^6.13.0+300
注意要和app/build.gradle中com.huawei.hms:push:6.13.0.300的版本保持一致,避免版本不匹配。
2. 调整Java编译配置
在android/app/build.gradle的android块中明确指定Java版本,适配Flutter 3.x要求:
android { compileOptions { sourceCompatibility JavaVersion.VERSION_17 targetCompatibility JavaVersion.VERSION_17 coreLibraryDesugaringEnabled true } kotlinOptions { jvmTarget = '17' } }
3. 清理后重新构建
完成上述修改后,执行以下命令彻底清理并重新构建:
flutter clean flutter pub get cd android && ./gradlew clean cd .. flutter build apk
备选方案(无法升级插件时)
如果暂时不能升级插件,需手动修改插件的Java代码(不推荐,优先升级):
- 移除
io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry的导入语句 - 将
pluginRegistrantCallback.registerWith(new ShimPluginRegistry(flutterEngine));替换为pluginRegistrantCallback.registerWith(flutterEngine); - 替换旧的
PluginRegistrantCallback导入为新版Flutter嵌入API的对应类
内容的提问来源于stack exchange,提问作者Ashfaq Manu
相关产品推荐
相关产品推荐

