I’ve spent the past 2 days struggling with trying to create an IntelliJ project and I’m starting to wonder if there’s something fundamentally wrong with my understanding of modules and how they work. All I’m trying to do is create an application project in IntelliJ, add a module to it and call the module code from my main application code. Can’t for the life of me get it to work.
Here’s the exact steps I’m following:
-
I create a new project of type “JVM Application” called “ModuleTest” using the Gradle build System.
-
I create a module called “MyModule” by doing File → New → Module → Kotlin → JVM | IDEA.
-
I set dependencies by doing File → Project Structure → Modules. I then select ModuleTest, Add Module Dependency of “MyModule” and enable “Export”. Scope is set to the default of “Compile”. (Reloading my gradle file later causes this dependency to disappear, so I strongly suspect I’m wasting my time with this step).
-
I add a class called ModuleClass to MyModule\src:
class ModuleClass {
}
-
I add an entry file called Entry.kt to ModuleTest\src\main\kotlin:
fun main() {
ModuleClass()
}
When I build this project I get the following error:
`e: C:\...\ModuleTest\src\main\kotlin\Entry.kt: (2, 5): Unresolved reference: ModuleClass`
The module class and manifest file appear in the ModuleTest\out\production\MyModule folder, and I can get the build to work by modifying my build.gradle.kts to include those files directly:
compile(files("./out/production/MyModule"))
Something tells me that’s not the right way to go about this though (not the least of which is the warning due to compile
being deprecated). I’ve also tried adding include(":MyModule")
to my settings.gradle.kts file but that didn’t work at all.
This is frustrating beyond belief. Can someone please tell me what I’m doing wrong?
Gradle is used as the main source for the structure of your project. If you reimport it, any/most changes you made to IntelliJ modules will be lost. So your Gradle files must specify the dependencies between modules. For this you have to:
- Include the modules in
settings.gradle
like you did before. This will only tell Gradle to build that module, but won’t automatically make that module a dependency of all your other modules.
- Add a project dependency on
MyModule
to module ModuleTest
: Declaring dependencies
1 Like
Thanks for the response jstuyts, but unfortunately it hasn’t worked. This is what I’ve added to my settings.gradle.kts file:
include(":MyModule")
And this is in my build.gradle.kts:
dependencies {
implementation(project("MyModule"))
This results in the following error:
org.gradle.internal.exceptions.LocationAwareException: Build file 'C:\mypath\ModuleTest\build.gradle.kts' line: 13
Project with path 'MyModule' could not be found in root project 'ModuleTest'.
at org.gradle.kotlin.dsl.execution.InterpreterKt$locationAwareExceptionFor$2.invoke(Interpreter.kt:557)
at org.gradle.kotlin.dsl.execution.InterpreterKt.locationAwareExceptionFor(Interpreter.kt:564)
...etc...
I’ve also tried any and all combinations of MyModule
, :MyModule
, ModuleTest.MyModule
, ModuleTest:MyModule
and so on…nothing is working.
You need the colon in front of the module name. Gradle project paths are absolute:
implementation(project(":MyModule"))
1 Like
Thanks for the clarification, but it’s something I’ve already tried multiple times. When I specify :MyModule
in both files I get this:
Could not determine the dependencies of task ':compileJava'.
> Could not resolve all task dependencies for configuration ':compileClasspath'.
> Could not resolve project :MyModule.
Required by:
project :
> Unable to find a matching configuration of project :MyModule:
- None of the consumable configurations have attributes.
This is a fresh install of IntelliJ btw.
BTW, I’ve upload a repo to GitHub if it helps any…
https://github.com/Myndale/ModuleTest
There were 2 problems:
- Your module did not have a Gradle build script, so Gradle cannot know that it is a Kotlin project.
-
TestClass
was put in the wrong folder.
Other tips:
- Use module-specific packages.
- Do not commit the
.gradle
directory.
See Fixed Gradle project structure by jstuyts · Pull Request #1 · Myndale/ModuleTest · GitHub
2 Likes
Thank-you!! I’d tried numerous time creating the module with it’s own gradle build file but the piece of the puzzle I was missing was the need for that specific directory structure. Dunno why, I just don’t recall having read that anywhere. As I’m sure you can tell, I’m literally just days into learning Kotlin, and while I’ve tucked away my fair share of java apps over the years I’m predominately a .NET guy. Absolutely loving Kotlin so far, but I’m still in that very steep learning curve.
Thanks again, very grateful for you taking the time.
1 Like
Gradle has a steep learning curve. I remember that I had lot’s of problems with it when I started using kotlin. That said, once you get to know gradle it’s pretty much the best build tool out there (at least in my experience). It’s super powerful and customizable and it’s documentation is good, but it’s sometimes hard to find what your looking for exactly.
Also the recent move to kotlin gradle dsl (while great) makes it harder to find good tutorials and beginner guides.
4 Likes
It’s worth mentioning that because IntelliJ supports importing Gradle projects, it’s possible to just follow the Gradle tutorials and open it in IntelliJ after the initial setup. Of course you can always edit inside or outside of IntelliJ and IntelliJ will show you whatever the current configuration Gradle is at that point.
IntelliJ modules are technically different things than Gradle modules (aka, Gradle subprojects). Here’s a tutorial for creating a Gradle Multi-project build (basically a root project with subprojects). IntelliJ should automatically see the subprojects as modules if Gradle is including them.
I’d recommend creating the root project then opening IntelliJ while you do the rest of the tutorial. That way you can see IntelliJ update when you work through each step of the tutorial.
3 Likes