简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM