android - Android Gradle Kotlin Dsl 中 Unresolved reference 问题

标签 android kotlin gradle 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

关于android - Android Gradle Kotlin Dsl 中 Unresolved reference 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68610161/

相关文章:

android - 在 Android 中使用带有证书的双向身份验证

android - 如何使用数据绑定(bind)设置单击监听器并将edittext字段值传递给 View 模型

intellij-idea - Intellij IDEA告诉我 “Compilation completed with 52 errors and 0 warnings”

kotlin - 如何将元素放入 map 并返回 map

gradle - 任务参数

android - React Native 是否带有 Gradle?

android - 在 Chrome 中运行的 Android 应用程序中使用媒体硬件按钮

android - 从 map fragment 切换到另一个 fragment 并返回

gradle - 如何将外部 jar 文件添加到 gradle 构建脚本

android - 如何在从另一个 Activity 调用一个 Activity 时延迟一些?