4

I want to change the package name of my Android project using Android Studio from let's say, com.company.myapp to com.name.company.myapp. While there are many questions and answers that deal with renaming a part of existing package name but I couldn't find any help on how to add a new part to it.

Please help me in this regard.

Thanks

Shahood ul Hassan
  • 745
  • 2
  • 9
  • 19
  • 1
    This might help you. [[StackOverFlow] Android Studio Rename Package](https://stackoverflow.com/questions/16804093/android-studio-rename-package) – Minh Dao Oct 19 '18 at 03:30
  • @minhold That's the first one i read before posting my question but see it deals with "if you want to change com.example.app to my.awesome.game". In my case, I want to add a new directory in the overall package name. It is not as simple as renaming one directory. Thanks anyway. – Shahood ul Hassan Oct 19 '18 at 03:50
  • 1
    Possible duplicate of [Android Studio Rename Package](https://stackoverflow.com/questions/16804093/android-studio-rename-package) – Blasanka Oct 19 '18 at 04:47
  • it is enough to change `applicationId` in `build.gradle` for the app to change package. You don't need to move any sources to any new directories – Vladyslav Matviienko Oct 19 '18 at 04:59
  • In my opinion is the best way of rename package name is CMD + Shift + R in mac os to rename whole application package name. it is the best way of change package name. – Yogesh Sarvaiya Oct 19 '18 at 10:06
  • Don't forget to rename the `android#namespace` in the `build.gradle` of your module. `Refactor name` won't do that automatically. If you don't, all import references will be broken because the namespace is still using the previous name. – Jcorretjer Apr 20 '23 at 21:30

5 Answers5

8

Changing the application ID

(which is now independent of the package name) can be done very easily in one step. You don't have to touch AndroidManifest. Instead do the following:

1. Right click on the root folder of your project.
2. Click "Open Module Setting".
3. Go to the Default tab.
4. Change the applicationID to whatever package name you want. Press OK.

P.S. if you're using google-services.json , make sure you update new ID there too.

Teekam Suthar
  • 529
  • 1
  • 9
  • 20
7

Rename package name

  1. From project pane (left), click "Show option menu" it has settings like icon. Then from menu select "Compact Middle Packages"
  2. Then you will observe your main package.name folder expands. Right-click your (Package.name/ middle/ last name of your package name) expanded > Refactor > Rename
  3. Click "Do Refactor" from expanded Find tab at the bottom left of it.
  4. Finally click build.gradle (app-level), then change applicationId to 'your.new.packageName'. Then SYNC NOW
  5. Done
saidi mmaka
  • 91
  • 1
  • 3
1

I tried more than 10 approaches and none of them worked (and sometimes not explained completely, so I deviated a little and found my own way. Again, my way is also not simple, but it does the job and Google Play didn't give me any errors.

My Solution

1. On the Project pane on the left, select Project (it might say Android).
2. Go to nameOfYourProject -> app ->src -> main ->java ->com -> example
3. Right click on "example" -> Refactor -> Rename and type your desired name, select the two check boxes and hit Refactor.
4. Now go one level above example (folder) and right click on "com", but this time DO NOT check the two check boxes (as it will also rename the "com.google...." parts of your project) and hit Refactor.
5. Now open your Manifist file and manually change the package to your desired name.
6. Rebuild your project. If you see any errors, just open the file with the error and change the package name manually.
7. Open your gradle.build file and change application ID, to your new package (only the parts of it, not everything) and hit Sync (top right side). Hopefully this works.

P.S. Another "easy way" might be to use Agent Ransack, search for the package name by enclosing it with quotations and manually change the package name. You might want to get a copy of your project before doing this. I didn't try it, cause I was already knee-deep with all the other methods here and didn't try.

Community
  • 1
  • 1
user258959
  • 91
  • 6
0

This answer from the link in the comment provided would be most likely to have any issues arise, and would be simple. Possibly tedious depending on your app. I attempted changing package name once and android studio does not accomplish this very well. Simple solution

Vahalaru
  • 380
  • 4
  • 15
  • Thanks for your suggestion. However, it ended up with the following build error: Caused by: org.gradle.api.GradleException: No matching client found for package name 'com.name.company.myapp' at com.google.gms.googleservices.GoogleServicesTask.action(GoogleServicesTask.java:114) – Shahood ul Hassan Oct 19 '18 at 05:22
  • Run clean and build if you still have an error use find and replace. Find your old package name and replace new page name. Also is your app registered with any Api's? If so you might want to update those. – Vahalaru Oct 19 '18 at 06:08
  • Above error was due to google-services.json. It had the same package name as before. I had to change it at two places there manually. Then there were more errors relating to Room library. I think I would make a new project with changed package name and copy & paste all the classes there. Nothing is gonna be any cleaner than that. Android studio team should address this issue, however! – Shahood ul Hassan Oct 19 '18 at 11:11
-2

To Rename APP package:

  1. create a new package with the desired name by right clicking on the Java folder → New → Package;
  2. Cut then paste all your classes to the new package. Android Studio will refactor the package name everywhere;
  3. delete the old package;
  4. change AndroidManifest.xml and build.gradle file to the new package if you use this method.

Note: it’s more safe to make a new project.

Cosmin Staicu
  • 1,809
  • 2
  • 20
  • 27
Aml Salman
  • 11
  • 1