Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

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.

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.

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.

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"
    ...

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.

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top