赞
踩
为什么要升级到gradle.kts? 很简单,就是因为gradle.kts带提示功能。这里将针对AndroidStudio的默认构建脚本进行升级。
Android Studio: Android Studio Arctic Fox | 2020.3.1 Patch 2
gradle: 7.0.2
gradle-plugin: 7.0.2
整个工程一共有3个gradle文件,setting.gradle、Project的build.gradle和Moudle的build.gradle,升级哪个文件就需要在文件名后添加kts, 如:setting.gradle.kts、build.gradle.kts。下面将依次对这几个文件的文件内容进行修改。
原文件:
pluginManagement { repositories { gradlePluginPortal() google() mavenCentral() } } dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } } rootProject.name = "Test" include ':app'
修改后的文件:
pluginManagement { repositories { gradlePluginPortal() google() mavenCentral() } } dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } } rootProject.name = "Test" include ("app")
总结:基本无变化,只是将最后 include ':app’变成了include (“app”),其中include是个方法名,"app"是参数,因此这里可以总结出两个知识点:
1 .字符串使用必须使用双引号
2. 方法的调用由原来的方法名 参数 变成了 方法名(参数)
原文件:
plugins {
id 'com.android.application' version '7.0.2' apply false
id 'com.android.library' version '7.0.2' apply false
id 'org.jetbrains.kotlin.android' version '1.6.20' apply false
}
task clean (type: Delete) {
delete rootProject.buildDir
}
修改后的文件:
plugins {
id("com.android.application") version "7.0.2" apply false
id("com.android.library") version "7.0.2" apply false
id("org.jetbrains.kotlin.android") version "1.6.20" apply false
}
task("clean", Delete::class) {
delete(rootProject.buildDir)
}
总结:
原文件:
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' } android { compileSdk 32 defaultConfig { applicationId "com.secoo.trytry" minSdk 17 targetSdk 32 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.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 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:2.0.4' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }
修改后的文件:
plugins { id("com.android.application") id("org.jetbrains.kotlin.android") } android { compileSdk = 32 defaultConfig { applicationId = "com.secoo.trytry" minSdk = 17 targetSdk = 32 versionCode = 1 versionName = "1.0" testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner" } buildTypes { getByName("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("com.android.support:appcompat-v7:28.0.0") implementation("com.android.support.constraint:constraint-layout:2.0.4") testImplementation("junit:junit:4.13.2") androidTestImplementation("com.android.support.test:runner:1.0.2") androidTestImplementation("com.android.support.test.espresso:espresso-core:3.0.2") }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。