当前位置: 首页 > news >正文

android中gradle的kotlin编译配置选项

一、编译配置

1、Android中的配置

使用如下方式开启在Android中的gradle的kotlin编译配置:
该配置在其余平台不可用

android {...compileOptions {sourceCompatibility JavaVersion.VERSION_17targetCompatibility 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 receiverlog.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()}
}

传递参数的发过誓可以直接在里面写setFreeCompilerArgs(["-opt-in=org.mylibrary.OptInAnnotation"])

2、其余平台的配置

除了上述配置还可以使用以下配置,该配置可以在其余平台使用:

android {
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {sourceCompatibility = JavaVersion.VERSION_1_8targetCompatibility = JavaVersion.VERSION_1_8kotlinOptions {jvmTarget = '1.8'apiVersion = '1.1'languageVersion = '1.1'}
}

二、参考链接:

  1. Kotlin 1.6.20 的新特性
  2. Kotlin Gradle plugin 中的编译器选项
  3. IDE highlighting: False positive error “Context receivers should be enabled explicitly”
  4. compileKotlin block in build.gradle file throws error “Could not find method compileKotlin() for arguments […]”
http://www.lryc.cn/news/211990.html

相关文章:

  • 【知识串联】概率论中的值和量(随机变量/数字特征/参数估计)【考研向】【按概率论学习章节总结】(最大似然估计量和最大似然估计值的区别)
  • NOIP2023模拟6联测27 点餐
  • AMEYA360:类比半导体重磅发布车规级智能高边驱动HD7xxxQ系列
  • 【HarmonyOS】鸿蒙操作系统架构
  • JSON数据
  • 金融领域:怎么保持电力系统连续供应?
  • 批量重命名文件夹:用数字随机重命名法管理您的文件夹
  • RPC与HTTP的关系
  • OpenCV #以图搜图:感知哈希算法(Perceptual hash algorithm)的原理与实验
  • Android多张图片rotation旋转角度叠加/重叠堆放
  • HBuilderX 自定义语法提示
  • Leetcode—2562.找出数组的串联值【简单】
  • T0外部计数输入
  • 分治法求解棋盘覆盖问题
  • 爱写bug的小邓程序员个人博客
  • selenium判断元素可点击、可见、可选
  • 计算机网络重点概念整理-第六章 应用层【期末复习|考研复习】
  • html2pdf
  • css中页面元素隐藏
  • dp三步问题
  • 结构体和联合体嵌套访问
  • Linux ———— 管理磁盘
  • 文字的编码
  • 21.9 Python 使用Selenium库
  • C++初阶2
  • 网络安全(黑客)—小白自学
  • 在win10下,使用torchviz对深度学习网络模型进行可视化
  • 【自然语言处理】【长文本处理】RMT:能处理长度超过一百万token的Transformer
  • 交叉编译工具链(以STM32MP1为例)
  • 使用 Pyro 和 PyTorch 的贝叶斯神经网络