博客
关于我
《Kotin 极简教程》第12章 使用 Kotlin 集成Gradle 开发
阅读量:275 次
发布时间:2019-03-01

本文共 3840 字,大约阅读时间需要 12 分钟。

第12章 使用 Kotlin 集成 Gradle 开发

Kotlin 的功能 richness 和简洁的声明式语法使其成为构建脚本的理想选择,尤其是在与 Gradle 结合使用时。我们将深入探讨如何利用 Kotlin 来优化 Gradle 构建脚本,提升开发效率和代码可读性。

12.1 使用 Gradle 构建 Kotlin 工程

12.1.1 kotlin-gradle 插件配置

为了使用 Gradle 构建 Kotlin 项目,我们需要配置 kotlin-gradle 插件。以下是基本配置示例:

buildscript {    ext {        kotlinVersion = '1.1.3-2'    }    repositories {        mavenCentral()    }    dependencies {        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")    }    apply plugin: 'kotlin'}

此外,确保添加 Kotlin 标准库依赖:

dependencies {    compile("org.jetbrains.kotlin:kotlin-stdlib-jre8:${kotlinVersion}")    compile("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")}

12.1.2 Kotlin 与 Java 混合编程

Kotlin 代码可以与同一项目中的 Java 代码共存。默认配置下,源代码目录设置如下:

sourceSets {    main.kotlin.srcDirs += 'src/main/kotlin'    main.java.srcDirs += 'src/main/java'}

如果需要自定义目录布局,可以手动更新 sourceSets 属性。

12.1.3 Gradle JavaScript 项目配置

对于 JavaScript 项目,需要使用 kotlin2js 插件:

apply plugin: "kotlin2js"

默认生成的 JavaScript 文件可以通过 <code>kotlinOptions.metaInfo</code> 属性控制是否包含二进制描述符:

compileKotlin2Js {    kotlinOptions.metaInfo = true}

12.1.4 Gradle Android 项目配置

构建 Kotlin Android 项目时,需要使用 kotlin-android 插件:

buildscript {    ext.kotlin_version = '1.1.2-4'    repositories {        jcenter()    }    dependencies {        classpath('com.android.tools.build:gradle:2.3.1')        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")    }}

在 Android Studio 中,通常通过多项目配置管理子项目,确保每个子项目的 build.gradle.kts 文件正确引用插件。

12.1.5 Kotlin 标准库依赖管理

在项目中添加必要的 Kotlin 标准库依赖:

repositories {    mavenCentral()}dependencies {    compile("org.jetbrains.kotlin:kotlin-stdlib")}

对于 JDK 7/8 环境,可以选择相应的扩展版本:

dependencies {    compile("org.jetbrains.kotlin:kotlin-stdlib-jre7")    compile("org.jetbrains.kotlin:kotlin-stdlib-jre8")}

12.1.6 增量编译

从 Kotlin 1.1.1 开始,Gradle 支持增量编译:

compileKotlin {    kotlinOptions {        suppressWarnings = true    }}

12.2 使用 Kotlin 编写 Gradle 构建和配置文件

Gradle Script Kotlin 是 Gradle 团队与 Kotlin 团队合作开发的新语言,基于 Kotlin 脚本化构建配置文件。这种方式可以提高构建脚本的可读性和维护性。以下是一个完整的示例:

buildscript {    val kotlinVersion = "1.1.3-2"    val springBootVersion = "2.0.0.M2"    extra["kotlinVersion"] = kotlinVersion    repositories {        mavenCentral()        maven { setUrl("https://repo.spring.io/snapshot") }        maven { setUrl("https://repo.spring.io/milestone") }    }    dependencies {        classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")        classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion")    }    apply {        plugin("kotlin")        plugin("kotlin-spring")        plugin("eclipse")        plugin("org.springframework.boot")        plugin("io.spring.dependency-management")    }    version = "0.0.1-SNAPSHOT"    configure
{ setSourceCompatibility(1.8) setTargetCompatibility(1.8) } repositories { mavenCentral() maven { setUrl("https://repo.spring.io/snapshot") } maven { setUrl("https://repo.spring.io/milestone") } } dependencies { compile("org.springframework.boot:spring-boot-starter-actuator") compile("org.springframework.boot:spring-boot-starter-data-jpa") compile("org.springframework.boot:spring-boot-starter-freemarker") compile("org.springframework.boot:spring-boot-starter-web") compile("org.jetbrains.kotlin:kotlin-stdlib-jre8:${kotlinVersion}") compile("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}") runtime("org.springframework.boot:spring-boot-devtools") runtime("mysql:mysql-connector-java") testCompile("org.springframework.boot:spring-boot-starter-test") }}

本章小结

本章详细介绍了如何使用 Kotlin 集成 Gradle 开发项目,包括插件配置、源代码管理、JavaScript 和 Android 项目支持,以及基于 Kotlin 的构建脚本配置。通过这些配置,开发者可以充分发挥 Kotlin 的优势,提升构建脚本的可读性和维护性。

转载地址:http://clha.baihongyu.com/

你可能感兴趣的文章
Python:deepcopy(list) vs new_list = old_list[:]
查看>>
Python:Google API - 从消息中获取 mimeTypes
查看>>
Python:Google API - 从消息中获取 mimeTypes
查看>>
Python:subprocess.call,stdout to file,stderr to file,在屏幕上实时显示stderr
查看>>
Python:TypeError:无法连接“str“和“int“对象
查看>>
Python:XPath 在 ElementTree 中不可用
查看>>
Python:从 FTP 服务器下载文件
查看>>
python:从列表中创建元组列表
查看>>
Python:从嵌套列表中删除重复项
查看>>
Python:从间隔映射到值
查看>>
Python:使用 shell=False 的子进程调用不起作用
查看>>
Python:修改 PATH 环境变量的平台无关方式
查看>>
Python:假与 0
查看>>
Python:函数使用
查看>>
Python:函数进阶
查看>>
Python:在整数的二进制表示中查找最长的二进制间隙
查看>>
Python:如何从二进制转换为 base 64 并返回?
查看>>
Python:如何以非阻塞方式读取子进程的标准输出
查看>>
Python:如何使用 plotly 制作阴影区域或交替背景颜色?
查看>>
Python:如何根据日期时间获取值的计数
查看>>