Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 208
Victor Avatar asked Apr 17 '18 05:04

Victor


People also ask

Is Android databinding deprecated?

Recently Android has announced that with Kotlin 1.4. 20, their Android Kotlin Extensions Gradle plugin will be deprecated and will no longer be shipped in the future Kotlin releases. Android Kotlin Extensions plugin brought with it two very cool features : Synthetics let you replace calls to findViewById with kotlinx.

Can I use ViewBinding with databinding?

Data binding gives you everything that view binding does. So, if you have enabled data binding for the project, just use data binding. " seems kind of silly to have even created viewBinding after data binding has already been established" -- it is a matter of build performance.

How do you create a binding class?

A binding class is generated for each layout file. By default, the name of the class is based on the name of the layout file, converting it to Pascal case and adding the Binding suffix to it. The above layout filename is activity_main. xml so the corresponding generated class is ActivityMainBinding.

What is data binding in Android Studio?

Android data binding generates a Binding class based on this layout. This class holds all the bindings from the layout properties, i.e., the defined variable to the corresponding views. It also provides generated setters for your data elements from the layout.

Why is my binding class not generating in Android Studio?

If you changed the type of some <variable in your layout and getter setter type is not changing then Clean project ( Build> Clean Project) Finally if still your binding class is not generated, then we have our most powerful weapon. - Restart Android Studio First, try just restart, this always generates variables of my binding layout after restart.

How to get data binding class from a project?

If still your class is not generated. (Some time when we paste layout file, then it happens). Then Rebuild Project from Build> Rebuild (Not Build or Make project). It will generate your data binding class. (Rebuild does Magic for me.)

What is the use of user variable in android Data Binding?

The user variable within data describes a property that may be used within this layout. Android data binding generates a Binding class based on this layout. This class holds all the bindings from the layout properties, i.e., the defined variable to the corresponding views. It also provides generated setters for your data elements from the layout.


Video Answer


1 Answers

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.

like image 160
W00di Avatar answered Dec 26 '22 15:12

W00di