77

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?

lostintranslation
  • 23,756
  • 50
  • 159
  • 262

17 Answers17

96

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.

avatsav
  • 1,208
  • 1
  • 8
  • 16
63

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

Etherbit
  • 646
  • 4
  • 2
18

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'
18

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

Stuart Welch
  • 409
  • 5
  • 3
14

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

Soorya
  • 179
  • 1
  • 5
13

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.

the korovay
  • 563
  • 4
  • 16
6

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
Kostadin Georgiev
  • 866
  • 1
  • 9
  • 23
  • And Moshi to 1.12.0 and then IDE complains about Kotlin 1.5.0 not being compatible with Compose (In my case) – Rez May 20 '21 at 11:24
  • this helped me. I got an issue after updating the kotlin version to "1.5.10" In my case, I updated room version as i am using room in my project – Gulnaz Ghanchi Sep 08 '21 at 06:05
4

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.

Michał Klimczak
  • 12,674
  • 8
  • 66
  • 99
4

You should update hilt to 2.42 for kotlin 1.7

hamid Mahmoodi
  • 690
  • 4
  • 16
3

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Pruthvi
  • 31
  • 4
1

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

arjava
  • 1,719
  • 2
  • 17
  • 21
1

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

reza rayatnia
  • 81
  • 1
  • 6
0

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?

marcinj
  • 48,511
  • 9
  • 79
  • 100
0

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

AtomicStrongForce
  • 529
  • 1
  • 5
  • 11
0

Go to https://mvnrepository.com/artifact/com.google.dagger/hilt-android-gradle-plugin to get the latest hilt version.

Then, update the project build.gradle and update to the latest version:

dependencies {

    classpath('com.google.dagger:hilt-android-gradle-plugin:2.43.2')

}

Do the same for the app build.gradle

dependencies {

    implementation("com.google.dagger:hilt-android:2.43.2")
    kapt("com.google.dagger:hilt-android-compiler:2.43.2") 

}
Andrew Lee
  • 446
  • 4
  • 7
0

Got this error due to Gradle not recognizing . Sync project with gradle file and restarted , it works fine for me.

sakthi
  • 1
  • 1
0

Just check the dagger graph properly done or not. I missed the @module annotation on top of the class name here:

Before:

abstract class StorageModule {

}

After:

@module
abstract class StorageModule {

}

So, check all the annotations are added or not.

Stephen
  • 9,899
  • 16
  • 90
  • 137