简体   繁体   English

重构/重命名 Android Studio 中的 package 名称无法正常工作

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

First off, I acknowledge the countless posts about refactoring package name.首先,我感谢无数关于重构 package 名称的帖子。 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 在 Android Studio 中重命名 package

Refactor package name in Android Studio 在 Android Studio 中重构 package 名称

How do I rename the android package name? 如何重命名 android package 名称?

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.现在我即将启动我的应用程序,我意识到将我的名字命名为 package 名称“公司”显然不像拥有我的公司那样优雅和专业。 So I'm trying to refactor the package name as follows:所以我试图重构 package 名称如下:

Current = firstlast.appname当前 = firstlast.appname

Rename = com.companyname.appname重命名 = 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.我遇到的问题:我尝试通过实际重构来重构,而 IntelliJ 实际上并未重构所有内容,我在清单中更改了 package 名称以及 gradle,但仍然无法正常工作。 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.然后更糟糕的是,清理完所有错误后,在项目目录中随机弹出带有旧 package 名称的新包,我删除了这些,然后弹出更多。 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.我再次删除这些,最后只剩下新的 package 目录但是,我清理并重建并且没有错误,但是当我尝试启动时,我得到:在清单中引用的 Class 在项目或库中找不到,相关的to dex ...我追查并修复这些错误,但无处可去。 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...在这一点上,我的项目搞砸了,我考虑删除它并使用备份......相反,我使用历史记录到 go 回到过去......

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.我还尝试创建一个新的 package 并将所有类和文件移动到新创建的 package 并删除旧的,但它有自己的蠕虫罐,看起来比上面的更混乱。

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 https://developer.android.com/studio/build/application-id.html

I used the following instruction to rename the application ID:我使用以下指令重命名应用程序 ID:

Changing the application ID (which is now independent of the package name) can be done very easily in one step.只需一步即可轻松更改应用程序 ID(现在独立于 package 名称)。 You don't have to touch AndroidManifest.您不必触摸 AndroidManifest。 Instead, do the following: right-click on the root folder of your project.相反,请执行以下操作:右键单击项目的根文件夹。 Click "Open Module Setting".点击“打开模块设置”。 Go to the Flavours tab. Go 到风味选项卡。 Change the applicationID to whatever package name you want.将 applicationID 更改为您想要的任何 package 名称。 Press OK.按确定。

As found here:如发现here:

Rename package in Android Studio 在 Android Studio 中重命名 package

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?所以我想知道,(新问题,仅相关问题)如果我重命名应用程序 ID,就像我在这里所做的那样,并且它仅在 gradle 构建文件中更改,这是否适用于我的应用程序 package 名称以提交到应用程序商店作为共享应用程序? Or do I need to actually refactor the package completely?或者我是否需要完全重构 package?

First, you have to create your desired package name under the java package.首先,您必须在 java package 下创建所需的 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. 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.您可以通过选择所有文件并将它们剪切并粘贴到新文件以及 select 并拖放到新的 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其次,您将 go 到 build.gradle (app) 然后 select 应用程序 id

在此处输入图像描述

After selection press Ctrl + Shift + r then this screen will be appear选择后按Ctrl + Shift + r然后会出现这个屏幕

在此处输入图像描述

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添加新的 package 名称后,您必须单击全部替换按钮,将所有旧的 package 名称替换为新名称,然后同步 build.gradle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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