Modules, Gradle Subprojects, and IntelliJ Woes?

682 Views Asked by At

I am trying to make use of modules and gradle subprojects and either receive compile time errors or complaints from IntelliJ. My project structure is as follows:

Root
- settings.gradle
- core
  - build.gradle
- desktop
  - build.gradle
  - src/main/java/module-info.java

The desktop build.gradle file as a requirement on core as:

dependencies {
  implementation project(":core")
}

There is no reference to the "core" project in the module-info.java files.

In this configuration, IntelliJ doesn't like usage of classes from the "core" project in the "desktop" project (I see a lot of red), but running gradlew.bat desktop:run works with no issues. Bug with IntelliJ? I am using microsoft's openjdk, but hopefully that doesn't matter.

My best attempt to fix this, was to add a module-info.java file to the core project as well and reference the core project in the desktop project's module-info.java file. Unfortunately this configuration, while making IntelliJ happy, renders me unable to run the desktop from command line anymore. I get errors about the core project being unable to find required modules that are the exact same as the required modules I pull in for the desktop module. I think this might be an error on my part, but I'm not sure how or what to do to fix it.

Would appreciate any insight.

P.S. Of these issues, I think I would prefer to get IntelliJ working with the first setup, because I intend to have this core library be shared between the desktop project and an android project, and while I haven't looked into it yet, I am not sure Android supports new java modules?

1

There are 1 best solutions below

1
chehsunliu On

I think terminal should be the first priority. If your original setup made it compiled successfully under terminal, then there is no problem in your project. Do not add extra files for just making the IDE happy.

There's still something you can do. The first step is compiling your project successfully under terminal. This step downloads all the necessary dependencies before IntelliJ, reducing the problem scope to IntelliJ's linking and indexing. Then there are some options:

  1. In the Gradle tool window, click the top-left button Refresh all Gradle projects.
  2. Delete all the .idea folders in your project. Then open the root folder of your project via UI or CLI.
  3. Click the invalidate caches/restart in the main menu.

You can also also create a new Gradle multi-module project with command gradle init, and open it with IntelliJ. It should be analyzed by IntelliJ normally. Then you can compare its project structure with yours. Maybe there's something different.

Hope these steps could help you.