一、編譯配置
1、Android中的配置
使用如下方式開啟在Android中的gradle的kotlin編譯配置:
該配置在其余平臺不可用
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '1.8'
// freeCompilerArgs.add("-Xexport-kdoc")
setFreeCompilerArgs(["-Xcontext-receivers"])
//setFreeCompilerArgs(["-Xexport-kdoc","-Xcontext-receivers","-opt-in=org.mylibrary.OptInAnnotation"])
}
//如下方式會報錯
//tasks.withType(KotlinCompile::class).all {
// kotlinOptions.freeCompilerArgs = listOf("-Xcontext-receivers")
//}
...
}
以上配置會開啟Kotlin/JVM 的上下文接收者原型功能,否則該功能不可用,開啟后編碼可以使用以下代碼:
interface LoggingContext {
val log: Logger // This context provides a reference to a logger
}
context(LoggingContext)
fun startBusinessOperation() {
// You can access the log property since LoggingContext is an implicit receiver
log.info("Operation has started")
}
fun test(loggingContext: LoggingContext) {
with(loggingContext) {
// You need to have LoggingContext in a scope as an implicit receiver
// to call startBusinessOperation()
startBusinessOperation()
}
}
傳遞參數(shù)的發(fā)過誓可以直接在里面寫setFreeCompilerArgs(["-opt-in=org.mylibrary.OptInAnnotation"])
文章來源:http://www.zghlxwxcb.cn/news/detail-724210.html
2、其余平臺的配置
除了上述配置還可以使用以下配置,該配置可以在其余平臺使用:文章來源地址http://www.zghlxwxcb.cn/news/detail-724210.html
android {
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
kotlinOptions {
jvmTarget = '1.8'
apiVersion = '1.1'
languageVersion = '1.1'
}
}
二、參考鏈接:
- Kotlin 1.6.20 的新特性
- Kotlin Gradle plugin 中的編譯器選項
- IDE highlighting: False positive error “Context receivers should be enabled explicitly”
- compileKotlin block in build.gradle file throws error “Could not find method compileKotlin() for arguments […]”
到了這里,關于android中gradle的kotlin編譯配置選項的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!