A debug APK build in Android Studio can fail inside D8 with java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 31. Google identifies this as a regression in D8’s handling of classes that contain local-variable debug information. The fix is listed in Android Gradle Plugin 9.3.3 and AGP 9.4.1.
Representative error log
java.lang.ArrayIndexOutOfBoundsException:
Index -1 out of bounds for length 31
Compilation failed with an internal error.
Execution failed for task ':app:mergeProjectDexDebug'.
> A failure occurred while executing
com.android.build.gradle.internal.tasks.DexMergingTaskDelegate
The failing task and array length can vary. The important pattern is an Index -1 out of bounds exception from D8/DEX processing followed by an internal compilation failure.
Cause
According to Google’s issue report, D8 can access the invalid index -1 while processing local-variable debug metadata in a class that contains a LocalVariableTable. The same input compiled successfully in release mode and when Java was compiled with javac -g:lines,source, which omits local-variable debug information.
This is not necessarily an array-indexing bug in your application. If the stack trace points into D8/R8 and says Compilation failed with an internal error, check the toolchain regression before changing application array logic.
Affected conditions and versions
- D8 dexes a class that contains local-variable debug information, typically in a debug build.
- Google’s reproducer succeeds with R8 8.8.34 and earlier but fails from 8.9.27 through at least 9.4.12.
- The same bytecode may compile in release mode.
- The fix is listed in Android Gradle Plugin 9.3.3.
- The same fix is included in AGP 9.4.1, released with Android Studio Quail 4 Patch 1.
Recommended solution
1. Check the current AGP version
Inspect the root build.gradle.kts, build.gradle, or gradle/libs.versions.toml.
// build.gradle.kts
plugins {
id("com.android.application") version "9.3.2" apply false
}
2. Upgrade to AGP 9.3.3 or newer
If the project must remain on the 9.3 line, update to 9.3.3 or newer. Android Studio Quail 4 Patch 1 includes AGP 9.4.1, which also lists the fix.
// build.gradle.kts
plugins {
id("com.android.application") version "9.3.3" apply false
id("com.android.library") version "9.3.3" apply false
}
With a Version Catalog:
[versions]
agp = "9.3.3"
3. Align the Gradle Wrapper and JDK
The official AGP 9.3 compatibility table specifies Gradle 9.5.0 and JDK 17 as the minimum/default toolchain. Use Android Studio’s Upgrade Assistant and verify the Wrapper and Gradle JDK together.
./gradlew --version
4. Stop old daemons and rerun the debug build
./gradlew --stop
./gradlew clean
./gradlew assembleDebug --stacktrace
Verify the exact debug task that previously failed. A successful release build alone does not prove that the debug-mode regression is resolved.
Temporary alternatives
Google’s reproducer compiles when Java debug information is limited to source and line data, excluding the local-variable table. If an AGP upgrade is temporarily blocked, consider this setting only for the affected Java modules:
tasks.withType<JavaCompile>().configureEach {
options.debugOptions.debugLevel = "source,lines"
}
This reduces the local-variable information available to the debugger. It is an inference from the official reproducer result, not Google’s documented permanent fix. Prefer upgrading AGP.
Another temporary option is to pin the last working Android Studio/AGP/Gradle/JDK combination. Do not force an arbitrary standalone R8 jar into AGP; keep the supported toolchain aligned.
How to verify the fix
- Run
./gradlew --versionto confirm Gradle and the JDK. - Sync the project again.
- Run
./gradlew clean assembleDebug --stacktrace. - Run each previously failing
assemble<Variant>task when multiple variants are affected. - Confirm that the D8 internal
ArrayIndexOutOfBoundsException: Index -1no longer appears.
Cautions
- Check the exception origin and failed Gradle task before treating it as an application array bug.
- Setting
minifyEnabled falsemay not help because D8 still converts debug bytecode to DEX. - A successful release build does not verify the affected debug path.
- Do not treat cache deletion alone as a permanent fix.
- Use reduced debug metadata only as a short-term workaround or diagnostic step.
Summary
When D8 throws ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 31, the cause can be the R8 regression in processing local-variable debug metadata. Upgrade to AGP 9.3.3, AGP 9.4.1, or a newer fixed release; keep Gradle and the JDK compatible; and retest the failing debug variant. If upgrading is temporarily impossible, cautiously consider omitting local-variable debug metadata or returning to the previous working toolchain.
