Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

In a Gradle Java library, what is the difference between api, implementation, compileOnly and runtimeOnly configurations?

implementation dependencies are used to compile and run our module but are hidden from the compile classpath of modules that depend on us. api dependencies, available with the java-library plugin, are exposed to consumers because their types appear in our public API. compileOnly is for things needed only to compile, such as annotations, and runtimeOnly is for things needed only when running, such as a JDBC driver.

Why this question is asked

Every Gradle Java build uses these four words, and choosing between them decides how fast a multi-module build recompiles and how many accidental dependencies leak between modules. Interviewers ask this to see whether we know what a classpath is and why hiding dependencies is useful, not just which keyword to type. In this answer, we will recap the two classpaths Gradle builds, see what each configuration puts on them, look at an example with two modules, and see why implementation should be the default.

1A Quick Recap: Compile Classpath and Runtime Classpath

A classpath is the list of jars and directories where Java looks for classes. Gradle builds two of them for each module. The compile classpath is what javac sees when it compiles our code: if a class is not on it, we cannot reference it in source code. The runtime classpath is what the JVM sees when our code actually runs, in tests or in the application.

In Gradle, a configuration is a named bucket of dependencies. We put dependencies into buckets such as implementation, and Gradle combines buckets to build the compile classpath and the runtime classpath. Each of the four configurations in the question puts its dependencies on a different combination of classpaths, for our module and for the modules that depend on it.

2What Each Configuration Puts Where

The difference is easiest to see as a table. Our module is called orders-core, and a consumer is any module that declares implementation(project(":orders-core")).

Where each configuration's dependencies end up
ConfigurationOur compileOur runtimeConsumer compileConsumer runtime
apiyesyesyesyes
implementationyesyesnoyes
compileOnlyyesnonono
runtimeOnlynoyesnoyes
implementation still reaches the consumer at runtime; it is only hidden from the consumer's compiler.

The api configuration only exists when we apply the java-library plugin instead of the plain java plugin. That makes sense: only a library has consumers who compile against it. An application module uses implementation, compileOnly and runtimeOnly.

3An Example With Two Modules

Suppose orders-core exposes a method that returns a Jackson JsonNode, uses Guava internally, uses Lombok annotations to generate getters, and talks to PostgreSQL.

Java
// orders-core/build.gradle.kts
plugins { `java-library` }
dependencies {
api("com.fasterxml.jackson.core:jackson-databind:2.18.2") // JsonNode is in our public methods
implementation("com.google.guava:guava:33.4.0-jre") // used only inside our classes
compileOnly("org.projectlombok:lombok:1.18.36") // annotations, gone after compiling
annotationProcessor("org.projectlombok:lombok:1.18.36") // the processor that reads them
runtimeOnly("org.postgresql:postgresql:42.7.4") // loaded by JDBC through the driver name
testImplementation("org.junit.jupiter:junit-jupiter:5.11.4")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
// orders-api/build.gradle.kts
dependencies {
implementation(project(":orders-core"))
}
// In orders-api: import com.fasterxml.jackson.databind.JsonNode; // compiles
// In orders-api: import com.google.common.collect.ImmutableList; // error: package does not exist

Notice what the consumer sees. Jackson is on its compile classpath, because orders-api has to know JsonNode to call our method. Guava is not, so orders-api cannot accidentally start using Guava just because orders-core happens to; if it wants Guava, it must declare it. At runtime, both jars are present, because orders-core's code still needs Guava to run.

Lombok is a good example of compileOnly: its annotations are processed during compilation and nothing of it is needed at runtime. The PostgreSQL driver is the classic runtimeOnly case, since our code talks to java.sql interfaces and the driver is found at runtime. The test variants follow the same logic: testImplementation is for compiling and running tests, and testRuntimeOnly for things like the JUnit Platform launcher, which only the test runner needs.

4Why implementation Should Be the Default

The first reason is build speed. When a dependency is on a consumer's compile classpath, Gradle must recompile the consumer whenever that dependency's public classes change. If orders-core upgrades Guava from 33.3 to 33.4 and Guava is implementation, orders-api's compile classpath is unchanged, so its compileJava stays UP-TO-DATE. With api, every consumer recompiles. In a build of 50 modules, that difference adds up to minutes.

The second reason is cleaner design. Every api dependency is a promise: consumers can compile against those types, so removing or replacing the library later is a breaking change. With implementation, we can switch from Guava to plain Java collections without any consumer noticing. So the rule is simple: use implementation unless a type from the dependency appears in our public method signatures, superclasses or public fields. That is exactly the case api is for.

Step throughChoosing a configuration for a dependency
  1. Needed only to compile?annotations, processors
    YescompileOnlyNoNeeded only at runtime?
  2. Needed only at runtime?JDBC driver, logging backend
    YesruntimeOnlyNoIts types in our public API?
  3. Its types in our public API?
    YesapiNoimplementation

3 steps in order. Play them, or step through them one at a time.

5Follow-up questions

Is compileOnly the same as Maven's provided scope?

Close but not identical. Maven's provided puts the dependency on the compile and test classpaths and expects the runtime to supply it, for example a servlet container. Gradle's compileOnly is only on the main compile classpath, not on the test classpaths, so tests that need it must also declare it with testImplementation or testCompileOnly. For a war, the war plugin offers providedCompile and providedRuntime.

What is compileOnlyApi?

It is a java-library configuration for a dependency that consumers need to compile but nobody needs at runtime, typically annotations that appear in our public API, such as nullness annotations. It goes on our compile classpath and our consumers' compile classpath, and on no runtime classpath.

What happened to the old compile configuration?

compile and runtime were deprecated long ago and removed in Gradle 7. They behaved like api today, exposing everything to consumers, which is one reason old builds recompiled so much. Builds still using them fail on current Gradle and should be moved to implementation, with api only where needed.

6Conclusion

In this answer, we saw that Gradle builds a compile classpath and a runtime classpath, and that each configuration places dependencies on a different mix of them. implementation is used to compile and run our module but hidden from consumers' compilers, api exposes its types to consumers and exists with the java-library plugin, compileOnly is for compile time only and runtimeOnly for run time only, with test variants such as testImplementation and testRuntimeOnly. Making implementation the default means fewer consumer recompilations and fewer leaked types.

What to say in the interviewAbout 80 seconds aloud

Open with

All four decide which classpath a dependency goes on. implementation means our module compiles and runs with it, but modules that depend on us do not see it when they compile; they still get it at runtime, because our code needs it. api, which needs the java-library plugin, also puts the dependency on our consumers' compile classpath, and it is for libraries whose types appear in our public API, for example if a public method returns a Jackson JsonNode. compileOnly is needed only to compile, like Lombok or annotations, and is not packaged or on the runtime classpath. runtimeOnly is the opposite, needed only when running, like a JDBC driver or a logging backend.

Then, if they want more

There are test versions too, testImplementation and testRuntimeOnly, for example the JUnit Platform launcher. I use implementation by default for two reasons. First, if an implementation dependency changes, consumers' compile classpath does not change, so Gradle does not recompile them, which matters a lot in a large multi-module build.

Second, it keeps internals private: consumers cannot accidentally depend on our Guava, and we can replace it later without breaking anyone. I only use api when the type really is part of our contract.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Gradle user manual
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "In a Gradle Java library, what is the difference between api, implementation...": https://scrivio-interview-library.netlify.app/java/gradle/372d90402b/