This lets Android Studio know that the kotlin directory is a source root, so when the project model is loaded into the IDE it will be properly recognized. Alternatively, you can put Kotlin classes in the Java source directory, typically located in src/main/java.
使用 Gradle Kotlin DSL
构建脚本后,将源代码根目录java
修改为kotlin
。
Kotlin 版本 build.gradle.kts
:
android {
...
sourceSets["main"].java.srcDir("src/main/kotlin")
}
对应的 Groovy 版本 build.gradle
:
android {
...
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}