Error: "Unresolved reference: versionCode" in build.gradle.kts

Error: "Unresolved reference: versionCode" in build.gradle.kts

我正在尝试向我的项目添加一个新模块(库模块),但是在 build.gradle.kts(对于库模块)中我遇到了这个错误:

org.gradle.internal.exceptions.LocationAwareException: Build file ' ... /build.gradle.kts' line: 13
Script compilation errors:

  Line 13:         versionCode = 1
                   ^ Unresolved reference: versionCode

  Line 14:         versionName = "1.0"
                   ^ Unresolved reference: versionName

.
.
.

Caused by: ScriptCompilationException( ... )

build.gradle.kts

plugins {
    id("com.android.library")
    id("kotlin-android")
}

android {
    compileSdk = 30
    buildToolsVersion = "30.0.3"

    defaultConfig {
        minSdk = 21
        targetSdk = 30
        versionCode = 1       //error: Unresolved reference
        versionName = "1.0"   //error: Unresolved reference

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles("consumer-rules.pro")
    }

    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"
    }
}

如有任何帮助,我将不胜感激

versionCodeversionName 在库模块中没有意义。仅在应用程序模块中使用它们。

从长远来看,Google 计划在某些版本的 Android Gradle 插件中将它们从 DSL 中完全删除:

In a future version of Android Gradle plugin, the versionName and versionCode properties will also be removed from the DSL for libraries.

(source)

如果您使用的是 AGP 的早期非稳定版本,您可能已经进行了此更改。 Android Studio 中的代码模板并不总是与工具更改同步。

从库模块中删除 versionCode 和 versionName 并再次检查