Fully change package name including company domain

158,601

Solution 1

You can do this:

Change the package name manually in the manifest file. Click on your R.java class and the press F6 (Refactor->Move...). It will allow you to move the class to other package, and all references to that class will be updated.

reference: How do I rename the android package name?

Solution 2

current package : com.company.name

New package : com.mycomapny.name

Steps: 1) Suppose you are at this screen which is shown below.

enter image description here

2) Open project pane and click on settings icon.

3) Deselect Compact Empty Middle Packages.

4) Then your package is now broken into individual parts as shown below .

5) right click on "company" select Refactor -> select rename ->rename directory.

enter image description here

6) Now your "company" has been changed to new "mycomapny" and changes were reflected as shown in below figure.

enter image description here

7) Now change the package name in AndroidManifest.xml file .

enter image description here

8) Open app level build.gradle and change package name .

enter image description here

9) you will get errors as Cannot resolve symbol "R" .

10) Remove line which gives this error and studio will import new R file automatically.

11) If you have multiple files then use find and replace option by pressing " Cntrl+Shift+R "

Or " Select Edit->Find->Replace in path.."

enter image description here

12) select Replace All.

Solution 3

This worked for me, from https://stackoverflow.com/a/18637004/127434

Another good method is: First create a new package with the desired name by right clicking on the java folder -> new -> package.

Then, select and drag all your classes to the new package. AndroidStudio will refactor the package name everywhere.

Finally, delete the old package.

Solution 4

In Android Studio, you can do like this:

For example, if you want to change com.example.app to iu.awesome.game, then:

  1. In your Project pane, click on the little gear icon ( enter image description here )
  2. Uncheck / De-select the Compact Empty Middle Packages option enter image description here
  3. Your package directory will now be broken up in individual directories
  4. Individually select each directory you want to rename, and:

    • Right-click it
    • Select Refactor

    • Click on Rename

    • In the Pop-up dialog, click on Rename Package instead of Rename Directory

    • Enter the new name and hit Refactor

    • Click Do Refactor in the bottom

    • Allow a minute to let Android Studio update all changes

    • Note: When renaming com in Android Studio, it might give a warning. In such case, select Rename Allenter image description here

      1. Now open your Gradle Build File (build.gradle - Usually app or mobile). Update the applicationId in the defaultConfig to your new Package Name and Sync Gradle, if it hasn't already been updated automatically:enter image description here

      2. You may need to change the package= attribute in your manifest.

      3. Clean and Rebuild.

      4. Done! Anyway, Android Studio needs to make this process a little simpler.

Solution 5

@Luch Filip's solution works well if you just want to rename the App package. In my case, I also want to rename the source package too, so as not to confuse things.

Only 2 steps are needed:

  1. Click on your source folder e.g. com.company.example > Shift + F6 (Refactor->Rename...) > Rename Package > enter your desired name.

  2. Go to your AndroidManifest.xml, click on your package name > Shift + F6 (Refactor->Rename...) > enter same name as above.

Step 1 will automatically rename your R.java folder, and you can build straight away.

Share:
158,601
Filip Luchianenco
Author by

Filip Luchianenco

Passionate and energetic engineer. I love my family, I love what I do, and I love efficient teamwork. I tend to think that persistent people together lead to successful collaborations, increasing the chances of achieving the common goals.

Updated on September 19, 2021

Comments

  • Filip Luchianenco
    Filip Luchianenco almost 3 years

    Let's suppose this is the package name: package com.company.name. How do I change company?

    P.S. I saw how to change name but not company. I'm using Android Studio.

  • kaay
    kaay almost 9 years
    All good, except I get "IOException: cannot rename (...)" for the main source folder. Simple project, just opened.
  • LarsH
    LarsH about 8 years
    This worked partially for me, but it didn't fix the "import com.company.foo" statements. I was able to do the latter with a global replace in the src/ directory.
  • James Grey
    James Grey about 8 years
    I use Android studio 2.1.1 . My specific For example, at first, I have info.apachehive.cardview, I want have net.vyhn.cardview. Step 1. Choose tree view source code. Step 2. Click apachehive, press Shift+F6 Refactor\Rename to vyhn. Step 3. Click info folder, press Shift+F6 Refactor\Rename to net. File AndroidManifest.xml automatically change. Then I run debug, my application works well.
  • Asim K T
    Asim K T almost 8 years
    May be you can add some screenshots.
  • Amir Dora.
    Amir Dora. almost 6 years
    that's not the most reliable way, simply refactoring does the job, imagine someone has more complex project, that has styles, dimens, drawables, libraries. This method takes a lot of time.
  • Martin Jovial
    Martin Jovial almost 6 years
    It's reliable if you follow my method step by step and it is very speedy if you follow them. Just forget about any complexity and consider you're creating a new project from existing source, then as I can see you don't know where drawable dimens libraries stays, I'll explain to you then you have to upvote my answer. 1- libraries are iside your build.graddle so if you backed up the file you would just have to copy the content inside the new build.graddle 2- drawables, dimens, styles are all inside the res folder. so if you have both projects new/old(package) opened u just have to drag/drop
  • Martin Jovial
    Martin Jovial almost 6 years
    Sometime refractoring is more complicated than you can imagine because I tried it without success then using my method every things worked like a charm
  • Amir Dora.
    Amir Dora. almost 6 years
    Yes Martin, agree this is also one way and it works. But it is not the fastest method. Thanks for detailed answer.
  • Hasan El-Hefnawy
    Hasan El-Hefnawy over 4 years
    For me, this is the best answer. Thanks.
  • Adam Burley
    Adam Burley over 3 years
    good answer, but why would you only rename the directory? this is why you get all these issues with unresolved symbol R, etc. I followed your instructions but renamed the package rather than the directory. then I didn't have to change the manifest, R references etc.
  • Chucky
    Chucky over 2 years
    Probably worth saying that I tried a few of these ways and none of them were totally straight forward. I had to manually add a few folders to get the directories right after the rename. It's working now though, so read a few of the approaches and anticipate some hiccups.