Android data binding “Missing import expression although it is registered” after upgrade to gradle 5.0

冷暖自知 提交于 2019-12-09 07:36:04

问题


After I upgrade my Android studio to 3.4, Android Gradle Plugin to 3.4 and gradle to 5.1.1

I got the data binding errors like below

I have made sure I cleaned project and rebuild, I have cleared cache and restarted AS.

This issue never happened before the upgrade

I can confirm it is because of the new gradle update

DataBinderMapperImpl.java:54: error: cannot find symbol

e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors. ****/ data binding error ****msg:Missing import expression although it is registered

I found the solution: Solution:

  1. Now 3.4.1 released, use 3.4.1
  2. Remove all import type in layout XML
  3. Remove all string from import type in layout XML
  4. Remove all integer from import type in layout XML

回答1:


After I upgraded my Android studio and gradle plugin, I ran into similar issue because of the below line. I was using this <import type="java.lang.String" /> in my layout file. Removing this import solved the issue.

Just as in managed code, java.lang.* is imported automatically.




回答2:


I also face these errors in data binding. I tried with 'android.databinding.enableV2=true', it is not working. After I redo databinding for a layout.xml, I found these solutions. if I'm using 'android.view.View' in layout.xml, I did import View and declare variable like this,

<data>
    <import type="java.util.List" />        
    <import type="android.view.View" />
    <import type="com.example.android.mobilepos.data.pojos.ActionData" />
    <variable
        name="view"
        type="View" />
    <variable  name="actionList"  type="java.util.List&lt;com.example.android.mobilepos.data.pojos.ActionData>" />

And used variable view in like this.

 <EditText
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" 
   android:id="@+id/txt_payment_remark"                           
   android:hint="@string/hint_payment_remark"                            
   android:visibility="@{switch1.checked ? view.VISIBLE : view.GONE}" />
 <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/action_data_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/color_white"
                android:focusable="true"
                android:clickable="true"
                app:actionSource="@{actionList}"
              app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"                    app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
                tools:context=".ui.fragments.NewInvoiceFragment"
                tools:listitem="@layout/fragment_new_invoice">

I also did for Integer.toString() feature like this, but can't import and use Integer in layout.xml. So I shift Integer value to strings.xml with %d label.

 <EditText
     android:id="@+id/txt_qty"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"                        
     android:hint="@string/hint_quantity"
     android:inputType="number"
     android:maxLength="5"
     android:maxLines="1"
     android:singleLine="true"
     android:text="@{@string/shipment_qty(product.qty)}"
     android:textAlignment="center"
     android:textColor="@color/black"
     android:textStyle="bold"                        
     tools:text="1000" />

I hope these will solve your problems.




回答3:


I assume you are using gradle plugin version 3.4 (not 4.3 as you mentioned in question). See the list of available gradle-plugin version https://developer.android.com/studio/releases/gradle-plugin#updating-gradle. There is change in data binding compiler option https://developer.android.com/topic/libraries/data-binding/start#preview-compiler

To enable the new data binding compiler, add the following option to your gradle.properties file:

android.databinding.enableV2=true


来源:https://stackoverflow.com/questions/55754329/android-data-binding-missing-import-expression-although-it-is-registered-after

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!