4

在 Android Studio 3.4.1 中从 Kotlin 1.3.21 更新到 1.3.30+ 时,我们收到以下构建错误:

e: java.lang.IllegalStateException: failed to analyze: java.lang.AssertionError: annotation tree hasn't been attributed yet: @kotlin.Metadata(mv = {1, 1, 15}, bv = {1, 0, 3}, k = 1, d1 = {"\u0000$\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\b&\u0018\u0000*\b\b\u0000\u0010\u0001*\u00020\u00022\b\u0012\u0004\u0012\u0002H\u00010\u0003B\u001f\u0012\u0006\u0010\u0004\u001a\u00020\u0005\u0012\b\b\u0001\u0010\u0006\u001a\u00020\u0007\u0012\u0006\u0010\b\u001a\u00020\t\u00a2\u0006\u0002\u0010\n\u00a8\u0006\u000b"}, d2 = {"Lde/thermomess/field/ui/viewholder/BaseViewHolder;", "T", "", "Lcom/xroot/android/ui/recyclerview/RecyclerViewHolder;", "context", "Landroid/content/Context;", "layoutResId", "", "parent", "Landroid/view/ViewGroup;", "(Landroid/content/Context;ILandroid/view/ViewGroup;)V", "app_betaDebug"})

@kotlin.Metadata 背后的一切都不同,但在此之前它保持不变。使用 Kotlin 1.3.21 及更早版本,一切正常。

我们使用@kotlin.Metadata 来检查一个类是否是 Gson TypeAdapterFactory 的 Kotlin 类

private val Class<*>.isKotlinClass: Boolean
    get() = declaredAnnotations.find { it.annotationClass.java.name == "kotlin.Metadata" } != null

以及通过混淆规则跳过自定义注释

-keep class kotlin.Metadata { *; }
-keepclassmembers @kotlin.Metadata class * {
    <methods>;
}

但即使我们删除这些用法,错误仍然保持不变。

我们如何修复这个错误?

构建.gradle

dependencies {
    classpath 'com.android.tools.build:gradle:3.4.1'
    classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.31'
    classpath 'io.objectbox:objectbox-gradle-plugin:2.3.4'
}

应用程序/build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'io.objectbox'

android {
    compileSdkVersion 28
    defaultConfig {
        ...
        minSdkVersion 16
        targetSdkVersion 28
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    compileOptions {
        targetCompatibility 1.8
        sourceCompatibility 1.8
    }
}

dependencies {
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31'
    implementation 'org.jetbrains.kotlin:kotlin-reflect:1.3.31'
    ...
}
4

0 回答 0