29

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

dsh
  • 12,037
  • 3
  • 33
  • 51
Ehsan msz
  • 1,774
  • 2
  • 13
  • 26

2 Answers2

52

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.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • 1
    Thanks for the answer but this code is auto-generated by android studio and all library `build.gradle` files have the `versionCode` and `versionName` otherwise we can't set version name to libraries. – Ehsan msz Jun 02 '21 at 10:59
  • 3
    A little late, but the version phrases are indeed useless for libraries. Only used in releases. If you do need them in code, use `BuildConfigField` – Mahdi-Malv Oct 26 '21 at 13:16
  • You may also have to check you didn't apply `com.android.application` instead of `com.android.library` for a library module! It happened to me. – Mohsen Mirhoseini Jun 27 '23 at 11:37
0

Remove versionCode and versionName from library module and check again