'Change Package Name after implementation of data binding

I'm using Databinding with one of my project with project name com.abc.def. I've related all my views with binding like

ActivityLoginBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_login);

it worked fine but if I change my package name to com.Abc.Def it generated following error at the time of building Apk.

Cause: couldn't make a guess for com.Abc.Def.databinding.ActivityLoginBindingImpl .

Please Note:

  1. I have an old build with com.Abc.Def on playstore already live and I'm updating the version. That's why I have to Change package name.
  2. I can't remove Databinding from whole project.as it relates to all views.
  3. If I change my package name to old one ,it works fine.

I have already tried clean , rebuild and invalidate cache and restart .but no luck.



Solution 1:[1]

I just bumped into the same issue. I was able to fix it by toggling databinding.enabled inside Build.gradle (Module). Below is a little step-by-step guide, that I went through after renaming my company package (com.abc.myapp -> com.xyz.myapp), which got databinding to work as expected:


  1. Build > Clean Project
  2. go to your Build.gradle (Module) and disable databinding:

    android { dataBinding { enabled = false } }

  3. File > Sync Project with Gradle Files

  4. Build > Rebuild Project (no surprise: you'll get a ton of error messages)
  5. Now enable databinding again:

    android { dataBinding { enabled = true } }

  6. File > Sync Project with Gradle Files

  7. Build > Rebuild Project


Note: Some steps here may be unnecessary, but a little extra sanity checking has never done any harm during project setup, right!?

Solution 2:[2]

According to JAVA package naming conventions: The package name may contain uppercase or lowercase letters[a-z], numbers, and underscores [ _ ]. You can not use capital letters in naming packages.

Solution 3:[3]

com..Abc.Def.databinding.ActivityLoginBindingImpl .

Check if there is no empty package there, for those ..

Solution 4:[4]

first of all, did you changed package name only in Manifest? note that it could be different to applicationId - so you can only change it and leave app package as it was.

RCA: probably OS you are using to build is case-insensitive but java compiler is - that's reason why it can't find classes. Bindings are generated alongside other generated classes (for example dagger 2 classes generated by annotation processor), each generator creates own files within folder structure that reflects class package BUT if packages differ only with big/small letters, second generator will use same folder with wrong name. The reason is if OS is case-insensitive it assumes that folder already exist but java compiler not.

Other solution (except leaving app package as it is) is to :

  • rename all packages in app to other that differ to app package or to use OS that is case-sensitive (macOS could be formatter this way or linux)

Solution 5:[5]

I had the same issue, After spending several hours had to change the name of the layout to make it work.

Steps I followed to make it work.

  1. Tried clean build, invalidate cache, enable/disable binding. ( didn't work)
  2. Got a suggestion from one of my fellow developer to recreate and rename the XML file. (It worked) !!

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 Basti Vagabond
Solution 2 Himanshu Gupta
Solution 3 Maksym V.
Solution 4
Solution 5 Pranjal Khandelwal