5

First off, I acknowledge the countless posts about refactoring package name. I researched many of them and none of the answers worked for me... I'm wondering if anyone else has been having such an issue or if anyone knows how to fix this.

Here's what I've attempted, pretty much every different variant answer in all of these threads and others not included here:

Rename package in Android Studio

Refactor package name in Android Studio

How do I rename the android package name?

When I created my project I was greener grass than I am now and so just made the company name my name. Now that I'm about to launch my app I realize that having my name as package name "company" does not look as polished and professional as having my company, obviously. So I'm trying to refactor the package name as follows:

Current = firstlast.appname

Rename = com.companyname.appname

Problems I'm having: I try refactoring by actually refactoring and IntelliJ does not actually refactor everything, I change package name in the manifest as well as gradle, and still doesn't work. Then what's worse is that after cleaning up all the errors new packages pop up randomly in the project directory with the old package names, I delete these, and then more pop up. I delete these again and finally am left with the new package directories only however, I clean and rebuild and get no errors but when I try to launch I get: Class referenced in the manifest was not found in the project or the libraries, something relating to dex... I trace this down and fix these errors but get nowhere. At this point my project was so screwed up I considered deleting it and using a backup... Instead, I used history to go back in time...

I also tried creating a new package and moving all the classes and files to the newly created package and delete the old but this has its own can of worms that seems even messier than the above.

I am so lost as to why this isn't simple. Any help is much appreciated!

EDIT: Possible "go around"

I was looking at this documentation:

https://developer.android.com/studio/build/application-id.html

I used the following instruction to rename the application ID:

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: right-click on the root folder of your project. Click "Open Module Setting". Go to the Flavours tab. Change the applicationID to whatever package name you want. Press OK.

As found here:

Rename package in Android Studio

So I'm wondering, (NEW QUESTION, ONLY PERTINENT QUESTION) if I rename the application ID as I did here and it changes in the gradle build file only, does this apply as my apps package name for submission to the app store as well as sharing the app? Or do I need to actually refactor the package completely?

Syed Rafaqat Hussain
  • 1,009
  • 1
  • 9
  • 33
RobbB
  • 1,214
  • 11
  • 39

2 Answers2

12

First, you have to create your desired package name under the java package. After creating your desired package name under java then you have to select all the classes, packages, and activities and put them into the new package. you can do this by selecting all the files and cut and paste them to the new one and also select and drag and drop to the new package. I think that drag and drop is the most efficient way to do this. Delete the text and AndroidTest package under the src folder these folders are auto-generated and they will auto-generated when the new package name is created.

enter image description here

Second, you go to the build.gradle (app) then select application id

enter image description here

After selection press Ctrl + Shift + r then this screen will be appear

enter image description here

After adding your new package name then you have to click the replace all button that will replace all the old package name with the new one then sync the build.gradle

Syed Rafaqat Hussain
  • 1,009
  • 1
  • 9
  • 33
2

In Android Studio, you can do this: For example, if you want to change com.example.app to my.awesome.game, then:

  1. In your Project pane, click on the little gear icon enter image description here

  2. Uncheck the Compact Empty Middle Packages option

enter image description here

  1. Your package directory will now be broken up into individual directories
  2. Individually select each directory you want to rename enter image description here

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

Then clean and Rebuild.

enter image description here

You may need to change the package= attribute in your manifest or namespace in your build.gradle it depends on your Gradle version

Hope this will help you, original post from @aditya

Jimale Abdi
  • 2,574
  • 5
  • 26
  • 33