gradle–groovy-dsl和kotlin-dsl对比
常用对比
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
...
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
}
独立gradle文件对比
在groovy中,当我们发现一个build.gradle文件比较庞大臃肿时,可以很方便地将其中部分内容独立成另一个xxx.gradle文件并用apply from
进行引用 然而当你使用kotlin-dsl时,你会发现上面的想法并不顺利,脚本根本无法正常执行,因为kotlin-dsl并不支持这样做,要实现类似的功能,还得使用预编译脚本插件Developing Custom Gradle Plugins
示例
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" )
}
参考