【基于kotlin的Gradle Android】Android Gradle 的配置从 Groovy 迁移到 KTS【kotlin】

官方文档Gradle-kotlin:https://docs.gradle.org/current/userguide/kotlin_dsl.html

参考第三方文章:https://blog.csdn.net/qq_20613731/article/details/119766219

全局配置文件:settings.gradle.kts

// 引入app项目中的【build.gradle】配置文件
include(":app")
// 编译全局项目的【build.gradle】配置文件
rootProject.buildFileName = "build.gradle.kts"
rootProject.name = "KotlinGradleAppTemplate"

全局配置文件build.gradle.kts

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        maven(uri("https://maven.aliyun.com/repository/public/"))
        google()
        mavenCentral()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:7.0.4")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10")

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven(uri("https://maven.aliyun.com/repository/public/"))
        google()
        mavenCentral()
    }
}

tasks {
    val clean by registering(Delete::class) {
        delete(buildDir)
    }
}

项目app配置文件build.gradle.kts

plugins {
    /**
     * 1、android的属性配置要生效【这里必须是基于com.android.application】的插件
     * 1、系统默认的application实例不是基于【android】的插件,为此Android开发不能使用默认的!
     */
    id("com.android.application")
    kotlin("android")
}

/**
 * 1、安卓属性配置
 * 2、这里的属性要支持kts配置,必须引入安卓的application插件【com.android.application】
 */
android {
    compileSdk = 32

    defaultConfig {
        applicationId = "com.example.kotlingradleapptemplate"
        minSdk = 21
        targetSdk = 32
        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("androidx.core:core-ktx:1.3.2")
    implementation("androidx.appcompat:appcompat:1.2.0")
    implementation("com.google.android.material:material:1.3.0")
    implementation("androidx.constraintlayout:constraintlayout:2.0.4")
    testImplementation("junit:junit:4.+")
    androidTestImplementation("androidx.test.ext:junit:1.1.2")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.3.0")
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值