3

We have our application with declared package name as com.foo.app in AndroidManifest.xml

For some special reason to play some trick, we want to intentionally specify the package name of the generated R.java with package name com.bar.resource. And without change the application package name (still kept as com.foo.app).

For example, I have my application using com.foo.app, I want the generated R.java's package name to be com.bar.resource. And in the source code of my application, I will use reference like com.bar.resource.id.btn_ok

We know that we should not touch the content of R.java. And of course we can manually change it and build the final application since it is just a static class with constants.

But is there any configurable option in ADT/Eclipse to do this for an Android project?


Please stop telling me how to change the package name via Eclipse refactor method. Please correctly understand my question. I am asking how to specify a package name of autogenerated R.java. Which I want it to be different from the application package name specified in AndroidManifest.xml.

Robin
  • 10,052
  • 6
  • 31
  • 52
  • Check: [Package renaming in eclipse android project](http://stackoverflow.com/questions/3697899/package-renaming-in-eclipse-android-project) – Paresh Mayani Dec 11 '13 at 08:28
  • Thanks for your information, but I want a different package name for R.java – Robin Dec 11 '13 at 08:33
  • what do you mean by different package name for R.java? – Paresh Mayani Dec 11 '13 at 09:13
  • For example, I have my application using com.foo.app, I want the generated R.java's package name to be com.bar.resource. And in the source code of my application, I will use reference like com.bar.resource.id.btn_ok – Robin Dec 11 '13 at 10:37
  • As far as I know you can't rename or change R.java and its contents. (If you try to change contents, it will warn you and its not advisable) – Paresh Mayani Dec 11 '13 at 10:39
  • Yes I know, that is why I am here. – Robin Dec 11 '13 at 10:42

4 Answers4

1

Open src folder in the package explorer and then right click on com.foo.resource,then click on refactor then rename the package name and all relevant entries will also be renamed.

ARK
  • 165
  • 13
  • Hi, you misunderstood my question. I am asking for change the auto generated source code of R.java. Not a manually created resource package. – Robin Dec 11 '13 at 08:26
  • try renaming the gen folder and deleting the previous one – ARK Dec 11 '13 at 08:33
  • Thanks, but that is too trivial since every time eclipse will generate a new one. And I don't want to modify R.java code, just change its package name. – Robin Dec 11 '13 at 08:43
  • You can stop automatic generation of new R file by un-checking build automatically option in Project menu – ARK Dec 11 '13 at 09:00
1

Do these steps to change package name if you are using Eclipse,

  1. Right-click on the package name.
  2. Select Refactor > Rename
  3. Open the manifest file. Inside the tag, change the package name..
  4. import the package in the relevant classes as it is changed.
Sikander
  • 834
  • 2
  • 10
  • 33
1

I've run the exact same issue myself recently and found the best solution to actually have a smaller sub-project that I reference with the package name I want.

I use ADT, so I have

|Parent project|

-> |Sub project|

I use ADT and sub project is marked as a Library project, and referenced to (both in Android, Java Build Path, and Project Dependencies) so then parent project gets references to all of the resources in there, so I can give it whatever package name I want.

I've thrown a quick and dirty example of this setup on https://github.com/SixMinute/AndroidSeparatePackageResources.

It's not an absolutely perfect setup, but it definitely works the best and easiest for us, hope it helps you out.

seaders
  • 3,878
  • 3
  • 40
  • 64
1

R.java package name is depend on package name that you define in manifest file.

Sadeq9128
  • 29
  • 7