Troubleshoot Android build failures caused by Kotlin 2
Identify and fix Android build failures caused by the Kotlin 2 dependency introduced in LevelPlay 9.6.0 when building a native Android app whose Kotlin version is lower than 2.0.0.
읽는 시간 2분최근 업데이트: 2일 전
If you use the Kotlin version lower than 2.0.0, you might encounter Android build failures. This guide explains how to identify and resolve them.
Symptom
After you upgrade the LevelPlay SDK dependency to 9.6.0, your Android build fails during Kotlin compilation. The Build Output panel in Android Studio shows the failing task as (for example,), and the Gradle log contains an excerpt similar to:
:app:compile<Variant>Kotlin:app:compileDebugKotlin> Task :app:compileDebugKotlin FAILED e: Incompatible classes were found in dependencies. Remove them from the classpath or use '-Xskip-metadata-version-check' to suppress errors e: file:///.../transformed/jetified-kotlin-stdlib-2.1.21.jar!/META-INF/kotlin-stdlib.kotlin_module Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 2.1.0, expected version is 1.8.0. e: file:///.../src/main/java/.../<YourFile>.kt:NN:NN Class 'kotlin.Unit' was compiled with an incompatible version of Kotlin. The actual metadata version is 2.1.0, but the compiler version 1.8.0 can read versions up to 1.9.0.
To confirm before applying the fix, you can inspect the resolved Kotlin stdlib version:
./gradlew :app:dependencies | grep kotlin-stdlib
If you see in the graph while your project's Kotlin Gradle Plugin is on 1.x, this guide applies to you.
kotlin-stdlib:2.x.xCauses
Android build failures are likely to occur for the following reasons:
- The LevelPlay SDK 9.6.0 (and some of its network adapters) has a dependency on the Kotlin 2.x standard library (currently ).
kotlin-stdlib-2.1.21 - A Kotlin version defined for the project lower than 2.0 will be incompatible with LevelPlay SDK 9.6.0 that uses Kotlin 2.x.
Resolution
To resolve this issue, upgrade the Kotlin Gradle Plugin (KGP) version your project uses to 2.0.0 or higher. The recommended target is 2.1.21, which is the version LevelPlay 9.6.0 itselfships, so your project's stdlib matches the SDK's.
Upgrade requirements
Upgrading to Kotlin 2.0 or higher might require updating or configuring related toolchain versions, as follows:
- JDK 17 is required to run the Kotlin 2.0 or higher compiler. To set JDK 17 in Android Studio, in your project, go to Settings > Build, Execution, Deployment > Build Tools > Gradle > Gradle JDK.
- KSP, if used, is tied to the Kotlin version. Upgrade to a release that matches your Kotlin version.
com.google.devtools.ksp - Jetpack Compose, if used, requires the Compose Compiler Gradle plugin starting with Kotlin 2.0. The legacy setting no longer applies.
composeOptions.kotlinCompilerExtensionVersion - Upgrading Kotlin might also display deprecation warnings or changes in your own code under the K2 compiler. Refer to the Kotlin 2.0.0 What's new release notes for additional information.
source-incompatible
Verify the resolution
- In Android Studio, run File > Sync Project with Gradle Files, Build > Rebuild Project.
- Confirm the build is fixed by checking:
a. The Gradle output ends with a message and
BUILD SUCCESSFULcompletes without errors. b. The Incompatible classes that were found in dependencies message no longer appear in the build log.:app:compile<Variant>Kotlin
If the error persists, try the following:
- Confirm the kotlin dependencies in the use the updated kotlin version.
build.gradle - Clear the Gradle cache by running followed by
./gradlew --stop, then rebuild. Alternatively, in Android Studio, use File > Invalidate Caches…, then rebuild.rm -rf ~/.gradle/caches/transforms-3/