KotlinReflectionInternalError after updating Android Gradle Plugin to 7.0.0

Viewed 752

I have a project that uses Jackson for JSON serialization, and the release build uses R8 for obfuscation. Now, Everything works fine with AGP 4.2.1, but updating to AGP 7.0.0 causes this error in obfuscated builds:

kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Could not compute caller for function:
    public final fun setResult(result: com.example.networking.Result?): ud.v defined in com.example.networking.Response[DeserializedSimpleFunctionDescriptor@14aa15e] (member = null)
    at kotlin.reflect.jvm.internal.KFunctionImpl$caller$2.invoke(:89)
    at kotlin.reflect.jvm.internal.KFunctionImpl$caller$2.invoke(:36)
    at kotlin.reflect.jvm.internal.ReflectProperties$LazyVal.invoke(:62)
    at kotlin.reflect.jvm.internal.ReflectProperties$Val.getValue(:31)
    at kotlin.reflect.jvm.internal.KFunctionImpl.getCaller(Unknown Source:7)
    at kotlin.reflect.jvm.ReflectJvmMapping.getJavaMethod(:63)
    at kotlin.reflect.jvm.ReflectJvmMapping.getKotlinFunction(:136)
    at com.fasterxml.jackson.module.kotlin.KotlinAnnotationIntrospector.hasRequiredMarker(:123)
    at com.fasterxml.jackson.module.kotlin.KotlinAnnotationIntrospector.access$hasRequiredMarker(:23)
    at com.fasterxml.jackson.module.kotlinAnnotationIntrospector$a.a(:40)
    ...

(Note: package and class name changed for privacy reasons. Based on the mapping file, ud.v maps to kotlin.Unit)

The Response class looks something like this:

open class Response {
    @JsonSetter
    fun setResult(result: Result?) {
        // ...
    }
}

In ProGuard / R8 rules, the classes are kept with this rule:

-keep class com.example.networking.** { *: }

What could cause this issue to arise after updating to AGP 7.0.0? Is there something I should change in addition to just updating AGP version?

EDIT:

Here's the top-level build.gradle (unrelevant stuff omitted):

buildscript {
    ext.kotlin_version = '1.5.21'
    repositories {
        ...
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

And here's the project-level build.gradle (again, unrelevant stuff omitted):

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-parcelize'
apply plugin: 'kotlin-kapt'

android {
    defaultConfig {
        applicationId 'com.example.example'

        compileSdkVersion 30
        targetSdkVersion 30
        minSdkVersion 21

        multiDexEnabled true
    }

    buildFeatures.dataBinding = true

    signingConfigs { ... }

    buildTypes {
        debug {
            debuggable true
            signingConfig signingConfigs.debugSigning
        }

        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.releaseSigning
        }
    }

    dexOptions {
        preDexLibraries = false
        javaMaxHeapSize "2g"
    }

    packagingOptions { ... }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
        encoding "UTF-8"
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }

    kapt {
        correctErrorTypes true
    }
}

dependencies { ... }
3 Answers
# Temporary workaround for kotlin-reflect 1.5.20 https://issuetracker.google.com/issues/196179629
# When R8 is enabled for release builds, app will crash without this rule.
-keepnames class kotlin.* { *; }

I believe the problem is related to your build.gradle forcing Java 1.8 when Gradle 7.0 requires Java 11 and the latest versions of Android Studio use JDK 11 by default. I had a similar issue that took a while to solve here.

After two days of tightening the rope around the issue, I found the culprit. It boils down to this ProGuard / R8 rule:

-keep class kotlin.Metadata { *; }

If this line is removed, the error goes away. However, it's still unknown why this happens.

Related