Java 如何更改 Android 应用程序的包名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4025369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change package name of an Android Application
提问by Allen Gingrich
My keystore is corrupt, therefore the Android Market is requiring me to rename the app and resubmit it. However, whenever I go to edit the package names in Manifest and throughout the files, it gives me tons of errors.
我的密钥库已损坏,因此 Android Market 要求我重命名应用程序并重新提交。但是,每当我在 Manifest 和整个文件中编辑包名称时,都会出现大量错误。
What's the proper way to change the application name?
更改应用程序名称的正确方法是什么?
采纳答案by eldarerathis
If you're using Eclipse, you could try these instructionsfrom Android's developer site. They're specifically for the GestureBuilder sample but should apply to any application as far as I can tell:
如果您使用的是 Eclipse,则可以尝试从 Android 的开发者站点获取这些说明。它们专门用于 GestureBuilder 示例,但据我所知应该适用于任何应用程序:
[H]ere's how you could do this in Eclipse:
- Right-click on the package name (
src/com.android.gesture.builder
).- Select Refactor > Rename and change the name, for example to
com.android.gestureNEW.builder
.- Open the manifest file. Inside the
<manifest>
tag, change the package name tocom.android.gestureNEW.builder
.- Open each of the two Activity files and do Ctrl-Shift-O to add missing import packages, then save each file. Run the GestureBuilder application on the emulator.
[H] 下面是如何在 Eclipse 中执行此操作:
- 右键单击包名称 (
src/com.android.gesture.builder
)。- 选择 Refactor > Rename 并更改名称,例如更改为
com.android.gestureNEW.builder
。- 打开清单文件。在
<manifest>
标签内,将包名称更改为com.android.gestureNEW.builder
.- 打开两个 Activity 文件中的每一个,然后按 Ctrl-Shift-O 添加缺少的导入包,然后保存每个文件。在模拟器上运行 GestureBuilder 应用程序。
Also, be sure you remembered to rename the package itself along with the references to it in the files. In Eclipse you can see the name of the package in the file explorer window/tree view on the left hand side. In my experience, Eclipse can sometimes be a little finnicky and unreliable with this (leaving behind stray references to the old package name, for example).
此外,请确保您记得重命名包本身以及文件中对它的引用。在 Eclipse 中,您可以在左侧的文件资源管理器窗口/树视图中看到包的名称。根据我的经验,Eclipse 有时可能会有点挑剔和不可靠(例如,留下对旧包名称的杂散引用)。
回答by Marc Bernstein
There is a way to change the package name easily in Eclipse. Right click on your project, scroll down to Android Tools, and then click on Rename Application Package.
有一种方法可以在 Eclipse 中轻松更改包名称。右键单击您的项目,向下滚动到 Android 工具,然后单击重命名应用程序包。
回答by m0j0
Bernstein has the method, use the Eclipse tool, "Rename Application Package", you may have to do some clean-up even after the fact. Also, Eclipse sometimes loses track of things when you make changes to a project. You may have to use the "Clean Project" tool (under the "Project" menu.) If that doesn't work, you may have to close and restart Eclipse. Voo-doo solutions, but Eclipse can be that way.
Bernstein 有方法,使用 Eclipse 工具“重命名应用程序包”,即使事后您也可能需要做一些清理工作。此外,当您对项目进行更改时,Eclipse 有时会丢失跟踪内容。您可能必须使用“Clean Project”工具(在“Project”菜单下)。如果这不起作用,您可能必须关闭并重新启动 Eclipse。Voo-doo 解决方案,但 Eclipse 可以这样。
回答by robisaks
回答by AZ_
Here's how you could do this in Eclipse:
以下是在 Eclipse 中执行此操作的方法:
- Right-click on the package name (
src/com.android.gesture.builder
). - Select
Refactor > Rename
and change the name, for example tocom.android.gestureNEW.builder
. - Open the manifest file. Inside the
<manifest>
tag, change the package name tocom.android.gestureNEW.builder
. - Open each of the two
Activity
files and do Ctrl+Shift+Oto add missing import packages, then save each file. - Run the
GestureBuilder
application on the emulator.
- 右键单击包名称 (
src/com.android.gesture.builder
)。 - 选择
Refactor > Rename
并更改名称,例如更改为com.android.gestureNEW.builder
。 - 打开清单文件。在
<manifest>
标签内,将包名称更改为com.android.gestureNEW.builder
. - 打开每个两个
Activity
文件,并做Ctrl+ Shift+O添加缺少的导入包,然后保存每个文件。 GestureBuilder
在模拟器上运行应用程序。
Updatesuper easy way
right click on your project...
更新超级简单的方法,右键单击您的项目...
回答by erdomester
Changing package name is a pain in the ass. It looks like different methods work for different people. My solution is quick and error free. Looks like no cleaning or resetting eclipse is needed.
更改包名是一件很麻烦的事情。看起来不同的方法适用于不同的人。我的解决方案快速且无错误。看起来不需要清洁或重置日食。
- Right click on the package name then Refractor > Rename.
- Right click on the project name Android tools > Rename Application Package.
- Manually set the package names in the Manifest that have not been changed by the previous steps.
- 右键单击包名称,然后 Refractor > Rename。
- 右键单击项目名称 Android 工具 > 重命名应用程序包。
- 在Manifest中手动设置前面步骤没有改变的包名。
回答by LikeYou
There is also another solution for users without Eclipse, simply change the package attribute in <manifest> tag in AndroidManifest.xml, by replacing the the old package name used with a new one. Note:You have to adjust the all the related import statements and related folders manually in your project than, too.
对于没有 Eclipse 的用户,还有另一种解决方案,只需更改 AndroidManifest.xml 中 <manifest> 标记中的包属性,将使用的旧包名称替换为新包名称。注意:您还必须手动调整项目中所有相关的导入语句和相关文件夹。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="new.package.name"
.....
</manifest>
回答by Sathirar
- Fist change the package name in the manifest file
- Re-factor > Rename the name of the package in
src
folder and put a tick forrename subpackages
- That is all you are done.
- 拳头更改清单文件中的包名称
- 重构 > 重命名
src
文件夹中包的名称并勾选rename subpackages
- 这就是你所做的一切。
回答by clusterflux
Without Eclipse:
没有日食:
Change package name and Activity names in AndroidManifext.xml and anywhere else it shows up in .xml files (custom view names, etc)
Update package import statement across all source files
Rename all folders to match the package name. Folder locations:
a. bin/classes
b. gen
c. srcUpdate the project name in build.xml (or your apk's name won't change)
Delete all the .class files in bin/classes/com/example/myapp/ (if you skip this step the files don't get rewritten during build and dex give a bunch of trouble processing "class name does not match path" errors
Delete gen/com/example/myapp/BuildConfig.java (I don't know why deleting BuildConfig.class in step 3a didn't cause dex to update it's path to this, but until I deleted BuildConfig.java it kept recreating gen/com/oldapp_example/oldapp and putting BuildConfig.class in there. I don't know why R.java from the same location doesn't have this problem. I suspect BuildConfig.java is auto-generated in pre-compile but R.java is not)
更改 AndroidManifext.xml 中的包名称和活动名称以及它在 .xml 文件中显示的任何其他位置(自定义视图名称等)
跨所有源文件更新包导入语句
重命名所有文件夹以匹配包名称。文件夹位置:
一个。垃圾箱/类
b. 将军
c。源文件更新 build.xml 中的项目名称(否则你的 apk 名称不会改变)
删除 bin/classes/com/example/myapp/ 中的所有 .class 文件(如果你跳过这一步,文件在构建和 dex 过程中不会被重写,处理“类名与路径不匹配”错误时会出现一堆麻烦
删除 gen/com/example/myapp/BuildConfig.java (我不知道为什么在步骤 3a 中删除 BuildConfig.class 不会导致 dex 更新它的路径,但是直到我删除 BuildConfig.java 它一直重新创建 gen/ com/oldapp_example/oldapp 并将 BuildConfig.class 放在那里。我不知道为什么来自同一位置的 R.java 没有这个问题。我怀疑 BuildConfig.java 是在预编译中自动生成的,但 R.java不是)
The above 6 steps are what I did to get my package name changed and get a successful* build. There may be a better way to handle steps 5 and 6 via dex commands to update paths, or perhaps by editing classes.dex.d in the root directory of the project. I'm not familiar with dex or if it's ok to delete/edit classes.dex.d so I went with the method of deleting .class files that I know will be regenerated in the build. I then checked classes.dex.d to see what still needed to be updated.
以上 6 个步骤是我为更改包名称并获得成功* 构建所做的工作。可能有更好的方法来处理第 5 步和第 6 步,通过 dex 命令更新路径,或者通过编辑项目根目录中的 classes.dex.d。我不熟悉 dex 或者是否可以删除/编辑 classes.dex.d,所以我采用了删除我知道将在构建中重新生成的 .class 文件的方法。然后我检查 classes.dex.d 以查看仍然需要更新的内容。
*No errors or warnings left in my build EXCEPT dex and apkbuilder both state "Found Deleted Target File" with no specifics about what that file is. Not sure if this shows up in every build, if it was there before I messed with my package name, or if it's a result of my deletions and I'm missing a step.
*我的构建中没有错误或警告,除了 dex 和 apkbuilder 都声明“找到已删除的目标文件”,但没有具体说明该文件是什么。不确定这是否出现在每个构建中,是否在我弄乱我的包名称之前就在那里,或者它是否是我删除的结果并且我错过了一个步骤。
回答by MapleLover
Alternative:
选择:
- Open AndroidManifest.xml
- Change package="NEW NAME" within manifest tag.
- 打开 AndroidManifest.xml
- 在清单标签中更改 package="NEW NAME"。