Flutter项目升级后android_intent_plus引发Gradle构建失败
问题:升级Flutter依赖后android_intent_plus编译测试代码失败
问题描述
升级Flutter项目几乎所有依赖后,运行./gradlew build时出现编译错误,错误来自android_intent_plus的测试代码,推测是Mockito使用问题,但未找到相关公开问题。
已尝试的解决方法
- 跳过该依赖的指定测试类,配置无效:
project.tasks.withType(Test) { if (project.name == "android_intent_plus") { exclude '**/MethodCallHandlerImplTest.class' } } - 禁用该依赖的所有测试任务,同样无效:
if (project.name == 'android_intent_plus') { project.tasks.withType(Test) { enabled = false } } - 调整JVM目标版本为1.8和17,结果一致:
android { compileSdkVersion 34 ndkVersion "27.0.12077973" compileOptions { coreLibraryDesugaringEnabled true sourceCompatibility JavaVersion.VERSION_17 targetCompatibility JavaVersion.VERSION_17 } kotlinOptions { jvmTarget = '17' } }
错误日志
> Task :android_intent_plus:compileDebugUnitTestJavaWithJavac FAILED /Users/anderspetteroe/.pub-cache/hosted/pub.dev/android_intent_plus-5.1.0/android/src/test/java/io/flutter/plugins/androidintent/MethodCallHandlerImplTest.java:7: error: cannot find symbol import static org.mockito.Matchers.any; ^ symbol: class Matchers location: package org.mockito /Users/anderspetteroe/.pub-cache/hosted/pub.dev/android_intent_plus-5.1.0/android/src/test/java/io/flutter/plugins/androidintent/MethodCallHandlerImplTest.java:7: error: static import only from classes and interfaces import static org.mockito.Matchers.any; ^ /Users/anderspetteroe/.pub-cache/hosted/pub.dev/android_intent_plus-5.1.0/android/src/test/java/io/flutter/plugins/androidintent/MethodCallHandlerImplTest.java:8: error: cannot find symbol import static org.mockito.Matchers.eq; ^ symbol: class Matchers location: package org.mockito /Users/anderspetteroe/.pub-cache/hosted/pub.dev/android_intent_plus-5.1.0/android/src/test/java/io/flutter/plugins/androidintent/MethodCallHandlerImplTest.java:8: error: static import only from classes and interfaces import static org.mockito.Matchers.eq; ^ /Users/anderspetteroe/.pub-cache/hosted/pub.dev/android_intent_plus-5.1.0/android/src/test/java/io/flutter/plugins/androidintent/MethodCallHandlerImplTest.java:37: error: cannot find symbol private IntentSender sender; ^ symbol: class IntentSender location: class MethodCallHandlerImplTest /Users/anderspetteroe/.pub-cache/hosted/pub.dev/android_intent_plus-5.1.0/android/src/test/java/io/flutter/plugins/androidintent/MethodCallHandlerImplTest.java:38: error: cannot find symbol private MethodCallHandlerImpl methodCallHandler; ^ symbol: class MethodCallHandlerImpl location: class MethodCallHandlerImplTest /Users/anderspetteroe/.pub-cache/hosted/pub.dev/android_intent_plus-5.1.0/android/src/test/java/io/flutter/plugins/androidintent/MethodCallHandlerImplTest.java:43: error: cannot find symbol sender = new IntentSender(null, null); ^ symbol: class IntentSender location: class MethodCallHandlerImplTest /Users/anderspetteroe/.pub-cache/hosted/pub.dev/android_intent_plus-5.1.0/android/src/test/java/io/flutter/plugins/androidintent/MethodCallHandlerImplTest.java:44: error: cannot find symbol methodCallHandler = new MethodCallHandlerImpl(sender); ^ symbol: class MethodCallHandlerImpl location: class MethodCallHandlerImplTest /Users/anderspetteroe/.pub-cache/hosted/pub.dev/android_intent_plus-5.1.0/android/src/test/java/io/flutter/plugins/androidintent/MethodCallHandlerImplTest.java:54: error: cannot find symbol .setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class)); ^ symbol: method eq(String) location: class MethodCallHandlerImplTest /Users/anderspetteroe/.pub-cache/hosted/pub.dev/android_intent_plus-5.1.0/android/src/test/java/io/flutter/plugins/androidintent/MethodCallHandlerImplTest.java:54: error: cannot find symbol .setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class)); ^ symbol: method any(Class<BinaryMessageHandler>) location: class MethodCallHandlerImplTest /Users/anderspetteroe/.pub-cache/hosted/pub.dev/android_intent_plus-5.1.0/android/src/test/java/io/flutter/plugins/androidintent/MethodCallHandlerImplTest.java:68: error: cannot find symbol .setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class)); ^ symbol: method eq(String) location: class MethodCallHandlerImplTest /Users/anderspetteroe/.pub-cache/hosted/pub.dev/android_intent_plus-5.1.0/android/src/test/java/io/flutter/plugins/androidintent/MethodCallHandlerImplTest.java:68: error: cannot find symbol .setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class)); ^ symbol: method any(Class<BinaryMessageHandler>) location: class MethodCallHandlerImplTest 12 errors FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':android_intent_plus:compileDebugUnitTestJavaWithJavac'. > Compilation failed; see the compiler error output for details. * Try: > Run with --info option to get more log output. > Run with --scan to get full insights. Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0. You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. BUILD FAILED in 2s 198 actionable tasks: 3 executed, 195 up-to-date
问题原因及修复方案
原因分析
错误显示android_intent_plus:5.1.0的测试代码依赖Mockito旧版API(org.mockito.Matchers),但升级依赖后,项目中引入的Mockito版本已更新到3.0+,新版中Matchers类已被移到org.mockito.ArgumentMatchers,导致测试代码找不到对应类。另外测试代码中还存在类引用错误(IntentSender、MethodCallHandlerImpl),说明该版本的测试代码存在兼容性问题。
修复方法
升级android_intent_plus到最新版本
更新pubspec.yaml中的依赖到pub.dev上的最新版本:dependencies: android_intent_plus: ^最新版本号新版本大概率已修复测试代码的Mockito兼容性问题。
彻底跳过该依赖的测试编译任务
之前的配置只跳过了Test任务,但编译测试代码的compileDebugUnitTestJavaWithJavac任务仍在执行。在项目根目录的build.gradle中添加以下配置:subprojects { if (name == 'android_intent_plus') { tasks.withType(JavaCompile) { // 跳过测试代码编译 if (name.contains('UnitTest')) { enabled = false } } // 同时禁用测试任务 tasks.withType(Test) { enabled = false } } }临时替换该依赖的测试代码
若无法升级版本,可手动修改.pub-cache中android_intent_plus-5.1.0/android/src/test下的测试文件:- 将
import static org.mockito.Matchers.any;和import static org.mockito.Matchers.eq;替换为import static org.mockito.ArgumentMatchers.any;和import static org.mockito.ArgumentMatchers.eq; - 修复
IntentSender和MethodCallHandlerImpl的引用问题(确保导入正确的类)
注意:这种方法在重新拉取依赖后会失效,仅作为临时方案。
- 将
内容的提问来源于stack exchange,提问作者Anders Petterøe
相关产品推荐
相关产品推荐

