如何改变Android应用程序的软件包名称

135 人关注

我的密钥库损坏了,因此安卓市场要求我重命名该应用程序并重新提交。然而,每当我去编辑Manifest中的包名和整个文件时,它都会给我带来大量的错误。

改变应用程序名称的正确方法是什么?

5 个评论
感谢他们从2010年起增加了一个 "神奇的,改变软件包名称的按钮"--见@Marc Bernstein的回答--。 stackoverflow.com/a/4025633/383414
感谢理查德-勒...
当这个问题在3年前被问及时,很难成为一个重复的问题......
java
android
Allen Gingrich
Allen Gingrich
发布于 2010-10-26
18 个回答
Marc Bernstein
Marc Bernstein
发布于 2018-06-23
已采纳
0 人赞同

有一种方法可以在Eclipse中轻松地改变包的名称。在你的项目上点击右键,向下滚动到Android工具,然后点击重命名应用程序包。

刚试过Eldar的,很有效--但我相信这也会有效。
Adam
这是正确的答案,但目前的ADT是坏的,它破坏了对你的R类的每一个引用。所以你必须点击每个文件,做 "ctrl-shift-o"(或Mac上的cmd-shift-o),并选择你的新包命名为R(不要选择android.R!事情会破坏得很糟糕),然后点击保存。......对每一个文件都要重复 :(
@Adam 为了让我们的生活更轻松,点击项目并使用ctrl+shift+o,它将更新所有类/文件中的导入引用 :D
这刚刚救了我的命,因为我失去了我那可笑的存储钥匙,不得不重新发布我的申请。
我发现每个文件顶部声明的包名与新的包名不一致,也与它们所在的文件夹不一致。你可以右击仍有旧包名的源文件夹(至少我的总是这样),选择 "重构",然后 "重命名",将其重命名为新的包名。
eldarerathis
eldarerathis
发布于 2018-06-23
0 人赞同

If you're using Eclipse, you could try these instructions 从安卓的开发者网站上下载。它们是专门针对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 to com.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.
  • 另外,要确保你记得把包本身和文件中对它的引用一起重命名。在Eclipse中,你可以在左手边的文件资源管理器窗口/树状视图中看到包的名称。根据我的经验,Eclipse有时会有点小题大做,不可靠(例如,留下对旧包名的游离引用)。

    这可能是最好的方法,没有神奇的,改变包名的按钮。
    是的,有。 见@Marc Bernstein的回答。
    第一步不是 "右击",应该是在菜单中选择 "重构"。
    And you might need to change app_name in res->values=>strings.xml
    你仍然需要在你的XML文件中手动编辑所需的软件包名称,即manifest.xml。
    AZ_
    AZ_
    发布于 2018-06-23
    0 人赞同

    Here'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 to com.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.
  • Link to post

    超级简单的方法 在你的项目上点击右键...
    craned
    craned
    发布于 2018-06-23
    0 人赞同

    In 安卓工作室 ,老实说,你应该使用它,通过 right-clicking project structure -> Refactor -> Rename... 中的包名上更改包名。

    然后它给出了重命名目录或软件包的选项。选择软件包。目录也应该跟着重命名。键入你的新包名,然后点击Refactor。它将改变所有的导入,并为你删除多余的导入。你甚至可以让它在注释和字符串等方面为你修正。

    最后 在你的AndroidManifest.xml中把包的名字相应地改到顶部。否则你会得到到处都是抱怨R.whatever的错误。

    另一个非常有用的解决方案

    First create a new package with the desired name by right clicking on the java folder -> new -> package.`

    然后,选择并拖动你所有的类到新的包中。 AndroidStudio将在各处重新构建包的名称。

    之后:在你的应用程序的 build.gradle 中添加/编辑新的applicationId,即(在我的例子中是 com.a.bc )。

    defaultConfig {
        applicationId "com.a.bc"
        minSdkVersion 13
        targetSdkVersion 19
    

    原帖和更多评论here

    奇怪的是,当我这样做时,它没有给出选择的目录/包...只有 "模块名称"...
    craned
    如果我右击根目录,它现在对我也是这样做的。至少对我来说,这不是我的实际软件包名称。如果你的软件包名称是com.a.bc,根目录下面应该有一个叫做bc的目录。这就是你会得到不同选项的地方。现在,我得到的是目录或模块。试试模块。 如果它破坏了什么,你可以随时撤销它(Ctrl+z应该能做到)。
    谢谢......该死,你是指".idea "目录还是 "app "目录? 我刚刚发布了一个解释性的Q ... stackoverflow.com/questions/23786361/... 啊......我现在看到 "选择重构目录/模块",当我点击我的 "应用程序 "文件夹时.... hoy caramba!
    @JoeBlow right click on package under the folder java
    "Android Studio,说实话,你应该使用它。"请记住,这个问题是在2010年提出的,而AS甚至在2013年才宣布,更不用说作为稳定版本发布了(2014年)。诚然,用户 现今 应该使用AS。 :-)
    clusterflux
    clusterflux
    发布于 2018-06-23
    0 人赞同

    Without Eclipse:

  • 在AndroidManifext.xml和其他任何显示在.xml文件中的地方(自定义视图名称等)改变包名和活动名称。

  • 在所有源文件中更新包的导入语句

  • 重新命名所有的文件夹,使之与软件包的名称一致。文件夹位置。
    A. bin/classes
    b. gen
    c. src

  • 更新build.xml中的项目名称(否则你的apk名称不会改变)。

  • 删除bin/classes/com/example/myapp/中的所有.class文件(如果你跳过这一步,文件在构建过程中不会被重写,而且dex在处理 "class name does not match path "的错误时,会有很多麻烦。

  • 删除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却不是)

  • 以上6个步骤是我所做的,让我的包名得到改变,并得到成功的*构建。可能有更好的方法来处理步骤5和6,通过dex命令来更新路径,或者通过编辑项目根目录下的classes.dex.d。我对dex不熟悉,也不知道是否可以删除/编辑classes.dex.d,所以我采用了删除我知道会在构建中重新生成的.class文件的方法。然后我检查了classes.dex.d,看看还有哪些需要更新。

    *除了dex和apkbuilder都显示 "Found Deleted Target File",但没有具体说明是什么文件。不知道这是否在每次构建中都会出现,是否在我乱用包名之前就已经存在了,或者这是我删除文件的结果,我错过了一个步骤。

    I solved the rename as you did, but with a simplification: delete bin and gen folders. Also I couldn't import the renamed project in Eclipse until I modified the hidden file .project, it had the old project name under <name>
    Deepak Sharma
    Deepak Sharma
    发布于 2018-06-23
    0 人赞同

    Follow these steps:-

  • Right click on the project.
  • Go to Android tools.
  • Click on Rename Application Package.
  • Change the application package name.
  • 这很有效,但你也应该重构基本的src文件夹包名。
    Movsar Bekaev
    Movsar Bekaev
    发布于 2018-06-23
    0 人赞同

    在Android Studio 1.3中,最快的方法是这样做的 :

  • Change the package name in the manifest
  • Go to Module Settings[F4]-> Flavors , into Application Id write the same name.
  • Create new package with that name: [right-click-> new-> package]
  • Select all java files of your project and then proceed [Right-click-> Refactor-> Move-> {Select package}-> Refactor]
  • P.S.如果你不按照这个顺序,你可能最终会用新的导入来改变所有的java文件,并出现一堆编译时的错误,所以这个顺序非常重要。

    使用这种方法,R在重构中保持不变。因此,import com.app.myapp.R不改变为新的包名,你必须逐个...
    @OWADVL,当你把文件移到新的包里时,R会自动改变,当你移动.java文件时,Android Studio会自动执行重建,我每天都在做这件事,只要我保持这个顺序,我从不手动改变.java文件的任何内容
    Tom
    But in Eclipse, we could change the app package ID, without changing the package of all the code. The wizard would rename BuildConfig & R references for you. I'm puzzled that the same thing doesn't exist in AS.
    erdomester
    erdomester
    发布于 2018-06-23
    0 人赞同

    改变包的名称是一件很麻烦的事。看起来不同的方法对不同的人有效。我的解决方案是快速和无错误的。看起来不需要清理或重设eclipse。

  • 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.
  • m0j0
    m0j0
    发布于 2018-06-23
    0 人赞同

    Bernstein有方法,使用Eclipse工具,"重命名应用程序包",你可能要做一些清理工作,甚至在事后。 另外,当你对一个项目进行修改时,Eclipse有时会失去跟踪。你可能不得不使用 "清理项目 "工具(在 "项目 "菜单下)。 如果这不起作用,你可能不得不关闭并重新启动Eclipse。Voo-doo解决方案,但Eclipse可以是这样的。

    robisaks
    robisaks
    发布于 2018-06-23
    0 人赞同

    我发现最简单的解决方案是使用 Regexxer 将 "com.package.name "替换为 "com.newpackage.name",然后正确地重命名这些目录。超级简单,超级快速。

    Sathirar
    Sathirar
    发布于 2018-06-23
    0 人赞同
  • Fist change the package name in the manifest file
  • Re-factor > Rename the name of the package in src folder and put a tick for rename subpackages
  • That is all you are done.
  • I tried using Marc Bernstein's Android Tools->Rename Application Package, but it gave me error. This was simpler, though I had to fix some things after the change
    LikeYou
    LikeYou
    发布于 2018-06-23
    0 人赞同

    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: 你也必须在你的项目中手动调整所有相关的导入语句和相关文件夹。

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="new.package.name"
        .....
    </manifest>
        
    MapleLover
    MapleLover
    发布于 2018-06-23
    0 人赞同

    替代方案。

  • Open AndroidManifest.xml
  • Change package="NEW NAME" within manifest tag.
  • ritesh4326
    ritesh4326
    发布于 2018-06-23
    0 人赞同
  • Right Click on Project >Android Tools >Rename Application Package

  • Go to src and right click on your main package >Refactor >Rename

  • 进入清单文件,改变你的软件包名称。

  • Dudeist
    Dudeist
    发布于 2018-06-23
    0 人赞同

    我刚刚花了几个小时尝试所有的解决方案。我很确定,问题出在我的代码上--apk开始了,但在处理不同的类和活动时出现了一些错误。

    只有这样才能在我的项目中工作。

  • rename it - file/rename and check all checkboxes
  • check for non renamed places, I had in one of layout old name still in tools:context="..
  • create new workspace
  • import project into new workspace
  • For me it works, only solution.

    Raul Pinto
    Raul Pinto
    发布于 2018-06-23
    0 人赞同

    So, using IntelliJ with Android (Studio) plugin was: I went to AndroidManifest.xml on top and changed package name by right-clicking -> Refactor -> rename. Our then package name had four parts a.b.c.d , the new one only a.b.c . So I renamed the first three. Then I went to the directory of generated sources ( app\build\generated\source\r\development\debug\a\b\c\d ), right-clicked R class and Refactor->Move[d] it to one package higher. Same with BuildConfig in app\build\generated\source\buildConfig\development\debug\a\b\c\d .

    By using Refactor->Move, IntelliJ updates all references to BuildConfig and R .

    最后我更新了我在gradle.build中发现的所有applicationId[s]。我点击了Clean Project, Make and Rebuild Project。从我的手机中删除了应用程序,并点击了 "播放"。这一切都成功了,所以重构很容易而且相当快。

    Krishnadas PC
    Krishnadas PC
    发布于 2018-06-23
    0 人赞同

    在Android Studio 1.2.2中 假设你想把 com.example.myapp 改为 org.mydomain.mynewapp 。 你可以按照下面屏幕截图中显示的每个目录的步骤进行操作。它将给你一个预览选项,这样你就可以在做任何改变之前进行预览。

    进入 Refactor -> Rename ,给你的新名字。你必须为每个文件夹都这样做,比如为com、example和myapp。它将自动更新进口,这样你就不必担心了。

    同时更新没有自动更新的 build.gradle 文件。

    defaultConfig {
            applicationId "org.mydomain.mynewapp" //update the new package name here
            minSdkVersion 15
            targetSdkVersion 22
            versionCode 1
            versionName "1.0"
    

    之后,它将要求sync做。然后一切都会好起来。在这之后我就能把我的APK上传到谷歌商店了。请确保你已经签署了你的应用程序,以便将其上传到商店。

    Hanisha
    Hanisha
    发布于 2018-06-23
    0 人赞同
  • In your Android studio , at project panel , there is gear icon "Click on it".
  • Uncheck the Compact Empty Middle Packages
  • Now the package directory has been broken up i.e individual directories.
  • Select the directory you want to rename.
  • Right click on that particular directory.
  • Select refactor and then rename option.
  • The dialog box has been open up,rename it.
  • After putting new name, click on Refactor.
  • Then click on Do Refactor at bottom.
  • Then android studio will update all changes.
  •