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.
읽는 시간 3분최근 업데이트: 2일 전
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.
Symptom
After you upgrade the LevelPlay Unity (Ads Mediation) package to 9.6.0, your Android build fails with the following error:
Note: Some input files use or override a deprecated API.Note: Recompile with -Xlint:deprecation for details.ERROR:D8: com.android.tools.r8.kotlin.HFAILURE: 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:
- The line appears in the output.
ERROR:D8: com.android.tools.r8.kotlin.H - The failing artifact is (or any
kotlin-stdlib-2.1.21.jar).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 error and the secondary
D8: com.android.tools.r8.kotlin.H.StackOverflowError
Resolution
To resolve this issue, override R8 by adding a block to your by following these instructions:
buildscriptsettingsTemplate.gradle- If you don’t already have a 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
settingsTemplate.gradlewith ARTIFACTORYREPOSITORY and DIR_UNITYPROJECT tokens.ssets/Plugins/Android/settingsTemplate.gradle - Open .
Assets/Plugins/Android/settingsTemplate.gradle - Add a code block that pulls R8 8.5.35 from Google's R8 release Maven. Add it directly after the existing
buildscriptcode block:pluginManagement
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' }}
The following line of code forces Gradle to resolve R8 8.5.35 (which supports ) before the Android Gradle Plugin can select its bundled, outdated version. The task then uses the newer R8 and the build completes successfully.
Kotlin 2 stdlibmergeExtDexReleasebuildscript { dependencies { classpath 'com.android.tools:r8:...' } }
Verify the resolution
- Close any open Gradle daemon by running the following command from the project root:
Alternatively, you can delete Gradle transformations by running:
./gradlew --stop.~/.gradle/caches/transforms-3/ - In the Unity Editor, go to File > Build Settings… > Build (Release configuration).
- Confirm that the task completes and the APK or AAB builds without the D8 error.
:launcher:mergeExtDexRelease
If the error persists, try the following:
- Confirm the block is in
buildscriptand not insettingsTemplate.gradleormainTemplate.gradle.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/