android – Why isn't “api” method available in Gradle 4.4/Java plugin, when “implementation” is?-ThrowExceptions

Exception or error:

I’m using AS 3.1, trying to be a good programmer and clear the “compile is obsolete” warnings. But I desire this particular .jar to expose Guava as an API. Other projects (e.g. CloudServerApplication) need Guava, too. But when I use the api keyword, instead of compile or implementation, I get this:

>gradlew FrameManager:build

> Configure project :CloudServerApplication
4.4

> Configure project :FrameManager
4.4


FAILURE: Build failed with an exception.

* Where:
Build file 'C:\FrameManager\app\build.gradle' line: 16

* What went wrong:
A problem occurred evaluating project ':FrameManager'.
> Could not find method api() for arguments [com.google.guava:guava:14+] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

I tried Googling the error, but did not find anything useful. Not really sure where to look next.

My build.gradle:

apply plugin: 'java'

println GradleVersion.current().getVersion()

repositories {
    jcenter()
}

dependencies {
    implementation 'org.json:json:20160212'
    api 'com.google.guava:guava:14+'   //<-- This used to be "compile"
}
How to solve:

Could not find method api() for arguments

The java plugin doesn’t contain this method.

You have to use this plugin:

apply plugin: 'java-library'

As you can check in the official doc:

The key difference between the standard Java plugin and the Java Library plugin is that the latter introduces the concept of an API exposed to consumers. A library is a Java component meant to be consumed by other components. It’s a very common use case in multi-project builds, but also as soon as you have external dependencies.

The plugin exposes two configurations that can be used to declare dependencies: api and implementation.

Leave a Reply

Your email address will not be published. Required fields are marked *