我在项目中使用Kotlin和数据绑定,在最近升级到Android Studio 3.1.0后,我不断看到许多Android Support插件中的异常:
无法初始化android.databinding.tool.ext.ExtKt类 java.lang.NoClassDefFoundError: 无法初始化android.databinding.tool.ext.ExtKt类 在android.databinding.tool.reflection.ModelClass.getTypeName(ModelClass.java:688)中 at android.databinding.tool.reflection.ModelClass.equals(ModelClass.java:694)中 at android.databinding.tool.reflection.ModelClass.findSetter(ModelClass.java:614)中 at android.databinding.tool.reflection.ModelClass.findGetterOrField(ModelClass.java:523)中 at com.android.tools.idea.lang.databinding.DataBindingXmlReferenceContributor $ 2.getReferencesByElement(DataBindingXmlReferenceContributor.java:196)中 at com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistryImpl.getReferences(ReferenceProvidersRegistryImpl.java:135)中 at com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistryImpl.mapNotEmptyReferencesFromProviders(ReferenceProvidersRegistryImpl.java:123)中 at com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistryImpl.doGetReferencesFromProviders(ReferenceProvidersRegistryImpl.java:102)中 at com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry.getReferencesFromProviders(ReferenceProvidersRegistry.java:59)中 at com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry.getReferencesFromProviders(ReferenceProvidersRegistry.java:53)中 at com.android.tools.idea.lang.databinding.DataBindingPsiElement.getReferences(DataBindingPsiElement.java:32)中 at org.intellij.plugins.intelliLang.references.InjectedReferencesContributor.getInjectedReferences(InjectedReferencesContributor.java:56)中 at org.intellij.plugins.intelliLang.references.InjectedReferencesInspection $ 1.visitElement(InjectedReferencesInspection.java:40)中 at com.intellij.psi.impl.PsiElementBase.accept(PsiElementBase.java:274)中 at com.android.tools.idea.lang.databinding.psi.impl.PsiDbExprImpl.accept(PsiDbExprImpl.java:43)中 at com.android.tools.idea.lang.databinding.psi.impl.PsiDbRefExprImpl.accept(PsiDbRefExprImpl.java:42)中 at com.intellij.codeInspection.InspectionEngine.acceptElements(InspectionEngine.java:82)中 at com.intellij.codeInspection.InspectionEngine.createVisitorAndAcceptElements(InspectionEngine.java:70)中 at com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.doInspectInjectedPsi(LocalInspectionsPass.java:750)在com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.lambda $ inspectInjectedPsi $ 6(LocalInspectionsPass.java:338)中 在com.intellij.concurrency.ApplierCompleter.execAndForkSubTasks(ApplierCompleter.java:133)中 在com.intellij.concurrency.ApplierCompleter.tryToExecAllList(ApplierCompleter.java:223)中 在com.intellij.concurrency.ApplierCompleter.execAndForkSubTasks(ApplierCompleter.java:151)中 在com.intellij.openapi.application.impl.ApplicationImpl.tryRunReadAction(ApplicationImpl.java:1125)中 在com.intellij.concurrency.ApplierCompleter.lambda $ wrapInReadActionAndIndicator $ 1( ApplierCompleter.java:105)中 在com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:543)中 在com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:488)中 在com.intellij.openapi.progress.impl.ProgressManagerImpl.executePro cessUnderProgress(ProgressManagerImpl.java:94)中 在com.intellij.concurrency.ApplierCompleter.wrapInReadActionAndIndicator(ApplierCompleter.java:116)中 在com.intellij.concurrency.ApplierCompleter.lambda $ compute $ 0(ApplierCompleter.java:96)中 在java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:731)中 在java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)中 在java.util.concurrent.ForkJoinPool $ WorkQueue.pollAndExecCC(ForkJoinPool.java:1190)中 在java.util.concurrent.ForkJoinPool.helpComplete(ForkJoinPool.java:1879)中 在java.util.concurrent.ForkJoinPool.awaitJoin(ForkJoinPool.java:2045)中 在java.util.concurrent.ForkJoinTask.doJoin(ForkJoinTask.java:390)中 在java.util.concurrent.ForkJoinTask.join(ForkJoinTask.java:719)中 在java.util.concurrent.ForkJoinPool.invoke(ForkJoinPool.java:2616)中 在com.intellij.concurrency.JobLauncherImpl.invokeConcurrentlyUnderProgress(JobLauncherImpl.java:65)中 在com.intellij.concurrency.JobLauncher.invokeConcurrentlyUnderProgress(JobLauncher.java:57)中 在com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.inspectInjectedPsi(LocalInspectionsPass.java:341)中 在com.intellij.codeInsight.daemon.impl.LocalInspections我的主要build.gradle
buildscript {
ext.kotlin_version = '1.2.31'
ext.protobuf_version = '3.0.0'
ext.anko_version='0.10.4'
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.5'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
应用程序build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'project-report'
android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
defaultConfig {
applicationId "com.myapp"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dataBinding {
enabled = true
}
}
kapt {
useBuildCache = true
mapDiagnosticLocations = true
}
dependencies {
implementation project(':proto')
implementation "android.arch.persistence.room:runtime:1.0.0"
implementation 'com.android.support:recyclerview-v7:27.1.0'
kapt "android.arch.persistence.room:compiler:1.0.0"
kapt "com.android.databinding:compiler:3.1.0"
implementation "android.arch.lifecycle:extensions:1.1.1"
implementation "android.arch.lifecycle:common-java8:1.1.1"
implementation 'com.auth0.android:jwtdecode:1.1.1'
implementation 'com.squareup.okhttp3:okhttp:3.9.1'
// Anko
implementation "org.jetbrains.anko:anko-sdk15:$anko_version"
implementation "org.jetbrains.anko:anko-appcompat-v7:$anko_version"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'com.android.support:appcompat-v7:27.1.0'
implementation 'com.android.support:design:27.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:support-v4:27.1.0'
implementation 'com.android.support:support-vector-drawable:27.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
//noinspection GradleDependency
androidTestImplementation 'org.exparity:hamcrest-date:1.1.0'
}
还有gradle.properties
org.gradle.jvmargs=-Xmx2048m
kotlin.incremental.usePreciseJavaTracking=true
android.enableD8=true
android.databinding.enableV2=true
gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
到目前为止,我尝试过:
- 清除缓存并重新启动
- 重新导入项目
- 清理并重建
- 没有先前的配置进行干净的安装+重新导入项目+不安装额外插件
- 在gradle.properties中逐行注释每一行或所有行 + 失效并重新启动
- 尝试
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
在gradle中"以防万一"
没有任何帮助,错误仍然在几乎每个文件中弹出。我可以看到这与绑定和kotlin有关。
有人遇到过这个问题吗?
PS
在布局xml文件中,不存在的字符串资源甚至没有被突出显示,例如@string/non_existing_title
- 看起来像是有效的字符串资源(即使在干净安装后仍然如此)。
虽然我不确定这个问题是否与报告的异常有关。
更新1
我注意到,在失效并重新启动后,会抛出另一个异常:
com/squareup/javapoet/TypeName
java.lang.NoClassDefFoundError: com/squareup/javapoet/TypeName
at android.databinding.tool.ext.ExtKt.<clinit>(ext.kt:180)
at android.databinding.tool.reflection.ModelClass.getTypeName(ModelClass.java:688)
at android.databinding.tool.reflection.ModelClass.equals(ModelClass.java:694)
at android.databinding.tool.reflection.ModelClass.findSetter(ModelClass.java:614)
at android.databinding.tool.reflection.ModelClass.findGetterOrField(ModelClass.java:523)
....
Caused by: java.lang.ClassNotFoundException: com.squareup.javapoet.TypeName PluginClassLoader[org.jetbrains.android, 10.3.1] com.intellij.ide.plugins.cl.PluginClassLoader@19c41e97
at com.intellij.ide.plugins.cl.PluginClassLoader.loadClass(PluginClassLoader.java:63)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 63 more
我尝试添加javapoet依赖,但在无效后仍会抛出此异常。然后只有Could not initialize class android.databinding.tool.ext.ExtKt
异常如最初所报告的一样。
@string/non_existent_string
,则它不会以红色突出显示为缺失的字符串。但是,仅当xml使用数据绑定时才会发生,即根节点为<layout>。 在正常的xml(没有数据绑定)中,所有内容都按预期工作-缺少的资源以红色突出显示。因此,问题在于数据绑定处理。 我将尝试升级到3.2 Canary,以检查它在那里的运行情况。 - akamuza