'How to add kotlin source files to my java gradle groovy project? (NOT android)

I have a build.gradle in groovy, for a Java project. I have kotlin sources somewhere else. How can I add those sources to my java project?



Solution 1:[1]

In build.gradle,

Change

plugins {
    id 'java'
    id 'maven-publish'
}

to

plugins {
    id 'java'
    id 'maven-publish'
    id "org.jetbrains.kotlin.jvm" version "1.6.20-RC2"
}

then add the kotlin sources also

sourceSets { 
    main.java.srcDirs += 'src/main/kotlin'
} 

Don't forget to respect the my/package/file.kt where file.kt has package my.package. So, the file must be in the folder path like this.

On java you can simply import like this:

import my.package.*;

If your kotlin compiles but you get import errors, then try to verify the folder where the kotlin file is, it's likely a problem with this path. I think the difference from Java is that the file does nothave to follow the name of the class.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Guerlando OCs