Context
I am using a library that leverages databinding to define a custom attribute on any View
via its BindingAdapter
:
@BindingAdapter({"attribute"})
public static void bindAttribute(@NonNull View view, String attribute)
{...}
This lets me define custom attributes that are processed at runtime by the library:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:algolia="http://schemas.android.com/apk/res-auto">
<LinearLayout
<!-- ... -->
android:orientation="horizontal">
<ImageView
<!-- ... -->
algolia:attribute='@{"objectID"}'/>
</LinearLayout>
</layout>
This view is then used as a subclassed RecyclerView's ViewHolder's itemLayout
.
All of this works fine in my Java application.
Problem
I started building a similar app in Kotlin. To use databinding, I followed the documentation's guide and added the following:
// ./app/build.gradle
apply plugin: 'kotlin-kapt'
android {
dataBinding {
enabled true
}
...
}
dependencies {
kapt "com.android.databinding:compiler:$plugin_version"
...
}
// ./build.gradle
ext.plugin_version = '2.3.3'
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
...
}
However, running the app and displaying the RecyclerView
triggers an Exception in its onCreateViewHolder
:
08-31 14:32:00.511 22067-22067/com.algolia.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.algolia.app, PID: 22067
android.content.res.Resources$NotFoundException: Resource ID #0xffffffd6
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:190)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2105)
at android.content.res.Resources.getLayout(Resources.java:1119)
at android.view.LayoutInflater.inflate(LayoutInflater.java:424)
at android.databinding.DataBindingUtil.inflate(DataBindingUtil.java:116)
at android.databinding.DataBindingUtil.inflate(DataBindingUtil.java:88)
at com.algolia.instantsearch.ui.views.Hits$HitsAdapter.onCreateViewHolder(Hits.java:433)
at com.algolia.instantsearch.ui.views.Hits$HitsAdapter.onCreateViewHolder(Hits.java:398)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6367)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5555)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3025)
at android.view.View.measure(View.java:19909)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:872)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:924)
at android.view.View.measure(View.java:19909)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1391)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:784)
at android.view.View.measure(View.java:19909)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:19909)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19909)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19909)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19909)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:691)
at android.view.View.measure(View.java:19909)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
08-31 14:32:00.511 22067-22067/com.algolia.app E/AndroidRuntime:
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6343)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:241)
at android.app.ActivityThread.main(ActivityThread.java:6274)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
I don't understand how the same code in a Kotlin project can trigger a Resources$NotFoundException
. Moreover, the Resource ID seems odd: 0xffffffd6
is equal to 4294967254
, which is too big to be a valid integer.
What could cause such an issue? Am I missing something to use databinding's BindingAdapter
in a Kotlin app?
The issue was caused by the underlying library sending an invalid
View
resource ID.