Do Kotlin 1.2.10 and Java 9 have opposite rules regarding automatic modules?

后端 未结 2 379
耶瑟儿~
耶瑟儿~ 2021-01-05 08:16

I have a Gradle project using the Kotlin Gradle plugin. I want to build a Java 9 module, so my directory structure looks like this:

src/main/java/
    - modu         


        
相关标签:
2条回答
  • 2021-01-05 08:57

    I find it easiest to simply suppress the warnings for use of an automatic module. (In my case I have to use some automatic modules whether I want to or not, so these warnings are just distracting noise.) I have the following in my build.gradle.kts:

    val compilerArgs = listOf(
        "-Xlint:all",                           // Enable all warnings except...
        "-Xlint:-requires-automatic",           // Suppress "requires directive for an automatic module" warnings from module-info.java
        "-Xlint:-requires-transitive-automatic" // Suppress "requires transitive directive for an automatic module" warnings from module-info.java
    )
    
    // This task will compile all Java code in the target module except for test code.
    tasks.compileJava {
        doFirst {
            options.compilerArgs.addAll(compilerArgs)
        }
    }
    
    // This task will compile all Java test code in the target module.
    tasks.compileTestJava {
        doFirst {
            options.compilerArgs.addAll(compilerArgs)
        }
    }
    
    0 讨论(0)
  • 2021-01-05 09:13

    If you're authoring Java 9 module in Kotlin, you have to declare requires kotlin.stdlib in your module-info.java in order to satisfy runtime dependencies of the compiled Kotlin code in addition to the explicit dependencies on the standard library API.

    javac warns you about requiring an automatic module when lint is enabled, because automatic modules have some potential drawbacks compared to normal modules. Until the standard library is compiled as a normal module, you have to deal with this warning.

    -Xallow-kotlin-package compiler flag allows you to omit require kotlin.stdlib, because it is intended to be used only when the standard library itself is compiled. Obviously, if you specify this flag and omit that requirement, you won't be able to use any API from the standard library, so this is not really an option for you.

    0 讨论(0)
提交回复
热议问题