29

Beta 3 doesn't allow to rename resource files such as layout files. It does show the renaming dialog, but when trying to apply changes it reads the following error message:

Refactoring cannot be performed
File .../myapplication/R.java is read-only

Also I have Beta 2 version in a neighbor directory and renaming works perfect. Any ideas how to fix that?

UPD: I reported the bug (#65079508), and now it is marked as a duplicate of 65032914 https://issuetracker.google.com/issues/65032914

So, the issue is even wider and if I understand it well the bug will be fixed in some coming version of the Studio.

UPD#2: Beta 4 is released. The bug is still not fixed.

UPD#3 Fixed in Beta 5.

Roman
  • 502
  • 7
  • 17
  • Same issue happening for me after the upgrade from Beta2 to Beta3, also I can no longer remove directories or files from AS. –  Aug 30 '17 at 00:14
  • Issue is open here: https://issuetracker.google.com/issues/65032914 - Please star it ;-) – Patrick Boos Sep 01 '17 at 13:16

5 Answers5

14

meh, just get rid of the generated files && then refactor.

Gradle --> app --> build --> clean.

Gradle clean project (remove generated files/folders)

wooldridgetm
  • 2,500
  • 1
  • 16
  • 22
  • 2
    Cleaning the build folder does allow to rename the file, but refactoring can't work without R.java file – Roman Sep 03 '17 at 04:41
  • Agreed. Refactoring resources doesn't work if the R class doesn't exist. After you do a gradle clean and refactor (or rename) the resource you're interested in, you must rebuild the project. – wooldridgetm Sep 04 '17 at 16:46
  • FYI: By refactor, I meant to go to the option (in Android Studio) that allows you to rename the resource ⇧F6 (macOS). The problem stated Android Studio beta didn't allow renaming. – wooldridgetm Sep 04 '17 at 16:47
  • worked but after cleaning three four times ! Does the job anyway !! – zulkarnain shah Jul 13 '18 at 09:06
2

I am experiencing the same issue on Android Studio 3.0 Beta 3. I fixed the problem by:

  1. Exiting Android Studio
  2. Navigating to the folder that the layout file is saved in (either CLI or GUI)
  3. Rename the file to whatever you want the refactored file to be called.
  4. Reopen Android Studio.

Obviously this forces you to have to manually refactor the references to the old file, but I tried about a dozen different things (changing R.java permissions, Gradle clean, invalidate cache, etc.) and none of them worked.

Anyone else who has this issue should submit this bug in Android Studio by using Help > Submit Feedback.

Also, is there a chance you might also be using multiple layouts folders like I am?

spitfyre
  • 149
  • 1
  • 5
  • No, I don't use multiple layout folders. Thanks for the advice - I've reported the bug https://issuetracker.google.com/issues/65079508 – Roman Aug 26 '17 at 18:48
2

I've got the same issue. I was solved this with

File --> Invalidate Caches and Restart --> Invalidate and Restart.

And than try to rename again.

Hope this help. Happy coding :)

0

I was able to work around it temporarily by moving my R.java file into my src directory. For example, if you're package name is com.example.sample, move your R.java file from app/build/generated/source/r/debug/com/example/sample/R.java to app/src/main/java/com/example/sample/R.java. Be sure to delete it when you're finished refactoring, it will be regenerated during the build process.

Nathan Reline
  • 1,137
  • 10
  • 13
0

Cleaning doesn't work for everything. For instance, renaming a view isn't possible because the R.java file is automatically regenerated. I tried disabling Instant Run, but that didn't help either. Eventually I had to rename the view manually in the layout file.

From the comments on the bug ticket at https://issuetracker.google.com/issues/65032914 the fix will be in the beta 5 release of Android Studio.

Craig
  • 119
  • 7