在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 * {
;
}
但是即使我们删除了这些用法,该错误仍然保持不变。
我们如何解决该错误?
的build.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'
...
}