I have a .app domain registered and wanted to rename android application package with .app in the part, however after I changed the package name to .aap to .app view binding is not working and can't import R class
CodePudding user response:
There are two possible causes of this issue:
- Incorrect Import
- Build cache issue.
Incorrect Import
- Generally, the ViewBinding classes are generated using the ApplicationID specified in AndroidManifest and
build.gradle
file. - So, there is a chance, when you changed the application ID (a.k.a. packages), the import statements for those packages are not updated.
- Just update those import statements with correct package names and you are good to go.
Build cache issue
This is a common issue of build cache. Generally, the ViewBinding is created based on your Application ID specified in AndroidManifest and
build.gradle
file.When you update the package name, the build cache is sometimes marked as dirty (i.e. invalid or expired). Which doesn't allow you to access some auto generated classes like ViewBindings/DataBindings/Dependency injection classes.
The only solution for this problem is performing a clean build as follow:
- In this Android Studion, from the menu click on
Build -> Rebuild Project
. - From terminal, in your project directory perform
./gradlew clean build
.
- In this Android Studion, from the menu click on
In case your project build is failing, try to temporarily comment the code base causing the build failure and try above steps again. Once your build is successful, you can now revert back the commented changes. (NOTE: While uncommenting the codebase, make sure import statements are correct, i.e. they are using the latest package name).