Search code examples
androidkotlindata-binding

Data binding library when camel case package name


I am developing a new version of an android app which has been published in the market by some previous developers with a package name like com.mycompany.MyApp and I have to use the same package name.

In my project, I have used The Data Binding Library which needs lowercase notation for package names and I have been receiving "java.lang.IllegalArgumentException: couldn't make a guess for com.mycompany.MyApp.databinding.MyFragmentRecyclerViewItemBinding"

And then according to the official documentation and also some entries in SO, I have placed

<data class="com.mycompany.MyApp.databinding.MyFragmentRecyclerViewItemBinding" ></data> 

definition on top of layout in my_fragment_recycler_view_item.xml

Now I can import the class MyFragmentRecyclerViewItemBinding in MyRecyclerViewFragment.kt and access the view elements etc. but the project can not be compiled because the auto-generated class MyFragmentRecyclerViewItemBindingImpl can not be compiled.

Gradle gives "java.lang.IllegalArgumentException: couldn't make a guess for com.mycompany.MyApp.databinding.MyFragmentRecyclerViewItemBindingImpl" and I can see the error in MyFragmentRecyclerViewItemBindingImpl class as "Cannot inherit from final 'com.mycompany.MyApp.MyFragmentRecyclerViewItemBinding'"

I guess this is because in Kotlin all the classes are default final but I am stuck here anyway. Is there anything you can suggest or can you see what I am doing wrong?

Thanks in advance for all of the reads, comments, and answers. I can not share the code directly but symbolic codes are like as follows;

MyRecyclerViewFragment.kt

import com.mycompany.MyApp.databinding.MyFragmentRecyclerViewItemBinding

class MyRecyclerViewFragment: Fragment()
{
 override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val fragmentIntroSlidePage = MyFragmentRecyclerViewItemBinding.inflate(inflater,
            container, false)
        //TODO ..
        return fragmentIntroSlidePage.root
    }
}

my_fragment_recycler_view_item.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data class="com.mycompany.MyApp.databinding.MyFragmentRecyclerViewItemBinding">

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/myBgColor"
        android:orientation="vertical">


        <ImageView
            android:id="@+id/slide_bg_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/intro_slide_1_text"
            android:scaleType="fitXY"
            android:src="@drawable/intro_slide_01"
            app:layout_constraintHeight_min="150dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

auto-generated class

package com.mycompany.MyApp.databinding;

public class MyFragmentRecyclerViewItemBindingImpl extends MyFragmentRecyclerViewItemBinding  {
...

Solution

  • "java.lang.IllegalArgumentException: couldn't make a guess for com.mycompany.MyApp.databinding.MyFragmentRecyclerViewItemBindingImpl"
    

    Basically this exception states that one can't simply use capitalised name (Camel Hump case) in package names for objects passing through data variable in data-binding.

    So solution is to making package name in lower case to get rid of the issue. For the case of O.P. it would be solved by renaming MyApp to myapp or something convenient.

    Hence final result would be:

    <data class="com.mycompany.myapp.databinding.MyFragmentRecyclerViewItemBinding">