Build Gradle 返回结果随 KTS 扩展一起出现

build gradle returns appears with kts extension

提问人:Ukeme Elijah 提问时间:11/17/2023 更新时间:11/21/2023 访问量:20

问:

我最近将我的 Android Studio 更新到 Canary11 版本,当我创建一个新的 java 项目时,我刚刚发现 build.gradles 具有 kts 扩展名,就目前而言,除了我直接从 IDE 添加的 firebase own。我不知道为什么它会出现这样,我真的需要有关如何解决它的帮助。就是这样。

    build.gradle.kts(app)
    
    @Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
    plugins {
        alias(libs.plugins.androidApplication)
        id("com.google.gms.google-services")
    }
    
    android {
        namespace = "com.elijahukeme.quizapp"
        compileSdk = 34
    
        defaultConfig {
            applicationId = "com.elijahukeme.quizapp"
            minSdk = 21
            targetSdk = 34
            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
        }
    }
    
    dependencies {
    
        implementation(libs.appcompat)
        implementation(libs.material)
        implementation(libs.constraintlayout)
        implementation(libs.firebase.auth)
        implementation(libs.firebase.database)
        implementation(libs.firebase.firestore)
        implementation(libs.firebase.storage)
        testImplementation(libs.junit)
        androidTestImplementation(libs.androidx.test.ext.junit)
        androidTestImplementation(libs.espresso.core)
    }
    
    build.gradle.kts(project)
    
    buildscript {
        dependencies {
            classpath(libs.google.services)
        }
    }
    // Top-level build file where you can add configuration options common to all sub-projects/modules.
    @Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
    plugins {
        alias(libs.plugins.androidApplication) apply false
    }
    true // Needed to make the Suppress annotation work for the plugins block
    
    
    these are the dependencies that I wanted to add
    def nav_version = "2.7.5"
    
        // Java language implementation
        implementation "androidx.navigation:navigation-fragment:$nav_version"
        implementation "androidx.navigation:navigation-ui:$nav_version"
安卓 build.gradle

评论

0赞 CommonsWare 11/17/2023
“除了直接从 IDE 添加的 firebase 之外,我无法添加任何其他外部依赖项” -- Gradle 的构建脚本支持 Groovy () 和 Kotlin ()。Gradle 支持使用这两种语言的依赖项。请参阅文档,其中涵盖了 和 。build.gradlebuild.gradle.ktsbuild.gradlebuild.gradle.kts
0赞 Ukeme Elijah 11/17/2023
我已经浏览了文档,但它仍然没有给我如何添加新依赖项的范围,因为 .kts 的出现方式对我来说有点奇怪,我无法添加任何新的依赖项,我还想知道是否有办法将其恢复为我习惯@CommonsWare的正常 build.gradle
1赞 CommonsWare 11/17/2023
当然,欢迎您用等效文件替换文件。例如,您可以从某个较旧的项目中复制它们。或者,您可以考虑改回 Android Studio 的稳定版本,而不是 Canary 构建版本,因为 Giraffe 默认仍使用 Groovy 构建脚本。或者,您可能会看到 Canary 构建新项目向导是否为您提供了 Groovy 或 Kotlin 构建脚本的选择。build.gradle.ktsbuild.gradle

答:

0赞 Sergei Kozelko 11/21/2023 #1

您看到的是 Gradle 版本目录:Android 开发者文档、Gradle 文档。这可以与 Kotlin 和 Groovy 构建脚本一起使用。

您应该能够混合直接添加的依赖项和通过版本目录添加的依赖项,而不会出现问题,但您需要在文件中使用 Kotlin 语法:.kts

dependencies {
    val nav_version = "2.7.5"
    
    // Java language implementation
    implementation("androidx.navigation:navigation-fragment:$nav_version")
    implementation("androidx.navigation:navigation-ui:$nav_version")
}

或者,您可以将依赖项添加到版本目录中:gradle/libs.versions.toml

[versions]
nav_version = "2.7.5"

[libraries]
androidx-navigation-fragment = { group = "androidx.navigation", name = "navigation-fragment", version.ref = "nav_version" }
androidx-navigation-ui = { group = "androidx.navigation", name = "navigation-ui", version.ref = "nav_version" }

然后引用它们build.gradle.kts

dependencies {
    implementation(libs.androidx.navigation.fragment)
    implementation(libs.androidx.navigation.ui)
    // or use the automatic grouping
    // implementation(libs.androidx.navigation)
}