• 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
  • 相关阅读:
    超好用!在线即可制作电子产品图册
    OceanBase获奖!蚂蚁集团第三次入选世界互联网领先科技成果
    YOLOv6-4.0部分代码阅读笔记-loss_fuseab.py
    光学红外雨量IFR202型传感器智慧检测雨量场景等行业
    航空科普VR大型体验馆设备VR航天主题乐园星际飞碟vr游乐设备
    Unity 3D 简易对象池
    STM32G0 USB DFU 升级校验出错-2
    神经网络梯度是什么意思,神经网络中梯度的概念
    什么是电子设备的老化测试?
    Hive存储格式之RCFile详解,RCFile的过去现在和未来
  • 原文地址:https://blog.csdn.net/github_34790294/article/details/122506907