Skip to content

BuildConfig values are not available during annotation process after upgrading ksp from 2.2.10-2.0.2 -> 2.2.20-2.0.3 #2597

@RoarGronmo

Description

@RoarGronmo

My post in issuetracker: https://issuetracker.google.com/444480432

Thought it was an error in AS first, but it seems to have something to do with the annotation processing not being able to read BuildConfig values in ksp v2.2.20-2.0.3. This works as expected in v2.2.10-2.0.2.

Where did it appear ?
In a @Database annotation (like this one)

@Database(
        entities = [
            UserEntity::class,
...
            AssignmentControlFormCheckPointDataEntity::class,
        ],
        autoMigrations = [
            AutoMigration(from = 163, to=169),
...
            AutoMigration(from = 190, to=200)
        ],
        version = BuildConfig.APPDATABASEVERSION, //<-- Reading values defined in gradle 
        exportSchema = true
)

Error message during compile:

Image

How is it defined in the gradle file ?
Below is just the android part of the app's build.gradle.kts

android {
    namespace = "no.norva24.mslam"

    //Signing configs in this app

    signingConfigs {
        //create("debug")
        create("release")
    }

    //Signing keys
    if(project.hasProperty("AndroidProject.signing") &&
        File(project.property("AndroidProject.signing").toString()).exists()
    ) {
        val props = Properties()
        val propFile = File(project.property("AndroidProject.signing").toString())
        if(propFile.canRead())
        {
            props.load(FileInputStream(propFile))

            if(props.containsKey("RELEASE_STORE_FILE") &&
                    props.containsKey("RELEASE_STORE_PASSWORD") &&
                    props.containsKey("RELEASE_KEY_ALIAS") &&
                    props.containsKey("RELEASE_KEY_PASSWORD") &&
                    props.containsKey("DEBUG_STORE_FILE") &&
                    props.containsKey("DEBUG_STORE_PASSWORD") &&
                    props.containsKey("DEBUG_KEY_ALIAS") &&
                    props.containsKey("DEBUG_KEY_PASSWORD"))
            {
                signingConfigs.getByName("release").apply{
                    storeFile = file(props["RELEASE_STORE_FILE"] as String)
                    storePassword = props["RELEASE_STORE_PASSWORD"] as String
                    keyAlias = props["RELEASE_KEY_ALIAS"] as String
                    keyPassword = props["RELEASE_KEY_PASSWORD"] as String
                }
                signingConfigs.getByName("debug").apply {
                    storeFile = file(props["DEBUG_STORE_FILE"] as String)
                    storePassword = props["DEBUG_STORE_PASSWORD"] as String
                    keyAlias = props["DEBUG_KEY_ALIAS"] as String
                    keyPassword = props["DEBUG_KEY_PASSWORD"] as String
                }

            }
            else
            {
                throw Exception("Missing properties in: ${propFile.toPath()}")
            }

        }
        else
        {
            throw FileNotFoundException("Property file not found: ${propFile.toPath()}")
        }
    }

    //Maps properties file

    val mapProps = Properties()

    if(file("../maps.properties").exists())
    {
        file("../maps.properties").inputStream().use {mapProps.load(it)}
    }



    //Versioning, file for build#

    val versionPropsFile = file("version.properties")

    val versionMajor = 0 //Increment manually when public release ready
    val versionMinor = 13 //Increment manually, we have begun level 12 - schema registering
    val versionPrep = 194 //Increment manually for each public release //TODO: Remember to activate crashlytics !
    val versionAppDatabase = 200 //Increment manually for each db entity change
    var versionBuild:Int //Increments automatically from file

    if(versionPropsFile.canRead()) {
        val versionProps = Properties()
        versionProps.load(FileInputStream(versionPropsFile))
        versionBuild = versionProps["VERSION_BUILD"]?.toString()?.toInt() ?: 0
    } else {
        throw FileNotFoundException("Could not read version.properties !")
    }

    val autoIncrementBuildNumber = {
        if(versionPropsFile.canRead()) {
            val versionProps = Properties()
            versionProps.load(FileInputStream(versionPropsFile))
            versionBuild = versionProps["VERSION_BUILD"]?.toString()?.toInt()?.plus(1)?:0
            versionProps["VERSION_BUILD"] = versionBuild.toString()
            versionProps.store(versionPropsFile.writer(), null)
        } else {
            throw FileNotFoundException("Could not read/write version.properties!")
        }
    }


    class BuildNumberIncrementer : TaskExecutionGraphListener {
        override fun graphPopulated(taskGraph: TaskExecutionGraph) {
            if (taskGraph.hasTask(":app:extractApksForDebug") ||
                taskGraph.hasTask(":app:assembleDebug") ||
                taskGraph.hasTask(":app:assembleRelease") ||
                taskGraph.hasTask(":app:bundleRelease")
            ) {
                autoIncrementBuildNumber()
            }
        }
    }

    gradle.taskGraph.addTaskExecutionGraphListener(BuildNumberIncrementer())

    compileSdk = 36


    defaultConfig {
        applicationId = "no.norva24.mslam"
        minSdk = 29
        targetSdk = 36
        versionCode = versionPrep
        versionName = "$versionMajor.$versionMinor.$versionCode.$versionAppDatabase.$versionBuild"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

        buildConfigField("long","TIMESTAMP", "${System.currentTimeMillis()}L")
        buildConfigField("int","APPDATABASEVERSION",versionAppDatabase.toString())


    }




    buildTypes {
        getByName("release") {
            isMinifyEnabled = true
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
            resValue("string","google_maps_key",mapProps.getProperty("MAPS_API_KEY", ""))
        }
        getByName("debug") {
            resValue("string","google_maps_key",mapProps.getProperty("MAPS_API_KEY"," "))
        }
    }


    packaging {
        resources {
            excludes += setOf(
                "META-INF/DEPENDENCIES",
                "META-INF/versions/9/previous-compilation-data.bin"
            )
        }
    }
    //packagingOptions.resources.excludes.add('META-INF/versions/9/previous-compilation-data.bin')


    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_21
        targetCompatibility = JavaVersion.VERSION_21
    }
 
    buildFeatures {
        buildConfig = true
        dataBinding = true
        viewBinding = true
        compose = true
        resValues = true
    }
}

This value isn't available during the annotation process in 2.2.20-2.0.3 but is available in 2.2.10-2.0.2:
Image

Other of theese values, such as BuildConfig.TIMESTAMP is available throughout the compilation process, but not in annotations either.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions