'Databinding generate included layouts as view in aar file
My project contains multiple modules, and I am using aar files of other modules contains custom views and components. so I have an XML in .aar and I want to use it in my project.
Of course, I can but DataBinding doesn't generate it in the generated file, so I don't have access to XML's components and widgets.
My fragment's XML is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Comes from AAR file -->
<include
android:id="@+id/toolbarLayout"
layout="@layout/simple_tool_bar" />
Simple toolbar is:
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="@dimen/simple_tool_bar_height"
app:elevation="0dp">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navigationIcon="?attr/homeAsUpIndicator"
app:navigationIconTint="@color/primary" />
</com.google.android.material.appbar.AppBarLayout>
And Databinding generated file is:
public abstract class FragmentTestBinding extends ViewDataBinding {
@NonNull
public final View toolbarLayout;
So as you can see in the generated file toolbarLayout is an instance of View but it should be SimpleToolBarBinding.
When you are working on a project that contains modules instead of .aar it everything is working well, but after generating the .aar file and use it in another project it doesn't.
Solution 1:[1]
The difference between aar and moudle is that depends on moudle will generate moudlpackagename-binding_classes.json by DataBindingMergeDependencyArtifactsTask,in the build/intermediate/data_binding_base_class_logs_dependency_artifacts dir. The ***-binding_classes.json is just a copy from the moudle library's build output. I cannot find a way to config the aar which can tell the AGP that it contains the generated viewbinding classes and use the generate classes instead of just inflated a View for the include xml layout tag.
So, is there any other solution for this problem?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | RilkeZhu |
