Gradle 5.0 - Cannot Find Symbol

Problem

If you have recently attempted to upgrade your project to Gradle 5.x from the 4.x release, you may encounter something that looks like this:

 symbol:   method verify(View)
  location: class ExampleTests
[FILE_PATH]:[LINE_NUMBER]: error: cannot find symbol
        verify(view).simpleTest();
        ^

In the example above, verify() is a method from Mockito Core which is a transitive dependency we have access to through using Mockito Inline in our project. We have been using the dependency this way for quite a while, so why is it broken now?

Taking a look at the Gradle 5.0 upgrade guide, I found this section that explains the issue.

The important bit is here:

With this new behavior, the Java and Java Library plugins both honor the separation of compile and runtime scopes. This means that the compilation classpath only includes compile-scoped dependencies, while the runtime classpath adds the runtime-scoped dependencies as well. This is particularly useful if you develop and publish Java libraries with Gradle where the separation between api and implementation dependencies is reflected in the published scopes.

If this means as much to you as it did to me(nothing), here is the breakdown:

Previously, dependencies IN your dependencies could be used by your code without you including them in your build.gradle. Now, if they aren’t setup to be transitive, they won’t be available.

How do we know if they are transitive? In the case of Mockito Inline (what I was using), we need to check the .pom file that describes the dependency.

<dependencies>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>2.9.0</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>

The scope here being set to runtime means we can no longer use the mockito-core classes by importing the mockito-inline dependency.

Solutions

1: Manually include the dependecy

Instead of using this:

implementation("org.mockito:mockito-inline:2.9.0")

Use this:

implementation("org.mockito:mockito-core:2.9.0")
implementation("org.mockito:mockito-inline:2.9.0")

2: Check the dependency for updates

Realizing my dependecy was out of date, I looked at the .pom file for the newest version (2.24.0 at time of writing) of Mockito Inline.

<dependencies>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>2.23.0</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

Now that the scope is listed as compile mockito-core methods can be used without definining it as a dependency in my file.

Wrap up

Building the project once again using either of these methods should yeild similar results to building before your Gradle upgrade. Happy building!

comments powered by Disqus