Reputation: 3180
I currently have 3 subprojects in my Gradle project, structured like so:
Main Project
|
-- Project A
-- Project B
-- Common Src
Project A has dependency on Lib A and Common Src, and Project B has dependency on Lib B and Common Src. Lib A and Lib B contain different implementations of the same classes.
My plan for Common Src project was to house all of the utility classes, necessarily depending on both Lib A and Lib B.
Is there a way to set this up, and how should my gradle project look like?
I've created a Minecraft mod compatible with both Forge (Project A) and Fabric (Project B) modloaders. I have utility classes in both projects that have the same source code but need to be compiled twice, each time with a different dependency. I don't want to have to write the code twice each time I want to change something in the Utility classes.
Upvotes: 3
Views: 1349
Reputation: 170
This may be what you are looking for:
In the "shared" project (/module) build script, use "api" instead of "implementation" for every dependency you want your subprojects to inherit from. Example: (kotlin DSL)
dependencies {
api("org.jetbrains.kotlinx", "kotlinx-coroutines-core", "1.6.4")
...
}
Then, make Project A and Project B "inherit" from that shared project like this
dependencies {
implementation(project(":shared")) // "shared" is your "utility" prject
}
Upvotes: 0
Reputation: 3180
EDIT: I've found a workaround:
Project A
and Project B
, add a task in each of their build.gradle
to copy the source files from Common Src
to the build directory of each project.sourceSets
of both Project A
and Project B
to include source files from the build folder we just copied intoCommon Src
to be a gradle subproject, and add Lib A
(or Lib B
) to the dependencies of Common Src
(just to get IntelliJ some context)Upvotes: 0