首页 > 解决方案 > 将 Android Gradle 插件更新到 7.0.0 后出现 KotlinReflectionInternalError

问题描述

我有一个项目使用 Jackson 进行 JSON 序列化,并且发布版本使用 R8 进行混淆。现在,AGP 4.2.1 一切正常,但更新到 AGP 7.0.0 会在混淆构建中导致此错误:

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)
    ...

(注意:出于隐私原因更改了包和类名。基于映射文件,ud.v映射到kotlin.Unit

该类Response看起来像这样:

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

在 ProGuard / R8 规则中,这些类遵循以下规则:

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

更新到 AGP 7.0.0 后出现此问题的原因是什么?除了更新 AGP 版本之外,我还有什么需要改变的吗?

编辑:

这是顶级 build.gradle (省略不相关的东西):

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"
    }
}

这是项目级别的 build.gradle(同样,省略了不相关的内容):

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 { ... }

标签: androidkotlinjacksonandroid-gradle-pluginandroid-r8

解决方案


我相信这个问题与你的 build.gradle 强制 Java 1.8 有关,当 Gradle 7.0 需要 Java 11 并且最新版本的 Android Studio 默认使用 JDK 11 时。我有一个类似的问题需要一段时间才能在这里解决。


推荐阅读