Kotlin and Jetpack Compose Errors and Solutions

I decided to give Kotlin and Jetpack Compose a try but did not want to create a whole new project so I decided to add it to my test project. Now this project is old but like the name says its only a test project so who cares, well…. apparently Kotlin and Jetpack does because I ran some annoying errors so below is a two of the errors that I ran into and the existing solutions did not work. I will be adding to this list as my journey with Jetpack and Kotlin continues.

Error 1 Incompatible Kotlin versions

jetified-core-ktx-1.6.0-api.jar!/META-INF/core-ktx_release.kotlin_module: 
Module was compiled with an incompatible version of Kotlin. 
The binary version of its metadata is 1.5.1, expected version is 1.1.16

A few ways to resolve this one but ultimately they all boil down to targeting the right version for the “androidx.core:core-ktx:####” Module.

Right click on the root app folder and select “Open Module Settings”

When the Project structure screens opens click on “Dependencies” then “app” then look for the module named “core-ktx:#.#.#” in the requested version drop down select the correct version in the errors case above it should be core-ktx:1.1.6.

I ended up upgrading everything related to Kotlin so my version above does not match the error but this is the location where you can play around with the versions. I like this way better as it gives you a drop down list, but if you want you can type in the different versions into your build.gradle file.

Error 2 Compose Compiler

This version (1.2.0-alpha05) of the Compose Compiler requires Kotlin version 1.6.10 but you appear to be using Kotlin version 1.7.20 
which is not known to be compatible.  
Please fix your configuration (or `suppressKotlinVersionCompatibilityCheck` but don't say I didn't warn you!).

I spent a lot of time trying to figure out how to update the Compose compiler to resolve this as I was hell bent on not having to target a lower version of Kotlin because I just spend a day updating it to version 1.7.20 along with all the dependencies that broke by doing that.

Solution – if you dont have the highlighted lines below add them and make sure the version lines up with the Kotlin -> Compose Compiler compatibility map linked HERE.

android {
    compileSdkVersion 33

    defaultConfig {
        applicationId "com.st.androidtestapp"
        minSdkVersion 33
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildFeatures {
        compose true
    }

    composeOptions {
        kotlinCompilerVersion '1.3.2'
        kotlinCompilerExtensionVersion '1.3.2'
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    def composeBom = platform('androidx.compose:compose-bom:2022.12.00')
    implementation composeBom
    androidTestImplementation composeBom
    implementation "androidx.compose.compiler:compiler:$compose_version"
    implementation 'androidx.compose.material:material:1.3.1'
    implementation 'androidx.compose.ui:ui-tooling-preview'
    debugImplementation 'androidx.compose.ui:ui-tooling'
    implementation 'androidx.activity:activity-compose:1.5.1'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.navigation:navigation-fragment:2.5.3'
    implementation 'androidx.navigation:navigation-ui:2.5.3'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
repositories {
    mavenCentral()
}

Error 3 Updating Gradle error

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'ComposeTest'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve com.android.tools.build:gradle:7.4.0.
     Required by:
         project : > com.android.application:com.android.application.gradle.plugin:7.4.0
         project : > com.android.library:com.android.library.gradle.plugin:7.4.0
      > No matching variant of com.android.tools.build:gradle:7.4.0 was found. The consumer was configured to find a runtime of a library compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.5' but:
          - Variant 'apiElements' capability com.android.tools.build:gradle:7.4.0 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares an API of a component compatible with Java 11 and the consumer needed a runtime of a component compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5')
          - Variant 'javadocElements' capability com.android.tools.build:gradle:7.4.0 declares a runtime of a component, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5')
          - Variant 'runtimeElements' capability com.android.tools.build:gradle:7.4.0 declares a runtime of a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5')
          - Variant 'sourcesElements' capability com.android.tools.build:gradle:7.4.0 declares a runtime of a component, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5')

* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

Deciphering this error what its really trying to tell you is that there is an incompatibility this line “No matching variant of com.android.tools.build:gradle:7.4.0 was found. The consumer was configured to find a runtime of a library compatible with Java 8” is telling you that your currently configured to target Java 8 but this line “because this component declares an API of a component compatible with Java 11” is telling you that a component in the Gradle API is targeting Java 11. So how do we update this. Luckily Android Studio comes pre-packaged with a version of Java that you can point to.

In Android Studio go to “File -> Settings”

Go down to Build, Execution, Deployment -> Build Tools -> Gradle. Change the Gradle JDK option to Android Studio default, but make sure its targeting version 11. Alternatively you could update your current version of Java to 11 or if its already updated maybe you just need to target that instead.

Error 4 MutableState has no method

Type 'MutableState<TypeVariable(T)>' has no method 'getValue(WeightViewModel, KProperty<*>)' and thus it cannot serve as a delegate

OR

Type 'TypeVariable(T)' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate

I get these errors all the time, and I can never remember what I need to import to make them go away. Long story short you did something like this in your code:

@Composable
fun MainView(modifier: Modifier = Modifier, viewModel: MainViewModel) {

    val something by remember { mutableStateOf("Test")}
}
// OR Something like this

class WeightViewModel : ViewModel() {

    var queryResult  by mutableStateOf("")
}

To solve these errors you need to import the getValue and setValue packages, then the errors should clear up.

import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue