# 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.

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 `:app:compile<Variant>Kotlin` (for example,`:app:compileDebugKotlin`), and the Gradle log contains an excerpt similar to:

```xml
> 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:

```kotlin
./gradlew :app:dependencies | grep kotlin-stdlib
```

If you see `kotlin-stdlib:2.x.x` in the graph while your project's Kotlin Gradle Plugin is on 1.x, this guide applies to you.

## Causes

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 `com.google.devtools.ksp` to a release that matches your Kotlin version.
* Jetpack Compose, if used, requires the Compose Compiler Gradle plugin starting with Kotlin 2.0. The legacy `composeOptions.kotlinCompilerExtensionVersion` setting no longer applies.
* Upgrading Kotlin might also display deprecation warnings or `source-incompatible` changes in your own code under the K2 compiler. Refer to the [Kotlin 2.0.0 What's new release notes](https://kotlinlang.org/docs/whatsnew20.html) for additional information.

## Verify the resolution

1. In Android Studio, run **File** > **Sync Project with Gradle Files**, **Build** > **Rebuild Project**.
2. Confirm the build is fixed by checking:
   a. The Gradle output ends with a `BUILD SUCCESSFUL` message and `:app:compile<Variant>Kotlin` completes without errors.
   b. The Incompatible classes that were found in dependencies message no longer appear in the build log.

If the error persists, try the following:

* Confirm the kotlin dependencies in the `build.gradle` use the updated kotlin version.
* Clear the Gradle cache by running `./gradlew --stop` followed by `rm -rf ~/.gradle/caches/transforms-3/`, then rebuild. Alternatively, in Android Studio, use **File** > **Invalidate Caches…**, then rebuild.
