Ehsan Msz
Ehsan Msz

Reputation: 2164

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

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

Upvotes: 38

Views: 19935

Answers (3)

georgehardcore
georgehardcore

Reputation: 1055

If you really need it:

    defaultConfig {

    minSdk = libs.versions.minSdk.get().toInt()
    targetSdk = libs.versions.compileSdk.get().toInt()
    buildConfigField("int", "VERSION_CODE", libs.versions.appVerCode.get())
    buildConfigField("String", "VERSION_NAME", "\"${libs.versions.appVersion.get()}\"")
}

Upvotes: 1

Mohammad Reisi
Mohammad Reisi

Reputation: 116

Remove versionCode and versionName from library module and check again

Upvotes: 2

laalto
laalto

Reputation: 152797

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.

Upvotes: 61

Related Questions