将 Android kotlin 版本升级到1.5.0,在构建时抛出错误消息

运行 kotlin 版本’1.4.32’我的 Android 项目运行和构建。试图升级到 kotlin’1.5.0’,我的版本抛出:

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)

我甚至不知道从哪里开始找,还有谁升级到 kotlin 1.5.0有问题吗?

42235 次浏览

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.

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

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

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.

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.

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'

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.

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?

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

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

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

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

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

You should update hilt to 2.42 for kotlin 1.7

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")


}