'Upgrading Android kotlin version to 1.5.0 throwing error message on build

Running with kotlin version '1.4.32' my Android project runs and builds. Trying to upgrade to kotlin '1.5.0' and my build throws:

Execution failed for task ':app:kaptDefaultsDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
   > java.lang.reflect.InvocationTargetException (no error message)

I am not even sure where to start looking. Anyone else have problems upgrading to kotlin 1.5.0?



Solution 1:[1]

This is due to Dagger's use of older version of kotlinx-metadata-jvm. See https://youtrack.jetbrains.com/issue/KT-45885

Update your dagger to 2.34

Solution 2:[2]

I experienced the same issue today. Ran the gradle build command with --stacktrace and got a helpful stacktrace which narrowed down the issue to Moshi library.

Caused by: java.lang.IllegalStateException: Could not parse metadata! This should only happen if you're using Kotlin <1.1.
    at com.squareup.moshi.kotlinpoet.metadata.KotlinPoetMetadata.readKotlinClassMetadata(KotlinPoetMetadata.kt:70)
    at com.squareup.moshi.kotlinpoet.metadata.KotlinPoetMetadata.toImmutableKmClass(KotlinPoetMetadata.kt:50)
    at com.squareup.moshi.kotlin.codegen.MoshiCachedClassInspector.toImmutableKmClass(MoshiCachedClassInspector.kt:22)
    at com.squareup.moshi.kotlin.codegen.MetadataKt.targetType(metadata.kt:109)
    at com.squareup.moshi.kotlin.codegen.JsonClassCodegenProcessor.adapterGenerator(JsonClassCodegenProcessor.kt:136)
    at com.squareup.moshi.kotlin.codegen.JsonClassCodegenProcessor.process(JsonClassCodegenProcessor.kt:110)
    at org.jetbrains.kotlin.kapt3.base.incremental.IncrementalProcessor.process(incrementalProcessors.kt:90)
    at org.jetbrains.kotlin.kapt3.base.ProcessorWrapper.process(annotationProcessing.kt:175)
    at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:980)
    ... 41 more

Had to update Moshi to 1.12.0 and that did the trick.

Here's a changelog for Moshi 1.12.0 that mentions the fix for 1.5.0: https://github.com/square/moshi/blob/master/CHANGELOG.md#version-1120

Issue when it was first reported here: https://github.com/square/moshi/issues/1324

Another issue which mentions the fix here: https://github.com/square/moshi/issues/1337

So I'd suggest you run gradle build command with --stacktrace and figure out which library causes the incompatibility with the kotlinx-metadata-jvm library and update it.

Solution 3:[3]

In my case dagger caused the issue. Updating the dependency worked out.

implementation 'com.google.dagger:dagger:2.37'
kapt 'com.google.dagger:dagger-compiler:2.37'

Solution 4:[4]

This issue resurfaced for me after upgrading my project to Kotlin 1.6.0. In my case it's because of the Moshi JSON serialization lib. They have already included a fix in their next release. In the meantime, I have reverted back to 1.5.31.

https://github.com/square/moshi/issues/1433

Solution 5:[5]

If other answers didn't help you, and especially if you're on alpha or beta version of Android Studio, make sure that you using JDK embedded with Android Studio. It was my case, i did use external downloaded JDK.

To change JDK go to File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle and select Embedded JDK from dropdown Gradle JDK.

Solution 6:[6]

I had this issue while upgrading Kotlin version to 1.6.10

To fix, I had to update the dagger version to 2.36 or higher and I used 2.40.5

implementation "com.google.dagger:dagger:2.40.5”

kapt "com.google.dagger:dagger-compiler:2.40.5"

To find latest dagger versions - https://github.com/google/dagger/releases

Solution 7:[7]

You should check which of your dependencies probably depends on new kotlin changes in version 1.5.0 and try to update them. For me the room and dagger was causing problems, i've upgraded it to the latest versions and now i have no problems:

  • room to : 2.3.0
  • dagger to : 2.35.1

Solution 8:[8]

It might not necessarily be dagger or moshi. It can be any library that uses kapt (kotlin annotation processor). Look for kapt in your build.gradle and try to update everything that uses it.

For me it was a library which was using an older version of kotlinpoet.

Solution 9:[9]

I had same issue during build:

KaptWithoutKotlincTask$KaptExecutionWorkAction

I had a data binding issue in one of my XML, which threw this error.

This was my case:

abc.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
    </data>
    <TextView
    android:text="@{object.name}"
    ... /> 
</layout>

object variable was not declared. After declaration, it resolved the build issue.

The error message did not point to the correct location in my case.

Solution 10:[10]

yes in my case recently i solved the problem by updating dagger to latest version and gradle update to latest version as well as using latest jdk

Solution 11:[11]

My dagger error message looked as follows:

error: [androidprocessor:miscerror] dagger.android.processor.androidprocessor was unable to process this class because not all of its dependencies could be resolved. check for compilation errors or a circular dependency with generated code.

I also had this kapt error: java.lang.reflect.InvocationTargetException (no error message)

I was given which file dagger was not able to properly process, but no exact information on missing dependency. I applied all the fixes I found in this so. I had to carefully examine all the dependencies I could see in the generated by dagger file for which error was shown.

In my case it was dagger not able to find org.jetbrains.annotations.NotNull. I noticed this dependency was upgraded, and due to some fixes I moved to use androidx.annotation.NonNull in my code. I actually didnt depend on org.jetbrains.annotations - it was being included from some other dependency. The error looked as follows:

found in modules jetified-annotations-12.0 (com.intellij:annotations:12.0) and jetified-annotations-16.0.1 (org.jetbrains:annotations:16.0.1)

I was resolving conflicts which ended up so that dependency was not visible by dagger.

As usual in such cases it was very usefull to see dependecy tree and where dependencies are comming from. Some dependencies were outdated and used org.jetbrains:annotations in older version, and I had to either upgrade them or block this dependency from being included in my code with exclude, for example:

implementation ("io.reactivex.rxjava2:rxkotlin:$rxKotlinVersion") {
  exclude group: 'org.jetbrains', module: 'annotations'
}

for dependency tree listing see here: How do I show dependencies tree in Android Studio?

Solution 12:[12]

I have same issue, and finally solved by update moshi to 1.12.0

Solution 13:[13]

Got this error due to Gradle not recognizing some of my modules. Re-syncing settings.gradle fixed this for me.