'"Cannot find symbol R" error after renaming package name

After renaming my project package name (with Refactor), I got this error:

Error:(7, 44) error: cannot find symbol class R

All of my R usages are invalid. I tried to fix it manually but it doesn't work for me. Invalidate chases/Restart doesn't help me, either.



Solution 1:[1]

Try the following :

Try deleting your R.java file , android studio will regenerate it.

Clean Project

Rebuild Project

And check:

Check the AndroidManifest.xml, there's a package attribute on the top-level element

(Well it depends on how you renamed your package name)

Solution 2:[2]

Just do the following:

Ensure that the package name at the header of the AndroidManifest file has been renamed to your new package name. This worked for me.

Solution 3:[3]

Try to change application Id in build.gradle. Or remove any imports of com.***.R in your activity and let android studio to automatically import for you.

Solution 4:[4]

Seems like, you changed the application package name but didn't change the import statements. So look for import statement of class R. It must be like

import <package_name>.R;

If it's not a problem with import statements then look whether the R.java file has generated or not. It generally doesn't get generated if there is a problem with the application layout(xml) files. The R class file is generated only when all your xml layout files are correct.

Clean the project and check.

Solution 5:[5]

R error after renaming pakage

3 packages you may see as marked, When you edit the package name make sure you change all 3 of them else you may face R error which will prompt you to press alt+enter to name it as ur old package name

Solution 6:[6]

Check your AndroidManifest.xml ( com.package1.package2 )

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.package1.package2">

90% of symbol R errors because of that line. When you refactor your old package, mostly cases this package didn't change

Solution 7:[7]

if you rename your android manifest package you have to rename the "packages com.newname" sentence at the beginning of your class.

Solution 8:[8]

You have to first close android studio. Then

  1. Delete "build" folder
  2. open the commandline and type gradlew clean
  3. Then type gradlew assembledebug
  4. If the build succeeds, feel free to go back to android studio

Solution 9:[9]

In my case, this error showed because I changed the package name without ticking the boxes (Search in comments and strings and Search for text occurrences)

enter image description here

The solution for me: Rename package again and make sure you tick the two boxes as in the photo, then Do Refactor

Solution 10:[10]

When all these answers do not work:

In Android Studio : Menu File -> Invalidate Caches and restart Android Studio

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 Community
Solution 2 Daniel
Solution 3 xd3r 0N
Solution 4 Akoder
Solution 5 Sourav Nanda
Solution 6 Benzo
Solution 7 Alberto Perez
Solution 8 Dr Deo
Solution 9 Leo S
Solution 10 Eric Aya