skip to Main Content

I am using Kotlin 2.0.0. I was trying in my current android project. I was trying from this video.

class MainActivity : ComponentActivity() {

    val state: StateFlow<Int>
        field = MutableStateFlow(0)


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            AndroidTheme {
                SignInScreenRoute()
            }
        }
    }
}

It giving me error

Explicit backing field declarations are not supported in FE 1.0

libs.versions.toml

[versions]
androidGradlePlugin = "8.4.1"
kotlin = "2.0.0"
coreKtx = "1.13.1"
junit = "4.13.2"
junitVersion = "1.1.5"
espressoCore = "3.5.1"
lifecycleRuntimeKtx = "2.8.0"
activityCompose = "1.9.0"
composeBom = "2024.05.00"
navigationCompose = "2.7.7"
koinAndroid = "3.5.6"
koinAndroidxCompose = "3.5.6"
ktor = "2.3.10"
appcompat = "1.6.1"
material = "1.12.0"
serialization = "1.6.3"
googleFont = "1.6.7"

[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
androidx-ui = { group = "androidx.compose.ui", name = "ui" }
androidx-foundation = { group = "androidx.compose.foundation", name = "foundation" }
androidx-foundation-layout = { group = "androidx.compose.foundation", name = "foundation-layout" }
androidx-runtime = { group = "androidx.compose.runtime", name = "runtime" }
androidx-lifecycle-viewmodel-compose = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-compose" }
androidx-navigation = { group = "androidx.navigation", name = "navigation-compose", version.ref = "navigationCompose" }
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
androidx-material_icons = { group = "androidx.compose.material", name = "material-icons-extended" }
koin-android = { group = "io.insert-koin", name = "koin-android", version.ref = "koinAndroid" }
koin-androidx-compose = { group = "io.insert-koin", name = "koin-androidx-compose", version.ref = "koinAndroidxCompose" }
ktor-client-okhttp = { group = "io.ktor", name = "ktor-client-okhttp", version.ref = "ktor" }
ktor-client-logging = { group = "io.ktor", name = "ktor-client-logging", version.ref = "ktor" }
ktor-client-content-negotiation = { group = "io.ktor", name = "ktor-client-content-negotiation", version.ref = "ktor" }
ktor-client-core = { group = "io.ktor", name = "ktor-client-core", version.ref = "ktor" }
ktor-serialization-kotlinx-json = { group = "io.ktor", name = "ktor-serialization-kotlinx-json", version.ref = "ktor" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "serialization" }
google-font= { group = "androidx.compose.ui", name = "ui-text-google-fonts", version.ref = "googleFont" }

[plugins]
androidApplication = { id = "com.android.application", version.ref = "androidGradlePlugin" }
jetbrainsKotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
androidLibrary = { id = "com.android.library", version.ref = "androidGradlePlugin" }
serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version. ref = "kotlin" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }

build.gradle.kts(:app)

plugins {
    alias(libs.plugins.androidApplication)
    alias(libs.plugins.jetbrainsKotlinAndroid)
    alias(libs.plugins.compose.compiler)
}

android {
    namespace = "com.hub.abc"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.hub.abc"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }
    buildFeatures {
        compose = true
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

dependencies {

    implementation(project(":network"))
    implementation(project(":core:coroutines"))
    implementation(project(":core:ui"))
    implementation(project(":feature:signup:ui"))
    implementation(project(":feature:signup:data"))

    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(libs.androidx.ui)
    implementation(libs.androidx.foundation)
    implementation(libs.androidx.foundation.layout)
    implementation(libs.androidx.runtime)
    implementation(libs.androidx.lifecycle.viewmodel.compose)
    implementation(libs.androidx.navigation)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)
    implementation(libs.koin.android)
    implementation(libs.koin.androidx.compose)

    implementation(libs.serialization.json)

    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.ui.test.junit4)
    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)
}

2

Answers


  1. This feature is still experimental and needs to be explicitly activated. Add this to your gradle file:

    kotlin {
        sourceSets.all {
            languageSettings.enableLanguageFeature("ExplicitBackingFields")
        }
    }
    

    Now it should work.

    In the case of MutableStateFlow it was a bit wonky for me: Initially I had to explicitly use the fully qualified function name for the backing field, although I did import kotlinx.coroutines.flow.MutableStateFlow:

    val state: StateFlow<Int>
        field = kotlinx.coroutines.flow.MutableStateFlow(0)
    

    That went away after some time, though: I have no clue what changed, but now it works as one expects, just with field = MutableStateFlow(0) and I cannot reproduce the initial problem I had anymore.

    What I still didn’t get to work was the combined usage with an initializer, like this:

    val state: StateFlow<Int> = field.asStateFlow()
        field = MutableStateFlow(0)
    

    This doesn’t seem to be available in 2.0.0, not even as an experimental feature.

    As a closing note: Keep in mind that all of this only works with the new K2 compiler. That will be used for compiling your code when you run a gradle build, but by default it isn’t used by your IDE for syntax highlighting, code completion and such. That’s also the reason why the code will be highlighted as an error, even when it will successfully compile when the experimetal flag is set. If you want to also use K2 for your IDE follow these steps: https://kotlinlang.org/docs/whatsnew20.html#support-in-ides

    Login or Signup to reply.
  2. Yupe just tried also on my side and aint working, even with the explicit activation

    kotlin {
        sourceSets.all {
            languageSettings.enableLanguageFeature("ExplicitBackingFields")
        }
    }
    

    Android studio was showing some error around the field

    val eventFlow: SharedFlow<Int>
        field = MutableSharedFlow()
    

    But was able so compile. Struggle came in when I wanted to actually access the field…

    eventFlow.update { }
    

    That was not compiling anymore… I guess we’re gonna have to be more patient for the backing field feature

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search