44

I am using eclipse and I created a test android project and the package in the "gen" folder that contains R.java is currently called com.something.test (I thought I was just testing but build my whole app on it!)

This is referenced when loading the app and the phone sometimes displays it so I need to rename it. I tried this by clicking refactor but it regenerated it again with the old name!

Can I rename it?

Jonas
  • 121,568
  • 97
  • 310
  • 388
Bex
  • 4,898
  • 11
  • 50
  • 87
  • 5
    I'm not 100% sure but I think you have to change the application's package name in the manifest (then rebuild). – bigstones Mar 30 '11 at 14:34

7 Answers7

85

Right click your project, then Android Tools -> Rename Application Name:

enter image description here

Or, if you want to do it manually, go to your manifest file, change the package name, and make a Project Clean.

Cristian
  • 198,401
  • 62
  • 356
  • 264
  • I just Refactor -> Renamed my project, and Eclipse did not change the package name in Manifest. Thanks for your second answer!! – Lou Morda Sep 30 '14 at 17:15
  • I followed these steps but then I got a problem because it didnt fix the package in the manifest (just in case someone else has the same problem) – Dane411 Nov 11 '14 at 15:24
23

Check the AndroidManifest.xml, there's a package attribute on the <manifest> top-level element. That is where R.java is generated and you should be careful renaming it.

Felix
  • 88,392
  • 43
  • 149
  • 167
3

I know it's an old question, but I needed it now and maybe someone else needs it too. I'm using Android Studio 2.1.1

To make it work I had to change:

  • package - on AndroidManifest.xml
  • applicationId - on build.gradle of the app folder

This way there was no need to import R on every java class.

It is important to remember that this is not an usual change and should not be done every time.

The link applicationId vs. package name explains the difference between them and why they should not be changed.

liane
  • 51
  • 2
3

For those who attempt to manually change it, the statements that the R.java file is linked is correct. If you simply change it in the manifest, you will get a long sequence of "R cannot be resolved" errors throughout your java files that reference resources. To correct that you'll need to add an import for the R class in each of those files.

So if you had your package originally as: "com.mycomp.myapp.android" and changed it to "com.mynewcomp.myapp.android" you would need to go into each java class file and add:

import com.mynewcomp.myapp.android.R;

If you run the originally suggested rename command on the project this is done for you automatically.

Doug Porter
  • 7,721
  • 4
  • 40
  • 55
Rich
  • 39
  • 1
2

Another thing to check is if you have custom Views with xml namespaces. It took me a minute to realize I had to change the xmlns attribute.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myxmlnamespace="http://schemas.android.com/apk/res/com.mynew.packagename"
    ...
grgrssll
  • 121
  • 1
  • 3
2

It gets named to the root package of your app. If you change your app's root package to something else, R.java will exist in that package now.

Rich
  • 36,270
  • 31
  • 115
  • 154
0

I refactored my package one by one using Right Click->Refactor and Android Manifest.XML. My R file was still building on the old package name.

Closing and Opening the project fixed this issue, but I needed to go and do a manual change on all my java files.

Siddharth
  • 9,349
  • 16
  • 86
  • 148