Fixed kotlin module errors (the name must be the same throughout the project, otherwise it won't load correctly)

This commit is contained in:
Robinson 2021-04-25 16:14:04 +02:00
parent 917c32720b
commit 6e6ec96663
2 changed files with 19 additions and 17 deletions

View File

@ -716,7 +716,8 @@ open class StaticMethodsAndTools(private val project: Project) {
println("\tAllowing kotlin internal access for $moduleName") println("\tAllowing kotlin internal access for $moduleName")
project.tasks.withType(KotlinCompile::class.java).forEach { project.tasks.withType(KotlinCompile::class.java).forEach {
it.kotlinOptions.moduleName = moduleName // must be the same module name for everything! // must be the same module name as the regular one (which is the project name). If it is a different name, it crashes at runtime
it.kotlinOptions.moduleName = moduleName
} }
accessGroup.forEach { accessGroup.forEach {

View File

@ -49,7 +49,7 @@ class JavaXConfiguration(javaVersion: JavaVersion, private val project: Project)
val nameX = "_$ver" val nameX = "_$ver"
// If the kotlin plugin is applied, and there is a compileKotlin task.. Then kotlin is enabled // If the kotlin plugin is applied, and there is a compileKotlin task.. Then kotlin is enabled
val hasKotlin = StaticMethodsAndTools.hasKotlin(project) val hasKotlin: Boolean
val moduleFile = project.projectDir.walkTopDown().find { it.name == "module-info.java" } val moduleFile = project.projectDir.walkTopDown().find { it.name == "module-info.java" }
var moduleName: String var moduleName: String
@ -95,6 +95,8 @@ class JavaXConfiguration(javaVersion: JavaVersion, private val project: Project)
throw GradleException("The module name must be specified in the module-info file! Verify file: $moduleFile") throw GradleException("The module name must be specified in the module-info file! Verify file: $moduleFile")
} }
hasKotlin = StaticMethodsAndTools.hasKotlin(project)
val info = when { val info = when {
hasKotlin -> "Initializing JPMS $ver, Java/Kotlin [$moduleName]" hasKotlin -> "Initializing JPMS $ver, Java/Kotlin [$moduleName]"
else -> "Initializing JPMS $ver, Java [$moduleName]" else -> "Initializing JPMS $ver, Java [$moduleName]"
@ -208,7 +210,8 @@ class JavaXConfiguration(javaVersion: JavaVersion, private val project: Project)
sourceCompatibility = ver sourceCompatibility = ver
targetCompatibility = ver targetCompatibility = ver
kotlinOptions.jvmTarget = ver kotlinOptions.jvmTarget = ver
kotlinOptions.moduleName = compileMainKotlin.kotlinOptions.moduleName // must be the same module name // must be the same module name as the regular one (which is the project name). If it is a different name, it crashes at runtime
kotlinOptions.moduleName = project.name
} }
compileTestXKotlin.apply { compileTestXKotlin.apply {
@ -216,7 +219,8 @@ class JavaXConfiguration(javaVersion: JavaVersion, private val project: Project)
sourceCompatibility = ver sourceCompatibility = ver
targetCompatibility = ver targetCompatibility = ver
kotlinOptions.jvmTarget = ver kotlinOptions.jvmTarget = ver
kotlinOptions.moduleName = compileTestKotlin.kotlinOptions.moduleName // must be the same module name // must be the same module name as the regular one (which is the project name). If it is a different name, it crashes at runtime
kotlinOptions.moduleName = project.name
} }
} }
@ -234,22 +238,9 @@ class JavaXConfiguration(javaVersion: JavaVersion, private val project: Project)
mainX.allSource.srcDirs mainX.allSource.srcDirs
) )
val allCompiled = if (hasKotlin) {
proj.files(
compileMainJava.destinationDir,
compileMainKotlin.destinationDir
)
} else {
proj.files(
compileMainJava.destinationDir
)
}
source = allSource.asFileTree // the files live in this location source = allSource.asFileTree // the files live in this location
include("**/module-info.java") include("**/module-info.java")
sourceCompatibility = ver sourceCompatibility = ver
targetCompatibility = ver targetCompatibility = ver
@ -261,6 +252,12 @@ class JavaXConfiguration(javaVersion: JavaVersion, private val project: Project)
// modules require this! // modules require this!
doFirst { doFirst {
val allCompiled = if (hasKotlin) {
proj.files(compileMainJava.destinationDir, compileMainKotlin.destinationDir)
} else {
proj.files(compileMainJava.destinationDir)
}
// the SOURCE of the module-info.java file. It uses **EVERYTHING** // the SOURCE of the module-info.java file. It uses **EVERYTHING**
options.sourcepath = allSource options.sourcepath = allSource
options.compilerArgs.addAll(listOf( options.compilerArgs.addAll(listOf(
@ -315,6 +312,10 @@ class JavaXConfiguration(javaVersion: JavaVersion, private val project: Project)
val length = details.path.length + 1 val length = details.path.length + 1
val sourceDir = absolutePath.substring(0, absolutePath.length - length) val sourceDir = absolutePath.substring(0, absolutePath.length - length)
// println("checking file: $absolutePath")
// println("checking file: $sourceDir")
if (sourcePaths.contains(sourceDir)) { if (sourcePaths.contains(sourceDir)) {
// println("Moving: " + absolutePath) // println("Moving: " + absolutePath)
// println(" : " + details.path) // println(" : " + details.path)