【问题标题】:KotlinReflectionInternalError after updating Android Gradle Plugin to 7.0.0将 Android Gradle 插件更新到 7.0.0 后出现 KotlinReflectionInternalError
【发布时间】:2021-08-10 11:13:35
【问题描述】:

我有一个项目使用 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 { ... }

【问题讨论】:

  • 发布你的 build.gradle
  • @Larrikin 更新了(我希望)build.gradle 文件的相关部分。
  • 我在 R8 问题跟踪器中创建了一个issue 196179629。请自行抄送并跟进。

标签: android kotlin jackson android-gradle-plugin android-r8


【解决方案1】:

我认为问题与您的 build.gradle 强制使用 Java 1.8 有关,而 Gradle 7.0 需要 Java 11,而最新版本的 Android Studio 默认使用 JDK 11。我有一个类似的问题需要一段时间才能解决here

【讨论】:

  • 感谢您的建议。我尝试用版本 11 替换 sourceCompatibility、targetCompatibility 和 jvmTarget,但问题仍然存在。
【解决方案2】:

在解决问题两天后,我找到了罪魁祸首。归结为这个 ProGuard / R8 规则:

-keep class kotlin.Metadata { *; }

如果删除此行,错误就会消失。但是,为什么会发生这种情况仍然未知。

【讨论】:

    【解决方案3】:
    # 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.* { *; }
    

    【讨论】:

      猜你喜欢
      • 2021-10-07
      • 2021-12-02
      • 2016-08-16
      • 2016-03-11
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多