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

Viewed 5160

I'm trying to add a new module (library module) to my project but in build.gradle.kts (for library module) I have this error:

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

I would be grateful for any help

2 Answers

versionCode and versionName are meaningless in a library module. Use them only in the application module.

In the long term, Google plans to remove them from the DSL altogether in some version of Android Gradle Plugin:

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

(source)

If you are on an early non-stable version of AGP, you might have this change already. The code templates in Android Studio are not always in sync with the tooling changes.

Remove versionCode and versionName from library module and check again

Related