Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"
}
like image 305
greeble31 Avatar asked Mar 28 '18 20:03

greeble31


People also ask

What is the difference between implementation and API in Gradle?

In the first scenario, LibraryD is compiled by using api . If any change is implemented inside LibraryD, gradle needs to recompile LibraryD, LibraryB and all other modules which import LibraryB as any other module might use implementation of LibraryD.

What is API dependency in Gradle?

A Dependency represents a dependency on the artifacts from a particular source. A source can be an Ivy module, a Maven POM, another Gradle project, a collection of Files, etc... A source can have zero or more artifacts.

Does Gradle 4 support java8?

Gradle can only run on Java version 8 or higher. Gradle still supports compiling, testing, generating Javadoc and executing applications for Java 6 and Java 7. Java 5 and below are not supported.

What are the benefits of the Java library Gradle plugin?

It distinguishes between api and implementation dependencies, offering some key benefits for whoever consumes the library. 1. Gradle dependency configuration basics 2. Benefits of fine-grained classpath control 3. The Java Library Gradle plugin makes this possible 4. Real world example 5. Final thoughts 6. Resources

Are API dependencies enforced by the runtime in Gradle?

Note, that unlike public / private variables and methods, api / implementation dependencies are not enforced by the runtime. This is merely a build-time optimization, that allows Gradle to know which modules it needs to recompile when one of the dependencies changes its API.

What happened to compile configuration in Android Gradle plugin?

It is one of the breaking changes coming with Android Gradle plugin 3.0 that Google announced at IO17. The compile configuration is now deprecated and should be replaced by implementation or api

What is the difference between Gradle and Java module system?

The Java module system supports additional more fine granular encapsulation concepts than Gradle itself currently does. For example, you explicitly need to declare which packages are part of your API and which are only visible inside your module. Some of these capabilities might be added to Gradle itself in future versions.


1 Answers

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.

like image 96
Gabriele Mariotti Avatar answered Oct 20 '22 01:10

Gabriele Mariotti