# 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 you build with Unity 2022 LTS or earlier.

If you use Unity 2022 LTS or earlier with LevelPlay SDK 9.6.0, you might encounter Android build failures. This guide explains how to identify and resolve them.

> **Note:**
>
> This build failure issue only affects Unity 2022 LTS or earlier. Unity 6 and newer supports compatibility with a newer build toolchain and isn’t affected.

## Symptom

After you upgrade the LevelPlay Unity (Ads Mediation) package to 9.6.0, your Android build fails with the following error:

```xml
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
ERROR:D8: com.android.tools.r8.kotlin.H

FAILURE: Build completed with 2 failures.

1: Task failed with an exception.
-----------
* What went wrong:
  Execution failed for task ':launcher:mergeExtDexRelease'.
> Could not resolve all files for configuration ':launcher:releaseRuntimeClasspath'.
> Failed to transform kotlin-stdlib-2.1.21.jar (org.jetbrains.kotlin:kotlin-stdlib:2.1.21)
  to match attributes {artifactType=android-dex, ...}.
> Execution failed for DexingWithClasspathTransform:
  .../transformed/jetified-kotlin-stdlib-2.1.21.jar.
> Error while dexing.

2: Task failed with an exception.
-----------
* What went wrong:
  java.lang.StackOverflowError (no error message)

BUILD FAILED
```

Two indicators from this error message confirm this is a Kotlin 2 compatibility issue:

1. The `ERROR:D8: com.android.tools.r8.kotlin.H` line appears in the output.
2. The failing artifact is `kotlin-stdlib-2.1.21.jar` (or any `Kotlin 2.x stdlib`).

## 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 (`kotlin-stdlib-2.1.21`).
* Unity 2022 LTS bundles an AGP/R8 version that predates Kotlin 2 support. The bundled D8/R8 can’t dex Kotlin 2 stdlib metadata, which surfaces as the `D8: com.android.tools.r8.kotlin.H` error and the secondary `StackOverflowError`.

## Resolution

To resolve this issue, override R8 by adding a `buildscript` block to your `settingsTemplate.gradle` by following these instructions:

1. If you don’t already have a `settingsTemplate.gradle` file in your project, enable it in the Unity Editor by doing the following:
   a. Go to **File** > **Build Settings**.
   b. Select **Android** as the platform, then select **Player Settings**. The **Project Settings** window opens.
   c. In the **Android settings** tab, go to **Publisher Settings**, then enable **Custom Gradle Settings Template**. Doing so enables Unity to create a file in A`ssets/Plugins/Android/settingsTemplate.gradle` with **ARTIFACTORYREPOSITORY** and **DIR\_UNITYPROJECT** tokens.
2. Open `Assets/Plugins/Android/settingsTemplate.gradle`.
3. Add a `buildscript` code block that pulls R8 8.5.35 from Google's R8 release Maven. Add it directly after the existing `pluginManagement` code block:

> **Note:**
>
> The recommended best practice is to use R8 version 8.5.35 because it’s the validated version for LevelPlay 9.6.0 on Unity 2022 LTS. However, you can use a newer R8 version than 8.5.35. Specifically, any version of R8 that’s greater than or equal to 8.5.x that supports Kotlin 2 will work.

```kotlin
pluginManagement {
    repositories {
        **ARTIFACTORYREPOSITORY**
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}

buildscript {
    repositories {
        maven {
            url "https://storage.googleapis.com/r8-releases/raw"
        }
    }
    dependencies {
        classpath 'com.android.tools:r8:8.5.35'
    }
}
```

> **Note:**
>
> A Kotlin Gradle Plugin upgrade isn’t required to resolve this issue. The failure is in the dex pipeline, not in Kotlin compilation, so only the R8 needs to be upgraded.

The following line of code forces Gradle to resolve R8 8.5.35 (which supports `Kotlin 2 stdlib`) before the Android Gradle Plugin can select its bundled, outdated version. The `mergeExtDexRelease` task then uses the newer R8 and the build completes successfully.

```kotlin
buildscript { dependencies { classpath 'com.android.tools:r8:...' } }
```

> **Note:**
>
> Overriding R8 doesn’t affect your non-Kotlin code. This only changes the dexer/shrinker version, it doesn’t change your application code, AGP version, or minification rules.

## Verify the resolution

1. Close any open Gradle daemon by running the following command from the project root:
   `./gradlew --stop.` Alternatively, you can delete Gradle transformations by running: `~/.gradle/caches/transforms-3/`
2. In the Unity Editor, go to **File** > **Build Settings…** > **Build (Release configuration)**.
3. Confirm that the `:launcher:mergeExtDexRelease` task completes and the APK or AAB builds without the D8 error.

If the error persists, try the following:

* Confirm the `buildscript` block is in `settingsTemplate.gradle` and not in `mainTemplate.gradle` or `baseProjectTemplate.gradle`.
* Confirm no other template is reintroducing an older R8 classpath.
  0- Clear Gradle transform cache using the following command, then rebuild: `rm -rf ~/.gradle/caches/transforms-3/`
