androidflutterdartandroid-studiogradle

How do I fix my flutter gradle Mockito build error?


I've got a Flutter app I've been working on and it runs and debugs without any issues when I build for desktop. I decided to try it on Android, so I set up my phone for debugging and attempted to build it.

When I run ./gradlew build the build fails in the > Task :flutter_plugin_android_lifecycle:testDebugUnitTest FAILED section.

It's specifically happening in the getActivityLifecycle in the :flutter_plugin_android_lifecycle:testDebugUnitTest with io.flutter.embedding.engine.plugins.lifecycle.FlutterLifecycleAdapterTest.

The exception is

org.mockito.exceptions.base.MockitoException: 
Mockito cannot mock this class: class androidx.lifecycle.Lifecycle.

If you're not sure why you're getting this error, please open an issue on GitHub.


Java               : 21
JVM vendor name    : Oracle Corporation
JVM vendor version : 21.0.1+12-LTS-29
JVM name           : Java HotSpot(TM) 64-Bit Server VM
JVM version        : 21.0.1+12-LTS-29
JVM info           : mixed mode, sharing
OS name            : Linux
OS version         : 6.12.10-76061203-generic


You are seeing this disclaimer because Mockito is configured to create inlined mocks.
You can learn about inline mocks and their limitations under item #39 of the Mockito class javadoc.

Underlying exception : org.mockito.exceptions.base.MockitoException: Could not modify all classes [class androidx.lifecycle.Lifecycle, class java.lang.Object]

> org.mockito.exceptions.base.MockitoException: Could not modify all classes [class androidx.lifecycle.Lifecycle, class java.lang.Object]

This is with Gradle 8.14.3 and the Android Gradle plugin 8.3.2. How can I get past this?


Solution

  • I have two recommendations for this issue, also did you try flutter run or did you just try ./gradlew build ?

    1st Recommendation:

    If the 1st recommendation doesn't work, I recommend re-building the android folder, many times when I was building on iOS when I switched back to Android on a specific project that helped too, I've had some problems with versioning. Simply re-building the Android folder could solve the issue:

    2nd recommendation:

    Lastly, if this doesn't help can you please provide flutter doctor ?