• gradle迁移到gradle.kts(复制可用)


    为什么要升级到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。下面将依次对这几个文件的文件内容进行修改。

    setting.gradle

    原文件:

    pluginManagement {
        repositories {
            gradlePluginPortal()
            google()
            mavenCentral()
        }
    }
    dependencyResolutionManagement {
        repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
        repositories {
            google()
            mavenCentral()
        }
    }
    rootProject.name = "Test"
    include ':app'
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    修改后的文件:

    pluginManagement {
        repositories {
            gradlePluginPortal()
            google()
            mavenCentral()
        }
    }
    dependencyResolutionManagement {
        repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
        repositories {
            google()
            mavenCentral()
        }
    }
    rootProject.name = "Test"
    include ("app")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    总结:基本无变化,只是将最后 include ':app’变成了include (“app”),其中include是个方法名,"app"是参数,因此这里可以总结出两个知识点:

    1 .字符串使用必须使用双引号
    2. 方法的调用由原来的方法名 参数 变成了 方法名(参数)

    Project的build.gradle

    原文件:

    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
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    修改后的文件:

    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)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    总结:

    1. buildscript ->dependencies ->classpath 的赋值使用添加了"()", 由此可见kts中字符串赋值需要将 'xxx’或"xxx"更改为(“xxx”)
    2. task定义更改,由 task 名称(type 类型)更改为task(“名称”,类型::class)

    Module的build.gradle

    原文件:

    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'
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    修改后的文件:

    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")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
  • 相关阅读:
    时间戳转换为正常时间
    为了方便问题的快速定位,如您在使用
    【Java面试】List接口
    齐岳|近红外染料CY7.5标记PCL聚已内酯纳米载体CY7.5-PCL|PCL-CY7.5|CY7.5-PEG-PCL
    计算机视觉——图像视觉显著性检测
    Redis数据库的事物机制(详细讲解)
    阈值同态加密在隐私计算中的应用:解读
    百题千解计划【CSDN每日一练】环形单向链表。给一个单向链表,若其中包含环,请完善EntryNodeOfLoop方法..| 附多种解决方式:Python、JavaScript、Java、Go、C#..
    python selenium如何带cookie访问网站
    【docker专栏4】使用docker安装nginx提供web服务
  • 原文地址:https://blog.csdn.net/github_34790294/article/details/122506907