Android studio Databinding 报 different providers 错误

这个问题我和同事3台电脑都会出现,应该不是个别情况,但是 Google 看起来并没有发现这个严重的问题,可能 Databinding 的使用者比较少吧。

环境

  • Android studio 3.1 和 3.2 版本

  • Gradle 4.4

  • gradlePlugin 3.1.x - 3.2.x

  • model 依赖:

     android {
         compileSdkVersion 27
         buildToolsVersion "27.0.3"
         defaultConfig {
             applicationId "com.xxj.mvvm.demo.android_mvvm_demo"
             minSdkVersion 22
             targetSdkVersion 27
             versionCode 1
             versionName "1.0"
             testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
         }
         buildTypes {
             release {
                 minifyEnabled false
                 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
             }
         }
         dataBinding {
             enabled = true
         }
     }
    

操作步骤

  1. 把 activity_main.xml 转换为 Databinding 布局
  2. 在 Activity 中使用 DataBindingUtil 实例化,这时没有问题,代码提示正常,Ctrl 点击 ActivityMainBinding 可成功导航到 xml 文件
    ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    
  3. 然后 make/build project(工具栏小锤子)
  4. 发现 ActivityMainBinding 没有代码提示,也无法导航到 xml 布局,studio右下角有报错。

错误信息如下:

    Element: class com.intellij.psi.impl.source.xml.XmlFileImpl because: different providers: SingleRootFileViewProvider{myVirtualFile=file://C:/Users/52492/AndroidStudioProjects/MyOpenProject/Android-MVVM-Demo/app/src/main/res/layout/activity_main2.xml, content=VirtualFileContent{size=1789}}(1b3cefd9); SingleRootFileViewProvider{myVirtualFile=file://C:/Users/52492/AndroidStudioProjects/MyOpenProject/Android-MVVM-Demo/app/src/main/res/layout/activity_main2.xml, content=VirtualFileContent{size=1789}}(3501664b)
        invalidated at: see attachment
        com.intellij.psi.PsiInvalidElementAccessException: Element: class com.intellij.psi.impl.source.xml.XmlFileImpl because: different providers: SingleRootFileViewProvider{myVirtualFile=file://C:/Users/52492/AndroidStudioProjects/MyOpenProject/Android-MVVM-Demo/app/src/main/res/layout/activity_main2.xml, content=VirtualFileContent{size=1789}}(1b3cefd9); SingleRootFileViewProvider{myVirtualFile=file://C:/Users/52492/AndroidStudioProjects/MyOpenProject/Android-MVVM-Demo/app/src/main/res/layout/activity_main2.xml, content=VirtualFileContent{size=1789}}(3501664b)
            invalidated at: see attachment
            at com.intellij.psi.util.PsiUtilCore.ensureValid(PsiUtilCore.java:502)
            at com.intellij.psi.impl.smartPointers.SmartPointerManagerImpl.createSmartPsiElementPointer(SmartPointerManagerImpl.java:78)
            at com.intellij.psi.impl.smartPointers.SmartPointerManagerImpl.createSmartPsiElementPointer(SmartPointerManagerImpl.java:69)
            at com.intellij.psi.impl.smartPointers.SmartPointerManagerImpl.createSmartPsiElementPointer(SmartPointerManagerImpl.java:64)
            at com.intellij.codeInsight.completion.JavaPsiClassReferenceElement.(JavaPsiClassReferenceElement.java:49)
            at com.intellij.codeInsight.completion.AllClassesGetter.createLookupItem(AllClassesGetter.java:170)
            at com.intellij.codeInsight.completion.JavaClassNameCompletionContributor.createClassLookupItems(JavaClassNameCompletionContributor.java:237)
            at com.intellij.codeInsight.completion.JavaCompletionUtil.createLookupElements(JavaCompletionUtil.java:521)
            at com.intellij.codeInsight.completion.JavaCompletionUtil.processJavaQualifiedReference(JavaCompletionUtil.java:310)
            at com.intellij.codeInsight.completion.JavaCompletionUtil.processJavaReference(JavaCompletionUtil.java:277)
            at com.intellij.codeInsight.completion.JavaCompletionContributor.lambda$addReferenceVariants$7(JavaCompletionContributor.java:451)
            at com.intellij.codeInsight.completion.LegacyCompletionContributor.processReference(LegacyCompletionContributor.java:125)
            at com.intellij.codeInsight.completion.LegacyCompletionContributor.processReferences(LegacyCompletionContributor.java:108)
            at com.intellij.codeInsight.completion.JavaCompletionContributor.addReferenceVariants(JavaCompletionContributor.java:437)
            at com.intellij.codeInsight.completion.JavaCompletionContributor.fillCompletionVariants(JavaCompletionContributor.java:263)
            at com.intellij.codeInsight.completion.CompletionService.getVariantsFromContributors(CompletionService.java:83)
            at com.intellij.codeInsight.completion.CompletionResultSet.runRemainingContributors(CompletionResultSet.java:149)
            at com.intellij.codeInsight.completion.CompletionResultSet.runRemainingContributors(CompletionResultSet.java:142)
            at com.intellij.codeInsight.completion.JavaNoVariantsDelegator.fillCompletionVariants(JavaNoVariantsDelegator.java:61)
            at com.intellij.codeInsight.completion.CompletionService.getVariantsFromContributors(CompletionService.java:83)
            at com.intellij.codeInsight.completion.CompletionResultSet.runRemainingContributors(CompletionResultSet.java:149)
            at com.intellij.codeInsight.completion.CompletionResultSet.runRemainingContributors(CompletionResultSet.java:142)
            at com.intellij.codeInsight.completion.JShellCompletionContributor.fillCompletionVariants(JShellCompletionContributor.java:28)
            at com.intellij.codeInsight.completion.CompletionService.getVariantsFromContributors(CompletionService.java:83)
            at com.intellij.codeInsight.completion.CompletionResultSet.runRemainingContributors(CompletionResultSet.java:149)
            at com.intellij.codeInsight.completion.CompletionResultSet.runRemainingContributors(CompletionResultSet.java:142)
            at com.intellij.codeInsight.template.impl.LiveTemplateCompletionContributor$2.addCompletions(LiveTemplateCompletionContributor.java:90)
            at com.intellij.codeInsight.completion.CompletionProvider.addCompletionVariants(CompletionProvider.java:36)
            at com.intellij.codeInsight.completion.CompletionContributor.fillCompletionVariants(CompletionContributor.java:152)
            at com.intellij.codeInsight.completion.CompletionService.getVariantsFromContributors(CompletionService.java:83)
            at com.intellij.codeInsight.completion.CompletionService.performCompletion(CompletionService.java:112)
            at com.intellij.codeInsight.completion.CompletionProgressIndicator.calculateItems(CompletionProgressIndicator.java:839)
            at com.intellij.codeInsight.completion.CompletionProgressIndicator.access$600(CompletionProgressIndicator.java:92)
            at com.intellij.codeInsight.completion.CompletionProgressIndicator$1CalculateItems.run(CompletionProgressIndicator.java:821)
            at com.intellij.openapi.application.impl.ApplicationImpl.tryRunReadAction(ApplicationImpl.java:1130)
            at com.intellij.codeInsight.completion.AsyncCompletion.tryReadOrCancel(CompletionThreading.java:181)
            at com.intellij.codeInsight.completion.CompletionProgressIndicator.lambda$startCompletion$6(CompletionProgressIndicator.java:832)
            at com.intellij.codeInsight.completion.AsyncCompletion.lambda$null$0(CompletionThreading.java:108)
            at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$1(CoreProgressManager.java:157)
            at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:543)
            at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:488)
            at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:94)
            at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:144)
            at com.intellij.codeInsight.completion.AsyncCompletion.lambda$startThread$1(CompletionThreading.java:104)
            at com.intellij.openapi.application.impl.ApplicationImpl$1.run(ApplicationImpl.java:315)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
            at java.util.concurrent.FutureTask.run(FutureTask.java:266)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
            at java.lang.Thread.run(Thread.java:745)

这个错误困扰了我半年多了没法解决(自己菜),然后到 IDEA 社区找到了这个

Strange behaviour after update to 2017.3 when editing primefaces xhtml files

我看见这位大哥和我报错的关键字相同,我就看了一下,发现最初的 studio 3.1 版本就是基于 IDEA 2017.3 的(studio 3.0 基于 2017.1),所以我在项目中同时使用 config.gradle 和 Databinding 在 studio 3.0 运行正常,在 3.1 3.2 版本直接爆炸。

嗯,然后我就把这个 bug 归结为 IDEA 的更新导致 studio 上出了这个问题,社区中说这个 bug 已经在后面版本中解决,然而 studio 的高级版本还是没法使用。

最后

困扰多时的问题终于在 studio 3.3 版本解决了,但是现在 3.3 版本还是预览版,想使用 Databinding 的同学可以去下载

  • Android Studio 3.3 预览版
  • Android Studio 3.0.1 正式版

你可能感兴趣的:(Android studio Databinding 报 different providers 错误)