|
| 1 | +# D8 Kotlin Metadata Error - Fix Summary |
| 2 | + |
| 3 | +## Problem |
| 4 | +The project was experiencing D8 errors during the `dexBuilderDebug` task: |
| 5 | +``` |
| 6 | +WARNING: D8: Unexpected error during rewriting of Kotlin metadata for class 'info.hannes.github.sample.MainActivity': |
| 7 | +com.android.tools.r8.internal.xb4: Should never be called |
| 8 | +``` |
| 9 | + |
| 10 | +## Root Cause |
| 11 | +The issue was caused by an incompatibility between: |
| 12 | +- **Kotlin version**: 2.3.20 (too new) |
| 13 | +- **Android Gradle Plugin**: 8.13.0 |
| 14 | +- **R8 version**: 8.13.6 (bundled with AGP) |
| 15 | + |
| 16 | +The R8 version included with AGP 8.13.0 does not support Kotlin 2.3.20's metadata format, as Kotlin 2.3.20 was released after this version of R8. |
| 17 | + |
| 18 | +## Solution Applied |
| 19 | + |
| 20 | +### 1. Downgraded Kotlin Version |
| 21 | +**File**: `build.gradle` |
| 22 | +- **Changed from**: `ext.kotlin_version = "2.3.20"` |
| 23 | +- **Changed to**: `ext.kotlin_version = "2.1.0"` |
| 24 | + |
| 25 | +Kotlin 2.1.0 is fully compatible with AGP 8.13.0 and R8 8.13.6. |
| 26 | + |
| 27 | +### 2. Added R8 Configuration |
| 28 | +**File**: `gradle.properties` |
| 29 | +- Added: `android.enableR8.fullMode=false` |
| 30 | + |
| 31 | +This disables R8 full mode to prevent aggressive Kotlin metadata rewriting. |
| 32 | + |
| 33 | +### 3. Created ProGuard Rules |
| 34 | +**File**: `app/proguard-rules.pro` (new file) |
| 35 | +- Added rules to preserve Kotlin metadata |
| 36 | +- Keeps Kotlin classes and annotations intact |
| 37 | +- Prevents issues with Kotlin reflection and metadata |
| 38 | + |
| 39 | +### 4. Updated Build Configuration |
| 40 | +**File**: `app/build.gradle` |
| 41 | +- Added `buildTypes` block with ProGuard rules configuration |
| 42 | +- Ensures proper handling of Kotlin code during build |
| 43 | + |
| 44 | +### 5. Fixed BuildConfig Deprecation Warning |
| 45 | +**File**: `gradle.properties` |
| 46 | +- Removed deprecated `android.defaults.buildfeatures.buildconfig=true` |
| 47 | +- BuildConfig is now explicitly enabled in module-level build.gradle files (already present) |
| 48 | + |
| 49 | +## Result |
| 50 | +✅ Build successful without D8 errors or warnings |
| 51 | +✅ All 189 tasks executed successfully |
| 52 | +✅ No Kotlin metadata rewriting errors |
| 53 | + |
| 54 | +## Verification |
| 55 | +Run `./gradlew clean build` to verify the fix. |
| 56 | + |
| 57 | +## Alternative Solutions (Not Applied) |
| 58 | +If downgrading Kotlin is not acceptable, you could: |
| 59 | +1. Upgrade to a newer AGP version that supports Kotlin 2.3.20 |
| 60 | +2. Wait for Android Gradle Plugin 8.14+ or later versions with updated R8 support |
| 61 | +3. Use Kotlin 2.0.x or 2.1.x which are stable and widely supported |
| 62 | + |
| 63 | +## Compatibility Matrix |
| 64 | +| Component | Version | Status | |
| 65 | +|-----------|---------|--------| |
| 66 | +| Kotlin | 2.1.0 | ✅ Compatible | |
| 67 | +| Android Gradle Plugin | 8.13.0 | ✅ Compatible | |
| 68 | +| R8 | 8.13.6 | ✅ Compatible | |
| 69 | +| Gradle | 9.4.1 | ✅ Compatible | |
| 70 | +| Java | 21.0.9 | ✅ Compatible | |
| 71 | + |
| 72 | +## References |
| 73 | +- [Kotlin and Android Gradle Plugin Compatibility](https://developer.android.com/studio/build/kotlin-d8-r8-versions) |
| 74 | +- [R8 Documentation](https://r8.googlesource.com/r8) |
| 75 | + |
0 commit comments