'Android Studio can not generate Binding class while use DataBinding
- Android Studio : 3.1.0, 3.1.1
- Gradle : 4.4
- Build Tools : 3.1.0
I am using the DataBinding library. when I add a layout file to project or add any view into the layout file, Android Studio cannot generate a Binding class for the layout, until rebuilding the project.
layout file:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<data>
<variable
name="viewModel"
type="com.qk365.thweeks.MainViewModel" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/main_tab"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@android:color/white"
android:elevation="5dp"
app:tabGravity="fill"
app:tabIndicatorHeight="0dp"
app:tabMode="fixed"
app:tabSelectedTextColor="#7343ea"
app:tabTextAppearance="@style/TextAppearance.Design.Tab"
app:tabTextColor="#181818" />
<android.support.v4.view.ViewPager
android:id="@+id/main_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/main_tab">
</android.support.v4.view.ViewPager>
</RelativeLayout>
</layout>
I checked Android Studio logs, when layout file changes, there always have error message.
2018-04-17 11:20:01,699 [e-1024-b02] ERROR - .intellij.psi.impl.ElementBase - Element: class com.intellij.psi.impl.source.xml.XmlFileImpl because: different providers: SingleRootFileViewProvider{myVirtualFile=file://E:/AndroidStudioProjects/thweeks/module-order/src/main/res/layout/order_fragment_pick_up_order.xml, content=VirtualFileContent{size=2845}}(5200996b); SingleRootFileViewProvider{myVirtualFile=file://E:/AndroidStudioProjects/thweeks/module-order/src/main/res/layout/order_fragment_pick_up_order.xml, content=VirtualFileContent{size=2845}}(398a3993)
invalidated at: see attachment
com.intellij.psi.PsiInvalidElementAccessException: Element: class com.intellij.psi.impl.source.xml.XmlFileImpl because: different providers: SingleRootFileViewProvider{myVirtualFile=file://E:/AndroidStudioProjects/thweeks/module-order/src/main/res/layout/order_fragment_pick_up_order.xml, content=VirtualFileContent{size=2845}}(5200996b); SingleRootFileViewProvider{myVirtualFile=file://E:/AndroidStudioProjects/thweeks/module-order/src/main/res/layout/order_fragment_pick_up_order.xml, content=VirtualFileContent{size=2845}}(398a3993)
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.psi.impl.ElementBase$ElementIconRequest.<init>(ElementBase.java:183)
at com.intellij.psi.impl.ElementBase$ElementIconRequest.<init>(ElementBase.java:178)
at com.intellij.psi.impl.ElementBase.computeIcon(ElementBase.java:110)
at com.intellij.psi.impl.ElementBase.getIcon(ElementBase.java:86)
at com.intellij.codeInsight.lookup.DefaultLookupItemRenderer._getRawIcon(DefaultLookupItemRenderer.java:79)
at com.intellij.codeInsight.lookup.DefaultLookupItemRenderer.getRawIcon(DefaultLookupItemRenderer.java:48)
at com.intellij.codeInsight.completion.JavaMethodCallElement.renderElement(JavaMethodCallElement.java:444)
at com.intellij.codeInsight.completion.JavaCompletionUtil$4.renderElement(JavaCompletionUtil.java:468)
at com.intellij.codeInsight.completion.JavaCompletionUtil$4.renderElement(JavaCompletionUtil.java:465)
at com.intellij.codeInsight.lookup.LookupElementDecorator$VisagisteDecorator.renderElement(LookupElementDecorator.java:179)
at com.intellij.codeInsight.lookup.LookupElementDecorator.renderElement(LookupElementDecorator.java:83)
at com.intellij.codeInsight.lookup.impl.LookupCellRenderer.getListCellRendererComponent(LookupCellRenderer.java:150)
at com.intellij.ui.ExpandedItemListCellRendererWrapper.getListCellRendererComponent(ExpandedItemListCellRendererWrapper.java:38)
at javax.swing.plaf.basic.BasicListUI.paintCell(BasicListUI.java:209)
at com.intellij.ui.components.WideSelectionListUI.paintCell(WideSelectionListUI.java:56)
at javax.swing.plaf.basic.BasicListUI.paintImpl(BasicListUI.java:317)
at javax.swing.plaf.basic.BasicListUI.paint(BasicListUI.java:240)
at com.intellij.ui.components.WideSelectionListUI.paint(WideSelectionListUI.java:32)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:780)
at com.intellij.ui.components.JBList.paintComponent(JBList.java:186)
at javax.swing.JComponent.paint(JComponent.java:1056)
at com.intellij.ui.components.JBList.paint(JBList.java:118)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JViewport.paint(JViewport.java:728)
at com.intellij.ui.components.JBViewport.paint(JBViewport.java:236)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:586)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:306)
at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:762)
at java.awt.EventQueue.access$500(EventQueue.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:715)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:732)
at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:822)
at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:650)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:366)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
2018-04-17 11:20:01,699 [e-1024-b02] ERROR - .intellij.psi.impl.ElementBase - Android Studio 3.1.1 Build #AI-173.4697961
2018-04-17 11:20:01,699 [e-1024-b02] ERROR - .intellij.psi.impl.ElementBase - JDK: 1.8.0_152-release
2018-04-17 11:20:01,699 [e-1024-b02] ERROR - .intellij.psi.impl.ElementBase - VM: OpenJDK 64-Bit Server VM
2018-04-17 11:20:01,699 [e-1024-b02] ERROR - .intellij.psi.impl.ElementBase - Vendor: JetBrains s.r.o
2018-04-17 11:20:01,699 [e-1024-b02] ERROR - .intellij.psi.impl.ElementBase - OS: Windows 10
2018-04-17 11:20:01,699 [e-1024-b02] ERROR - .intellij.psi.impl.ElementBase - Last Action: EditorChooseLookupItem
Solution 1:[1]
I had the same problem. No matter what I tried it was not able to generate binding class. The trick was that you have to rename your layout xml file. Say you have a xml file my_layout.xml then rename it to my_layout_temp.xml. Now the binding class will be generated with name MyLayoutTempBinding. If you don't like the name then you can rename it back to my_layout.xml. At the end it will solve the binding class generate issue.
Solution 2:[2]
Note: For initial debugging steps see Khemraj's answer here Data Binding class not generated
- Android Studio : 3.1.4
- Gradle : 4.4
- Build Tools : 28.0.0
I can see the auto-generated binding classes momentarily after issuing a
File > Invalidate Caches / Restart
but only while the indexing is paused, immediately after Android Studio restarts (I should probably use something other than "databinding" for a test project name):
While paused, ActivityMain2Binding
will be recognized by compiler, however the build and make operations are not available. Shortly after resuming the indexing the associated binding classes are removed and the IDE error mentioned in the OP is logged.
Update (2018.09.11)
Using a tip found in the bug post commented above (https://issuetracker.google.com/issues/77607040), I changed the Data Binding setting to
I then added a reference to the generated class
Solution 3:[3]
I have the same error in the same environment as you. But unfortunately, I could not resolve this problem with Android Studio 3.1.1. For now, I'm using Android Studio 3.2 Canary11 and it works fine.
Solution 4:[4]
The radical solution
You should add this in the build.gradle(Module)
file to make the data binding enabled and press Sync
android {
...
buildFeatures {
dataBinding true
}
}
Solution 5:[5]
This is a bug of Android Studio. Fixed in Android Studio 3.2.1.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | W00di |
Solution 2 | |
Solution 3 | wakwak3125 |
Solution 4 | Tyler2P |
Solution 5 | Victor |