简体   繁体   中英

Refactor/rename package name in Android Studio not working as it should

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?

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.

在此处输入图像描述

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

在此处输入图像描述

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

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM