Gradle build error after updating android studio: NumberFormatException for versionCode

Viewed 1303

I got an error I had not before:

Execution failed for task ':app:generateDebugBuildConfig'.
> Failed to calculate the value of task ':app:generateDebugBuildConfig' property 'buildConfigPackageName'.
   > Failed to query the value of property 'packageName'.
      > java.lang.NumberFormatException: For input string: "${versionCode}"

The android studio version is:

Android Studio 4.1.1
Build #AI-201.8743.12.41.6953283, built on November 5, 2020
Runtime version: 1.8.0_242-release-1644-b3-6222593 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o

The build.gradle(:app) file contains the following:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt' //< for image visualization with glide

//Serialization
apply plugin: 'kotlinx-serialization'


android {
    signingConfigs {
        release {
            storeFile file('xxxx.jks')
            storePassword 'xxxxxxx'
            keyPassword 'xxxxxxxx'
            keyAlias 'my-app'
        }
    }
    compileSdkVersion propCompileSdkVersion
    buildToolsVersion propBuildToolsVersion

    defaultConfig {
        applicationId "com.user.myapp"
        minSdkVersion propMinSdkVersion
        targetSdkVersion propTargetSdkVersion
        versionCode 3
        versionName "2.0-beta.26"
        manifestPlaceholders = [versionCode:"3", versionName:"2.0-beta.26"]
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        debug {
            applicationIdSuffix ".debug"
            debuggable true
            resValue "string", "app_name", "myapp-dev"
        }

        optimizedDebug {
            initWith debug
            minifyEnabled false
            applicationIdSuffix ".optimizedDebug"
            debuggable false
            resValue "string", "app_name", "myapp-optimized-dev"
        }

        debugProd {
            initWith debug
            minifyEnabled false
            applicationIdSuffix ".debugProd"
            debuggable true
            resValue "string", "app_name", "myapp-logs-in-prod"
        }

        release {
            minifyEnabled false
            resValue "string", "app_name", "myapp"
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
    }

    applicationVariants.all { variant ->
        variant.resValue "string", 'images_file_path', "Android/data/\"${applicationId}\"/files/Pictures"
    }

    /* Necessary to have Parcelable / @Parcelize functionalities */
    androidExtensions {
        experimental = true
    }


    lintOptions {
        checkReleaseBuilds false
        abortOnError true
    }
}

dependencies {
    ...
}

repositories {
    mavenCentral()
}

I have invalidated the caches and restarted several times, nothing changes. I would like to pinpoint exactly what happens, to at least be able to understand what causes gradle to be confused. Can anyone point me to a step by step run of gradle to find what is the problem ?

1 Answers

I found that in the manifest I had the following error annotated by android studio:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest
        xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.user.myapp"
        android:versionCode={$versionCode}
        android:versionName={$versionName}
        >
        ...
    </manifest>

It looks that in the 4.1.1 release of android studio, android:versionCode={$versionCode} is not working anymore.

This post https://commonsware.com/blog/2020/10/14/android-studio-4p1-library-modules-version-code.html says that we should no more use VERSION_CODE and VERSION_NAME. So I remove them entirely from my code and replaced it with custom buildConfigField gradle variables:

in gradle:

    defaultConfig {
        ...
        buildConfigField 'int', 'MYAPP_VERSION_CODE', "3"
        buildConfigField 'String', 'MYAPP_VERSION_NAME', "\"2.0-beta.26\""
        ...
    }

in manifest remove the part referencing android:versionXXXX:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest
        xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.user.myapp"
        >
        ...
    </manifest>

in kotlin code to display code and name version:

    val versionCode = BuildConfig.MYAPP_VERSION_CODE.toString()
    val versionName = BuildConfig.MYAPP_VERSION_NAME

It works nicely after that.

Related