文章目录
- gradle--groovy-dsl和kotlin-dsl对比
-
- 常用对比
- 插件引用对比
- gradle脚本引用对比
- 任务task对比
-
- 仓库对比
- 依赖对比
- groovy的ext和kotlin的extra
- 示例
-
- 参考
gradle–groovy-dsl和kotlin-dsl对比
- 官方文档:Migrating build logic from Groovy to Kotlin
- 官方demo:kotlin-dsl-samples/samples at master · gradle/kotlin-dsl-samples · GitHub
- android相关的迁移可以查阅:将构建配置从 Groovy 迁移到 KTS | Android 开发者 | Android Developers
- 对于想使用kotlin的小伙伴来说,经常会遇到groovy的这个东西kotlin该怎么写的烦恼,为此本文总结一些常用的对比
常用对比
- groovy可以使用单引号和双引号,而kotlin只能使用双引号
- groovy在函数调用时可以省略括号,而kotlin必须加上括号
- groovy在赋值时可以省略等于号,而kotlin必须加上等号
- 为了减少迁移成本,在groovy时就应该约定使用双引号,调用加上括号,使用等号赋值
插件引用对比
- Groovy DSL有两种方式去引用插件:
- 1 plugins{} //强烈推荐
- 2 apply plugin
- 注意,核心插件可以用短名称,非核心插件必须声明id和version(非核心插件地址:Gradle - Plugins)
plugins {
id 'java'
id 'jacoco'
id 'maven-publish'
id 'org.springframework.boot' version '2.4.1'
}
plugins {
java
jacoco
`maven-publish`
id("org.springframework.boot") version "2.4.1"
}
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'org.springframework.boot'
apply(plugin = "java")
apply(plugin = "jacoco")
apply(plugin = "org.springframework.boot")
apply<ExamplePlugin>()
gradle脚本引用对比
apply from: 'other.gradle'
apply(from = "other.gradle.kts")
任务task对比
配置任务
tasks.jar {
archiveFileName = 'foo.jar'
}
tasks.named('jar') {
archiveFileName = 'foo.jar'
}
tasks.getByName('jar') {
archiveFileName = 'foo.jar'
}
tasks.named('test') {
useJUnitPlatform()
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
task sourcesJar(type: Jar, dependsOn: classes) {
archiveClassifier = 'sources'
from sourceSets.main.allSource
}
tasks.jar {
archiveFileName.set("foo.jar")
}
tasks.named<Jar>("jar") {
archiveFileName.set("foo.jar")
}
tasks.getByName<Jar>("jar") {
archiveFileName.set("foo.jar")
}
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.withType<JavaCompile> {
options.encoding = "UTF-8"
}
val sourcesJar by tasks.registering(Jar::class) {
dependsOn("classes")
archiveClassifier.set("sources")
from(sourceSets["main"].allSource)
}
创建任务
task greeting {
doLast { println 'Hello, World!' }
}
tasks.create('greeting') {
doLast { println('Hello, World!') }
}
tasks.register('greeting') {
doLast { println('Hello, World!') }
}
tasks.register('docZip', Zip) {
archiveFileName = 'doc.zip'
from 'doc'
}
tasks.create(name: 'docZip', type: Zip) {
archiveFileName = 'doc.zip'
from 'doc'
}
task clean(type: Delete) {
delete rootProject.buildDir
}
task("greeting") {
doLast { println("Hello, World!") }
}
tasks.create("greeting") {
doLast { println("Hello, World!") }
}
tasks.register("greeting") {
doLast { println("Hello, World!") }
}
tasks.register<Zip>("docZip") {
archiveFileName.set("doc.zip")
from("doc")
}
tasks.create<Zip>("docZip") {
archiveFileName.set("doc.zip")
from("doc")
}
tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
仓库对比
repositories {
mavenLocal()
google()
mavenCentral()
maven { url 'https://www.jitpack.io' }
}
repositories {
mavenLocal()
google()
mavenCentral()
maven("https://www.jitpack.io")
}
依赖对比
plugins {
id 'java-library'
}
dependencies {
implementation 'com.example:lib:1.1'
runtimeOnly 'com.example:runtime:1.0'
testImplementation('com.example:test-support:1.3') {
exclude(module: 'junit')
}
testRuntimeOnly 'com.example:test-junit-jupiter-runtime:1.3'
}
plugins {
`java-library`
}
dependencies {
implementation("com.example:lib:1.1")
runtimeOnly("com.example:runtime:1.0")
testImplementation("com.example:test-support:1.3") {
exclude(module = "junit")
}
testRuntimeOnly("com.example:test-junit-jupiter-runtime:1.3")
}
groovy的ext和kotlin的extra
- ExtraPropertiesExtension - Gradle DSL Version 7.4.2
- groovy我们可以用ext来统一管理变量
- 而kotlin则变为extra,但统一管理变量还用buildSrc更好一些
...
ext {
min_sdk = 21
target_sdk = 31
}
android {
defaultConfig {
minSdk min_sdk
targetSdk rootProject.target_sdk
}
}
...
rootProject.extra["min_sdk"] = 21
extra["target_sdk"] = 31
val min_sdk: Int by rootProject.extra
android {
defaultConfig {
minSdk = rootProject.extra["min_sdk"] as Int
targetSdk = rootProject.properties["target_sdk"] as Int
}
}
- 假如工程里groovy和kotln都有,那么kotlin可以通过 rootProject.extra[“min_sdk”] 或rootProject.properties[“target_sdk”] 得到groovy主工程中定义的ext,如下
ext {
min_sdk = 21
target_sdk = 31
}
示例
groovy
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "My Application"
include ':app'
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.4"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
ext{
min_sdk=21
target_sdk =31
}
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.xxx.myapplication"
minSdk min_sdk
targetSdk rootProject.target_sdk
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
kotlin
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "My Application"
include(":app")
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle:7.0.4")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20")
}
}
tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
extra["min_sdk"] = 21
extra["target_sdk"] = 31
plugins {
id("com.android.application")
id("kotlin-android")
}
android {
compileSdk = 31
defaultConfig {
applicationId = "com.xxx.myapplication"
minSdk = rootProject.properties["min_sdk"] as Int
targetSdk = rootProject.properties["target_sdk"] as Int
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation (fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation("androidx.core:core-ktx:1.2.0")
implementation("androidx.appcompat:appcompat:1.4.1")
implementation("com.google.android.material:material:1.5.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.3")
testImplementation("junit:junit:4.+")
androidTestImplementation("androidx.test.ext:junit:1.1.3")
androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")
}
参考
- Migrating build logic from Groovy to Kotlin
- (6条消息) 使用Kotlin DSL构建Android项目_blue_zy的博客-CSDN博客
- 再见,Groovy! 教你如何使用Kotlin SDL 编写Gradle脚本! - JavaShuo