I have three gradle
projects. Say ProjectA
, ProjectB
and ProjectC
.
ProjectC
is dependent on both ProjectA
and ProjectB
. While ProjectB
is dependent on ProjectA
.
So ProjectC
's build.gradle
has the following lines:
dependencies {
implementation project(':ProjectA')
implementation project(':ProjectB')
}
And ProjectB
's build.gradle
has the following:
dependencies {
implementation project(':ProjectA')
}
My question is why do I need explicit implementation
declaration for ProjectA
in ProjectC
's build file?
Since, I am adding ProjectB
, shouldn't ProjectA
be included automatically since ProjectB
is dependent on ProjectA
?
In other words, why the following does not work for ProjectC
?
dependencies {
implementation project(':ProjectB')
}
I am new to gradle
and thus trying to understand how dependency management between Project's work.
Edit:
So I want to change ProjectB
's build.gradle to below:
dependencies {
api project(':ProjectA')
}
So that I can simplify ProjectC
's build.gradle to:
dependencies {
implementation project(':ProjectB')
}
However, I get the following error:
A problem occurred evaluating project ':ProjectB'.
> Could not find method api() for arguments [:ProjectA] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Am I missing something?
Transitive dependency Releases of a module hosted on a repository can provide metadata to declare those transitive dependencies. By default, Gradle resolves transitive dependencies automatically. The version selection for transitive dependencies can be influenced by declaring dependency constraints.
You can tell Gradle to disable transitive dependency management for a dependency by setting ModuleDependency.
It means that dependency graph contains multiple dependencies with the same group and module but different versions for e.g. org.
Because implementation
is precisely for that: it tells that ProjectA is needed for the code of ProjectB to work (internally), but is not part of its API (i.e. you don't want clients of ProjectB to rely on the fact that it uses ProjectA internally).
If you want ProjectA to be part of the API of ProjectB, then use the api
configuration rather than implementation.
See the guide for more details.
Add
plugins {
id 'java-library'
}
to the build.gradle
file, this will enable api
modifier for the parent project
See: https://docs.gradle.org/current/userguide/java_library_plugin.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With