getByName(“release”) {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile(“proguard-android-optimize.txt”), “proguard-rules.pro”)
}
}
指定 JDK 改造
Groovy 语法的指定 JDK 如下所示:
compileOptions {
sourceCompatibility JavaVersion . VERSION_1_8
targetCompatibility JavaVersion . VERSION_1_8
}
kotlinOptions {
jvmTarget = ‘1.8’
}
利用 Kotlin 可以改造成下面这样:
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = “1.8”
}
依赖的改造
Groovy 语法的依赖如下所示:
dependencies {
implementation “org.jetbrains.kotlin:kotlin-stdlib:1.4.31”
implementation ‘androidx.core:core-ktx:1.2.0’
implementation ‘androidx.appcompat:appcompat:1.1.0’
implementation ‘com.google.android.material:material:1.1.0’
implementation ‘androidx.constraintlayout:constraintlayout:1.1.3’
}
利用 Kotlin 可以改造成下面这样:
dependencies {
implementation(“org.jetbrains.kotlin:kotlin-stdlib:1.4.31”)
implementation(“androidx.core:core-ktx:1.2.0”)
implementation(“androidx.appcompat:appcompat:1.1.0”)
implementation(“com.google.android.material:material:1.1.0”)
implementation(“androidx.constraintlayout:constraintlayout:1.1.3”)
}
完整代码展示
到此为止,我们已经将自动生成 app/build.gradle 里面的内容全部改造完了,我们来看下完整代码,如下所示:
plugins {
id(“com.android.application”)
kotlin(“android”)
kotlin(“android.extensions”)
}
android {
compileSdkVersion(30)
buildToolsVersion(“30.0.3”)
defaultConfig {
applicationId = “com.example.kotlingradledsl”
minSdkVersion(23)
targetSdkVersion(30)
versionCode = 1
versionName = “1.0”
testInstrumentationRunner = “androidx.test.runner.AndroidJUnitRunner”
}
buildTypes {
getByNam