Exception during databinding generation after upgrading to gradle plugin 3.0.1 - android

I upgraded a project to Gradle plugin 3.0.1, but during the databinding classes generation, I get the following error (and the binding classes are not generated):
error: cannot generate view binders java.lang.NullPointerException
at android.databinding.tool.store.SetterStore.getMatchingMultiAttributeSetters(SetterStore.java:642)
at android.databinding.tool.store.SetterStore.getMultiAttributeSetterCalls(SetterStore.java:529)
at android.databinding.tool.BindingTarget.resolveMultiSetters(BindingTarget.java:221)
at android.databinding.tool.LayoutBinder.<init>(LayoutBinder.java:249)
at android.databinding.tool.DataBinder.<init>(DataBinder.java:52)
at android.databinding.tool.CompilerChef.ensureDataBinder(CompilerChef.java:90)
at android.databinding.tool.CompilerChef.sealModels(CompilerChef.java:203)
at android.databinding.annotationprocessor.ProcessExpressions.writeResourceBundle(ProcessExpressions.java:193)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:794)
...
After some investigation, it seems that the binding class is correctly generated if I have at most 1 attribute using data binding on a given element in a layout xml. If I add a second one, I get the previous exception.
In the context of my project, the following view causes the error, but unfortunately, in a brand new project, it compiles normally.
<?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">
<data>
<import type="android.view.View"/>
<variable
name="title"
type="String"/>
<variable
name="visible"
type="boolean"/>
</data>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- with either text or visiblility, the project builds, but not with both -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{title}"
android:visibility="#{visible ? View.VISIBLE : View.GONE}"
/>
</RelativeLayout>
</layout>
Has anyone encountered this issue ?

Related

Unable to parse XML - Kotlin

Caused by:
org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A
failure occurred while executing com.android.build.gradle.internal.res.ParseLibraryResourcesTask$ParseResourcesRunnable
Other error codes
FAILURE: Build completed with 2 failures.
1: Task failed with an exception.
What went wrong:
Execution failed for task ':app:parseDebugLocalResources'.
A failure occurred while executing com.android.build.gradle.internal.res.ParseLibraryResourcesTask$ParseResourcesRunnable
Failed to parse XML file '/home/shinto/Documents/PostMethodTrialChecking/HelpIntern/app/build/intermediates/packaged_res/debug/layout/fragment_signup.xml'
fragment_signup.xml
<data>
<variable
name="users"
type="com.shinto.helpintern.MainViewModel" />
</data>
android:visibilities="#{users.}"
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/progBar"
android:layout_width="match_parent"
android:layout_height="match_parent">
Your XML code is wrong. And as the error suggests, compiler expects a <, means an opening tag. Why?
Because this line
android:visibilities="#{users.}" is misplaced and isn't in any tag. Even if it was in its correct position, it wouldn't work, because there's nothing called visibilities, it's visibility.
I assume you want to toggle the visibility of the ContraintLayout based on any (assuming boolean) value of the data. To do that, change your code as:
<data>
<import type="android.view.View"/>
<variable
name="users"
type="com.shinto.helpintern.MainViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/progBar"
android:visibility="#{users.yourValue? View.GONE : View.VISIBLE}"
android:layout_width="match_parent"
android:layout_height="match_parent">
This will toggle the layout's visibility based on the value of your model.

AAPT: error parsing XML: unbound prefix although namespace is defined

I'm trying to transition my app to the action bar. I'm following Google's tutorial. Unfortunately, when trying to build the app, AAPT returns the following error:
menu.xml:6: AAPT: Error parsing XML: unbound prefix
I did some research and found out that this error usually happens when the line xmlns:android="http://schemas.android.com/apk/res/android" isn't included in the XML because then AAPT doesn't know how to interpret the android.xxx tag. However, as you can see from my code, I've included this line in the first node:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
</LinearLayout>
So why doesn't this compile?
You have not declared the app namespace via xmlns:app, but you use it in app:popupTheme. IIRC, there is a quick-fix for that in Android Studio. If not, add xmlns:app="http://schemas.android.com/apk/res-auto" to your root element.

Android Studio shows errors in layout.xml

I'm using data binding library and I experience following issues in Android Studio 3.0:
somelayout.xml:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="meeting"
type="some.package.MeetingStatusResponse"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{meeting.title}"
tools:text="Title"
/>
...
</LinearLayout>
</layout>
In this place: android:text="#{meeting.title}" Android Studio 3.0 underlines the # symbol and informs about error:
Error:(29, 27) <expr> or <lambda expression> expected, got '#'
Model class is written in Kotlin if it has any relevance.
The code completion is also not working for bound classes. The application can be successfully build using both, gradle command and Android Studio run app button. So this is just editor issue.
I observe this issue on Mac. On Windows it works ok. I do not have more computers to check if it's platform related issue.
Maybe you forgot to add in gradle compileOptions ?
`sourceCompatibility JavaVersion.VERSION_1_7`
`targetCompatibility JavaVersion.VERSION_1_7`

Error inflating class android.support.design.widget.NavigationView Android

I am able to test my android app on my mobile 5.0 perfectly fine, but testing it on anything lower than 5.0 throws an exception.
"Binary XML file line #16: Error inflating class android.support.design.widget.NavigationView"
My line 16 is basically this
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/main_app_bar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
I did see other posts about this error, but none of it seems to fix my issue.
This is the full callstack of the error
http://pastebin.com/raw/FaJQFCie
I found it. The error comes from navigation's menu. When I created new project with navigation drawer, Android Studio generates complete work for me. There's a drawable folder named drawable-v21 which stores the icon xml files
I had had the same problem some time ago.
1) Look at your gradle dependencies: you should add library -
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
2) Look at your based colors: you should have
colorPrimary
colorPrimaryDark
colorAccent
3) Look at your layout.nav_header_main. If you will have in the end of error log some message like "Caused by: java.lang.OutOfMemoryError: Failed to" - background image of nav_header_main has a very big resolution or kb weight. Change it and will have success ;)

Data binding expression not compiling

I'm trying out the new data binding library. I have a weird issue where binding the visibility property is not compiling.
This is a simplified version of the xml file:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="header"
type="com.example.EmailHeader" />
</data>
<RelativeLayout ... >
<TextView
...
android:text="#{header.senderName ?? header.senderAddress}"
android:visibility="#{header.hasAttachment ? View.VISIBLE : View.INVISIBLE}" />
</RelativeLayout>
</layout>
I get the follow message when compiling:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:Identifiers must have user defined types from the XML file. View is missing it
Everything compiles (and works!) when I remove the android:visiblity declaration.
I don't see what I'm missing here
Inside of the data tag you need to also add:
<import type="android.view.View" />
Problem persisted despite adding <import type="android.view.View" /> to my data tag.Finally found the error to be caused by a mismatch of my variable name and object of my POJO class.
This was my data tag:
<data>
<import type="android.view.View" />
<variable
name="employee"
type="com.example.Employee"/>
</data>
and I was using:
<TextView
...
android:text="#{user.lastName}" />
instead of:
<TextView
...
android:text="#{employee.lastName}" />
Forgot to change it after copying code from documentation.
Look out for mistakes like this which are hard to detect for newbies to DataBinding
I faced the exact same error which was caused by the fact that the POJO object was in a library project.
Just upate the build.gradle of the library to enable databinding as well as in the main project:
dataBinding {
enabled = true
}

Categories

Resources