Module SDK for Gradle

Answered

What's the recommended way of defining a module SDK for a Gradle module?  I have a multi-module setup under one module and I have to configure the sdk at every level, e.g. top level, sub module level, sub-module src, submodule test, etc.  Is this really that way it's supposed to work?  I can refer to the project sdk, but I don't see a way to refer to the module sdk from any of the submodules.

 

I also get a warning saying that

"The module is imported from Gradle.  Any changes made in its configuration may be lost after reimporting"

So there appears to be yet another place I need to define the SDK, at the Gradle level.  Can I just do this in one place and be done or am I forced to run the whole thing as a project and not have the ability to pull in a grade module along with my other projects?

0
9 comments

>I can refer to the project sdk, but I don't see a way to refer to the module sdk from any of the submodules.

There is only Project and Module SDK. Module SDK can specify either separate JDK or specify to inherit project JDK, see Define an SDK

>"The module is imported from Gradle. Any changes made in its configuration may be lost after reimporting"

In case of a Gradle/Maven based project IDE takes configuration for project structure from Gradle/Maven files so you must set build-related configuration in build.gradle/pom.xml, like for example source/target bytecode level configuration (sourceCompatibility/targetCompatibility) is taken from build.gradle - this corresponds to Project Structure | Project | Project language level (and individual modules level in Project Structure | Modules | Sources | Language level) and Settings (Preferences on macOS) | Build, Execution, Deployment | Compiler | Java Compiler | Project bytecode version, but

>So there appears to be yet another place I need to define the SDK, at the Gradle level.

IDE does not take configuration for the SDK from Gradle. So you define it in IDE settings manually.

0
Avatar
Permanently deleted user

Thanks for the reply.  The issue is that the submodules don't seem to take the Module SDK, but instead default to the project SDK.  Is that supposed to be the case?  If they defaulted to the module sdk, then I wouldn't have to go an override them all over the place.

0

>submodules don't seem to take the Module SDK, but instead default to the project SDK

What sub-modules do you mean? There is only one level of nesting for module. They all take the SDK from the project by default. As mentioned they can inherit it only from Project.

0
Avatar
Permanently deleted user

If I have a multi-module gradle project, like below:

* parent-module
    ** child-module-a
        ***src/main
        ****src/test
    ** child-module-b
        ***src/main
        ***src/test

 

At all of these levels, I need to explicitly declare the SDK, otherwise it will take the project SDK.  This seems to be a place where it would be nice to inherit from the parent-module sdk. 

 

0

Why do you need to use different JDKs for modules?

0
Avatar
Permanently deleted user

They way I work I have one project open with multiple, often unrelated modules open.  Most of them use the same JDK which I have defined at the project level, but I have one module (with submodules) that is using a later JDK.  All I was asking about is, is there a way for me to define the JDK for a module in one place and have it trickle down to all of the submodules.  It seems the answer is not at this time, which is a shame because the current behavior seems to be hostile to multi module development in that it forces you to define the same information at every level, even at the source set level!

0

I see. Thank you for your clarifications. Please vote for these requests in our tracker:

https://youtrack.jetbrains.com/issue/IDEA-109686

https://youtrack.jetbrains.com/issue/IDEA-217060

1
Avatar
Permanently deleted user

Thanks. I think it wasn't so much of a problem in the past when a it took 5 years between jdks, but with the new release cadence, it may be coming up more and more.

0

By the way if you use Gradle for Settings (Preferences on macOS) | Build, Execution, Deployment | Build Tools | Gradle | Build and run using then in this case when building IDE uses the JDK specified in Settings (Preferences on macOS) | Build, Execution, Deployment | Build Tools | Gradle | Gradle JVM for this Gradle project. So you need to only set it there. It will be used to build all the sub-modules of this Gradle project.

0

Please sign in to leave a comment.