Android Gradle Kotlin Dsl 中未解决的参考问题

Unresolved reference Issue in Android Gradle Kotlin Dsl

我将 gradle 文件从 groovy 迁移到了 kotlin dsl。一切正常,但从另一个 kotlin 文件向 gradle 导入值时出现问题。我可以导入变量并且自动完成功能正常,但是当我构建项目时,它会给我 Unresolved reference 错误。 感谢任何帮助。

Gradle 版本:7.1.1

Android工作室:北极狐

我在 buildSrc 下创建的

build.gradle.kts :

plugins {
    `java-gradle-plugin`
    `kotlin-dsl`
    `kotlin-dsl-precompiled-script-plugins`
}

repositories {
    mavenCentral()
}

我从 buildSrc/src/main/java/Dependencies 中导入的文件:

object Versions {
    const val compose_version = "1.0.0"
}

object Dependencies {
    const val compose_ui = "androidx.compose.ui:ui:${Versions.compose_version}"
    const val compose_material = "androidx.compose.material:material:${Versions.compose_version}"
    const val compose_ui_tooling_preview = "androidx.compose.ui:ui-tooling-preview:${Versions.compose_version}"
    const val compose_ui_tooling = "androidx.compose.ui:ui-tooling:${Versions.compose_version}"
    const val compose_ui_test = "androidx.compose.ui:ui-test-junit4:${Versions.compose_version}"
}

应用程序 build.gradle.kts 文件:

plugins {
    id("com.android.application")
    kotlin("android")
}

android {
    compileSdk = 30
    defaultConfig {
        applicationId = "com.example.compose"
        minSdk = 21
        targetSdk = 30
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }

    buildTypes {
        debug {
            isMinifyEnabled = false
            isDebuggable = true
        }
        release {
            isMinifyEnabled = true
            isDebuggable = 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"
    }
    buildFeatures {
        compose = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.0.0"
    }
    packagingOptions {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

dependencies {
    implementation("androidx.core:core-ktx:1.6.0")
    implementation("androidx.appcompat:appcompat:1.3.1")
    implementation("com.google.android.material:material:1.4.0")

    // Jetpack Compose
     implementation(Dependencies.compose_ui)
     implementation(Dependencies.compose_material)
     implementation(Dependencies.compose_ui_tooling_preview)
     debugImplementation(Dependencies.compose_ui_tooling)
     androidTestImplementation(Dependencies.compose_ui_test)

    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.3.1")
    implementation("androidx.activity:activity-compose:1.3.0")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.3")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")
}

你说你的另一个文件在buildSrc/src/main/java/Dependencies下。由于这是一个 Kotlin 文件,因此它应该是 buildSrc/src/main/kotlin/Dependencies