Problem in understanding Gradle dependencies between local source projects
I'd like to keep multiple projects on my local disk with dependencies to each other. Like project `app` depends on project `lib`.
Coming from Rust+Cargo it's as easy as adding `[dependencies] lib = { path="/path/to/lib" }`.
I cannot figure out how to do that with Gradle. (I'm completely new to Gradle and I didn't identify my case in the official documentation)
In the "Gradle" view I linked `lib` to the `app` project which adds the source tree to the "Project" view. So I can see both.
When I try to use a class file from `lib`, IntelliJ suggests to add a module dependency to make that class visible to `app`. Doing so makes the IDE happy. But when I try to compile the project the compiler complains about a non-existent package.
Performing a "reimport all Gradle projects" removes that dependency. When I add this module dependency in the project settings by hand, I even get a warning about dependencies being managed by Gradle.
How can I add this dependency to a Gradle build file?
Please sign in to leave a comment.
Please see https://intellij-support.jetbrains.com/hc/en-us/community/posts/360003318419-Muliti-Project-or-Module-guide .
So, I added `includeBuild '../lib'` to `app/settings.gradle` which makes `lib` appear as child node in the Gradle view.
Then, I added a dependency `implementation project(':lib')` to `app/build.gradle`.
This gives me an error: "Project with path ':lib' could not be found in root project 'app'."
IntelliJ is still suggesting "add dependency on module ':lib'" for the missing class reference - so it basically accepts the `lib` project. It just isn't able to configure Gradle correctly.
(multi-module seems to work fine, but multi-project is giving me headaches)
Please provide a link to a sample project so that we can verify the configuration.
If the build works in the command line Gradle, but not in IntelliJ IDEA, it's probably some bug, otherwise you need to fix your Gradle project configuration and the problem is unrelated to IntelliJ IDEA, you may want to ask for help at https://stackoverflow.com/.
I don't think it's a bug but a misunderstanding on how to work with multiple source projects.
I uploaded my MVP to https://gist.github.com/kawogi/9ad87c1d43ede143d8cb03299fb694c8 (gradle.zip) with the following description:
The `app` project has a dependency to the `lib` project.
IntelliJ recognized this dependency.
Gradle knows about that dependency, too, but fails to
provide the `lib`-classes for a build of the `app`-project.
How to I have to set up this dependency?
How can IntelliJ help with this task?
Please see https://guides.gradle.org/creating-multi-project-builds/ .
Normally you will have 3 build.gradle files. One in the root and 2 in the sub-directories for the app and the library. There is also a compile dependency specified for the app on lib:
Both projects aren't necessarily part of the same gradle project. This is why I'm asking how to address `lib` from `app`. A multi project build (multi modules in a single IntelliJ project) work fine.
`compile project(':lib')` fails with `> Project with path ':lib' could not be found in root project 'app'.` because I don't know how to address the other project which has been included via `includeBuild("../lib")`
I'm trying to use composite build for that: https://docs.gradle.org/current/userguide/composite_builds.html
Please have a look at the provided project files.
In build.gradle of the app inside dependencies section add the following:
Note that this question has absolutely no relation to IntelliJ IDEA or any other IDE. I'd recommend using Gradle support forums or https://stackoverflow.com/ in the future for similar questions.
Thanks a lot, that's been the missing bit!
Being new to Gradle and IDEA I didn't know where to start looking. Those projects were created and configured with IDEA. I've been asking here because I originally expected IntelliJ IDEA to help me on that trivial task. I've been expecting that I'd just issue "a add dependency to (already known) project/module XY" and everything is set.
IDEA even adds a dependency, which vanishes on "Reimport". Maybe this intent(?) should be hidden then.
---------------
I'm having a follow up question using the Gradle multi project build as an alternative approach. Is it required to have a single big IDEA project at the (multi-project-)root? Or can I have a root with several IDEA-projects below?
I created a IDEA project below an existing gradle-root and the created project contains a `settings.gradle` with `rootProject.name = 'app'`. This makes be believe IDEA isn't aware of being part of a multi project build.
You can have several modules imported into a project: https://www.jetbrains.com/help/idea/creating-and-managing-modules.html#add-new-module . I'd open/import all the modules that are a part of the project you are working on.
IntelliJ IDEA Gradle integration is one way, it imports the settings from build file, but it cannot modify build files based on your settings in the module dependencies. There will be a warning displayed that all the manual settings are lost on reimport.
Start with a Gradle project that builds and runs in the command line, then import it in the IDE.
"IntelliJ IDEA Gradle integration is one way, it imports the settings from build file, but it cannot modify build files based on your settings in the module dependencies. There will be a warning displayed that all the manual settings are lost on reimport."
This is why I've been confused that IDEA creates such a dependency as intent without a warning.
I should re-visit maven. Maybe the IDE support is better there.
-------------
re: Gradle project root: I don't want to have modules but separate projects. I don't want to import the root into any IDEA project. Like:
gradle-root/{build.gradle, settings.gradle, .gradle}
gradle-root/my-app/{.idea, build.gradle}
gradle-root/my-lib/{.idea, build.gradle}
Maven integration in IntelliJ IDEA works the same, it's also one way.
Here is the warning displayed in the IDE when you try to add a dependency manually in Gradle or Maven project:
You can open only the subdirectory, like `app`, no root project is needed when using composite builds per https://blog.jetbrains.com/idea/2017/03/webinar-recording-composite-builds-with-gradle .
I know. This is what I meant in the opener with "When I add this module dependency in the project settings by hand, I even get a warning about dependencies being managed by Gradle." - I just wanted to note that IDEA shouldn't insert such a dependency on "Alt + Enter" then. It makes the user believe it would be ok to add such a dependency.
(no warning shown when IDEA adds the same (volatile) dependency for me)
Same window after Gradle reimport - dependency to lib.main is gone.
Ok, I interpret your suggestion about opening modules as projects as: No, it's not possible to have Gradle multi builds without a single IDEA project in the Gradle root containing everything.
I tried to open a module as project which converted the module in kind of a sub-project of the root project. The build failed until I manually copied that `gradle/wrapper` folder into that module. Do I have to copy anything else or should that folder have been there in there automatically?
After a Gradle reimport I'm seeing the entire root project tree just as if I had opened the entire root project. The only difference is "[:apps:my-app]" appearing behind the label of the module I opened.
>I know. This is what I meant in the opener with "When I add this module dependency in the project settings by hand, I even get a warning about dependencies being managed by Gradle." - I just wanted to note that IDEA shouldn't insert such a dependency on "Alt + Enter" then. It makes the user believe it would be ok to add such a dependency.
Thank you for the feedback. It is known problem, please vote for https://youtrack.jetbrains.com/issue/IDEA-167745
>Ok, I interpret your suggestion about opening modules as projects as: No, it's not possible to have Gradle multi builds without a single IDEA project in the Gradle root containing everything.
>I tried to open a module as project which converted the module in kind of a sub-project of the root project.
Gradle will fail to build such project without root project if it is required. IDE works here the same as Gradle: it auto-discovers the root project that has settings.gradle file if the child project references it.
>I tried to open a module as project which converted the module in kind of a sub-project of the root project. The build failed until I manually copied that `gradle/wrapper` folder into that module. Do I have to copy anything else or should that folder have been there in there automatically?
How did you open this project (the exact actions) - did you add it to an existing project or opened from File | New... action?
IDE when opening the project uses the Gradle specified in Settings (Preferences on macOS) | Build, Execution, Deployment | Build Tools | Gradle | Use Gradle from option. So if you have the wrapper.properties set there (default value) - IDE is looking for gradle wrapper in the project root. If you do not use it in project please specify the location from your disk. Related request: https://youtrack.jetbrains.com/issue/IDEA-215792