Search code examples
androidandroid-viewbinding

Adding buildFeatures { viewBinding true } results in "Cannot find a setter for <... android:visibility> that accepts parameter type 'int'


I want to start using viewBinding in our project but the mere addition of the configuration results in a compile error:

android {
    buildFeatures {
        dataBinding true
        viewBinding true // new line and only change
    }

results in:

e: /home/leo/StudioProjects/android-wallet/mbw/build/generated/source/kapt/btctestnetDebug/com/mycelium/wallet/DataBinderMapperImpl.java:37: error: cannot find symbol
import com.mycelium.wallet.databinding.FragmentBequantAccountBindingImpl;
                                      ^
  symbol:   class FragmentBequantAccountBindingImpl
  location: package com.mycelium.wallet.databinding




Cannot find a setter for <com.mycelium.wallet.databinding.ItemBequantSearchBinding app:visibility> that accepts parameter type 'int'

If a binding adapter provides the setter, check that the adapter is annotated correctly and that the parameter type matches.

The offending code is:

    <data>

        <import type="android.view.View" />

        <variable
            name="viewModel"
            type="com.mycelium.bequant.market.viewmodel.AccountViewModel" />
    </data>
...
<include
    android:id="@+id/searchBar"
    layout="@layout/item_bequant_search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="@{viewModel.searchMode ? View.VISIBLE : View.GONE}" removing="this line fixes compilation"
    app:layout_constraintTop_toBottomOf="@id/hideZeroBalance" />

Changing the offending line to any of

android:visibility="@{viewModel.searchMode ? `visible` : `gone`}"
app:visibility="@{viewModel.searchMode ? View.VISIBLE : View.GONE}"

results in similar errors.

I read I might have to define a BindingAdapter but why and where?

I tried adding


    @BindingAdapter("visibility")
    fun setVisibility(target: View, visible: Boolean) {
        target.visibility = if (visible) View.VISIBLE else View.GONE
    }

to AccountFragment which inflates above xml file changing the xml to

android:visibility="@{viewModel.searchMode}"

but this appears to have no effect.

Both fragment_bequant_account.xml and item_bequant_search.xml use androidx.constraintlayout.widget.ConstraintLayout instead of androidx.constraintlayout.ConstraintLayout.

I tried to put a @BindingAdapter into the AccountViewModel as suggested here but with no success.


Solution

  • I had the same problem in my project. I used databinding in my code and had dataBinding true in the gradle. As soon as I added viewBinding true I got the same error pointing to the xml line android:visibility="@{viewModel.searchMode ? View.VISIBLE : View.GONE}"

    To fix, I added the tools:viewBindingIgnore="true" attribute to the root view of a certain layout file so that layout is ignored while generating binding classes.

    You can see documentation on the tools:viewBindingIgnore="true" attribute at https://developer.android.com/topic/libraries/view-binding#data-binding