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

Refresh

April 2019

Views

286 time

5

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

3 answers

0

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.

0

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
10

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.