简体   繁体   English

如何更改Android应用程序的包名称

[英]How to change package name of an Android Application

My keystore is corrupt, therefore the Android Market is requiring me to rename the app and resubmit it. 我的密钥库已损坏,因此Android Market要求我重命名该应用并重新提交。 However, whenever I go to edit the package names in Manifest and throughout the files, it gives me tons of errors. 但是,每当我在Manifest和整个文件中编辑包名时,它都会给我带来大量的错误。

What's the proper way to change the application name? 更改应用程序名称的正确方法是什么?

There is a way to change the package name easily in Eclipse. 有一种方法可以在Eclipse中轻松更改包名称。 Right click on your project, scroll down to Android Tools, and then click on Rename Application Package. 右键单击您的项目,向下滚动到Android Tools,然后单击重命名应用程序包。

If you're using Eclipse, you could try these instructions from Android's developer site. 如果您正在使用Eclipse,则可以尝试从Android开发人员站点获取这些说明 They're specifically for the GestureBuilder sample but should apply to any application as far as I can tell: 它们专门用于GestureBuilder示例,但应该适用于任何应用程序,据我所知:

[H]ere's how you could do this in Eclipse: [H]你是如何在Eclipse中做到这一点的:

  1. Right-click on the package name ( src/com.android.gesture.builder ). 右键单击包名称( src/com.android.gesture.builder )。
  2. Select Refactor > Rename and change the name, for example to com.android.gestureNEW.builder . 选择Refactor> Rename并更改名称,例如更改为com.android.gestureNEW.builder
  3. Open the manifest file. 打开清单文件。 Inside the <manifest> tag, change the package name to com.android.gestureNEW.builder . <manifest>标记内,将包名称更改为com.android.gestureNEW.builder
  4. Open each of the two Activity files and do Ctrl-Shift-O to add missing import packages, then save each file. 打开两个Activity文件中的每一个,然后按Ctrl-Shift-O添加缺少的导入包,然后保存每个文件。 Run the GestureBuilder application on the emulator. 在模拟器上运行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. 在Eclipse中,您可以在左侧的文件资源管理器窗口/树视图中查看包的名称。 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有时可能有点笨拙且不可靠(例如,留下对旧包名称的杂散引用)。

Here's how you could do this in Eclipse: 以下是在Eclipse中执行此操作的方法:

  1. Right-click on the package name ( src/com.android.gesture.builder ). 右键单击包名称( src/com.android.gesture.builder )。
  2. Select Refactor > Rename and change the name, for example to com.android.gestureNEW.builder . 选择Refactor > Rename并更改名称,例如更改为com.android.gestureNEW.builder
  3. Open the manifest file. 打开清单文件。 Inside the <manifest> tag, change the package name to com.android.gestureNEW.builder . <manifest>标记内,将包名称更改为com.android.gestureNEW.builder
  4. Open each of the two Activity files and do Ctrl + Shift + O to add missing import packages, then save each file. 打开两个Activity文件中的每一个,然后按Ctrl + Shift + O添加缺少的导入包,然后保存每个文件。
  5. Run the GestureBuilder application on the emulator. 在模拟器上运行GestureBuilder应用程序。

Link to post 链接到帖子

Update super easy way right click on your project... 更新超级简单的方法右键单击您的项目... 在此输入图像描述

In Android Studio , which, quite honestly, you should be using, change the package name by right-clicking on the package name in the project structure -> Refactor -> Rename... Android Studio中 ,说实话,您应该使用,通过right-clicking project structure -> Refactor -> Rename...的包名称来更改包名称project structure -> Refactor -> Rename...

It then gives the option of renaming the directory or the package. 然后,它提供重命名目录或包的选项。 Select the package. 选择包。 The directory should follow suit. 该目录应该效仿。 Type in your new package name, and click Refactor. 键入新包名称,然后单击“重构”。 It will change all the imports and remove redundant imports for you. 它将更改所有导入并删除多余的导入。 You can even have it fix it for you in comments and strings, etc. 您甚至可以在评论和字符串等中为它修复它。

Lastly , change the package name accordingly in your AndroidManifest.xml towards the top. 最后 ,将AndroidManifest.xml中的包名称更改为顶部。 Otherwise you will get errors everywhere complaining about R.whatever. 否则你会到处抱怨R.whatever。

Another very useful solution 另一个非常有用的解

First create a new package with the desired name by right clicking on the java folder -> new -> package.` 首先by right clicking on the java文件夹 - > new - > package by right clicking on the创建一个具有所需名称by right clicking on the新包

Then, select and drag all your classes to the new package. 然后,选择并将所有类拖到新包中。 AndroidStudio will re-factor the package name everywhere. AndroidStudio会在任何地方重新计算软件包名称。

After that: in your app's build.gradle add/edit applicationId with the new one. 之后:在你的应用程序的build.gradle添加/编辑带有新的applicationId。 ie ( com.a.bc in my case): 即(在我的情况下com.a.bc ):

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

Original post and more comments here 原帖和更多评论在这里

Without Eclipse: 没有Eclipse:

  1. Change package name and Activity names in AndroidManifext.xml and anywhere else it shows up in .xml files (custom view names, etc) 更改AndroidManifext.xml中的包名称和活动名称以及它在.xml文件中显示的任何其他位置(自定义视图名称等)

  2. Update package import statement across all source files 跨所有源文件更新包导入语句

  3. Rename all folders to match the package name. 重命名所有文件夹以匹配包名称。 Folder locations: 文件夹位置:
    a. 一个。 bin/classes 斌/班
    b. gen
    c. C。 src SRC

  4. Update the project name in build.xml (or your apk's name won't change) 在build.xml中更新项目名称(或者您的apk名称不会更改)

  5. 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 删除bin / classes / com / example / myapp /中的所有.class文件(如果跳过此步骤,则在构建期间不会重写文件,并且dex会给处理“类名不匹配路径”错误带来一堆麻烦

  6. 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) 删除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. 我做了以上6个步骤,以便更改我的包名并获得成功的*版本。 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. 可能有更好的方法通过dex命令来处理步骤5和6以更新路径,或者可能通过编辑项目根目录中的classes.dex.d。 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. 我不熟悉dex或者删除/编辑classes.dex.d是可以的,所以我选择了删除.class文件的方法,我知道这些文件将在构建中重新生成。 I then checked classes.dex.d to see what still needed to be updated. 然后我检查了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. *我的构建中没有错误或警告EXCEPT dex和apkbuilder都声明“找到删除的目标文件”,没有关于该文件的具体内容。 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. 不确定这是否出现在每个构建中,如果它在我搞砸了我的包名之前就已存在,或者是否是我删除的结果而我错过了一步。

Follow these steps:- 跟着这些步骤:-

  • Right click on the project. 右键单击该项目。
  • Go to Android tools. 转到Android工具。
  • Click on Rename Application Package. 单击重命名应用程序包。
  • Change the application package name. 更改应用程序包名称。

DONE DONE

The fastest way to do that in Android Studio 1.3 : 在Android Studio 1.3中执行此操作的最快方法

  1. Change the package name in the manifest 更改manifest的包名称
  2. Go to Module Settings[F4]-> Flavors , into Application Id write the same name. 转到Module Settings[F4]-> Flavors ,进入Application Id写入相同的名称。
  3. Create new package with that name: [right-click-> new-> package] 使用该名称创建新包: [right-click-> new-> package]
  4. Select all java files of your project and then proceed [Right-click-> Refactor-> Move-> {Select package}-> Refactor] 选择项目的所有java文件,然后继续[Right-click-> Refactor-> Move-> {Select package}-> Refactor]

PS If you will not follow this order you can end up changing all the java files one by one with new imports and a bunch of compile time errors, so the order is very important. PS如果您不遵循此顺序,您最终可能会使用新导入和一堆编译时错误逐个更改所有java文件,因此顺序非常重要。

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. 看起来不需要清理或重置日食。

  1. Right click on the package name then Refractor > Rename. 右键单击包名称,然后单击Refractor> Rename。
  2. Right click on the project name Android tools > Rename Application Package. 右键单击项目名称Android tools> Rename Application Package。
  3. Manually set the package names in the Manifest that have not been changed by the previous steps. 在Manifest中手动设置尚未通过前面步骤更改的包名称。
  1. Fist change the package name in the manifest file Fist更改清单文件中的包名称
  2. Re-factor > Rename the name of the package in src folder and put a tick for rename subpackages Re-factor>重命名src文件夹中包的名称,并勾选rename subpackages
  3. That is all you are done. 这就是你所做的一切。

Bernstein has the method, use the Eclipse tool, "Rename Application Package", you may have to do some clean-up even after the fact. Bernstein有方法,使用Eclipse工具“重命名应用程序包”,即使事后也可能需要做一些清理工作。 Also, Eclipse sometimes loses track of things when you make changes to a project. 此外,当您对项目进行更改时,Eclipse有时会丢失对事物的跟踪。 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. 您可能必须使用“清理项目”工具(在“项目”菜单下)。如果这不起作用,您可能必须关闭并重新启动Eclipse。 Voo-doo solutions, but Eclipse can be that way. Voo-doo解决方案,但Eclipse可以这样。

I found the easiest solution was to use Regexxer to replace "com.package.name" with "com.newpackage.name", then rename the directories properly. 我发现最简单的解决方案是使用Regexxer将“com.package.name”替换为“com.newpackage.name”,然后正确地重命名目录。 Super easy, super fast. 超级简单,超级快。

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. 对于没有Eclipse的用户,还有另一种解决方案,只需更改AndroidManifest.xml中<manifest>标签中的package属性,方法是将旧包名称替换为新包名称。 Note: You have to adjust the all the related import statements and related folders manually in your project than, too. 注意:您必须在项目中手动调整所有相关的导入语句和相关文件夹。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="new.package.name"
    .....
</manifest>

Alternative: 替代方案:

  1. Open AndroidManifest.xml 打开AndroidManifest.xml
  2. Change package="NEW NAME" within manifest tag. 在清单标签内更改package =“NEW NAME”。
  1. Right Click on Project >Android Tools >Rename Application Package 右键单击Project> Android Tools> Rename Application Package

  2. Go to src and right click on your main package >Refactor >Rename 转到src并右键单击主程序包> Refactor> Rename

  3. Go to manifest file and change your package name . 转到清单文件并更改您的包名称。

I just lost few hours trying all solutions. 我只是花了几个小时尝试所有解决方案。 I was sure, problem is in my code - apk starting but some errors when working with different classes and activities. 我确定,问题出现在我的代码中 - 在开始使用apk时,但在使用不同的类和活动时会出现一些错误。

Only way working with my project: 只有这样才能处理我的项目:

  1. rename it - file/rename and check all checkboxes 重命名 - 文件/重命名并选中所有复选框
  2. check for non renamed places, I had in one of layout old name still in tools:context=".. 检查非重命名的地方,我在其中一个布局旧名称仍在工具中:context =“..
  3. create new workspace 创建新工作区
  4. import project into new workspace 将项目导入新工作区

For me it works, only solution. 对我来说它有效,只有解决方案。

In Android Studio 1.2.2 Say if you want to change com.example.myapp to org.mydomain.mynewapp You can follow the steps in each of the directories as displayed in the below screen shot. 在Android Studio 1.2.2中说如果要将com.example.myapp更改为org.mydomain.mynewapp您可以按照以下屏幕截图中显示的每个目录中的步骤操作。 It will give you the preview option so that you can preview before making any changes. 它将为您提供预览选项,以便您可以在进行任何更改之前进行预览。

Go to Refactor -> Rename and give your new name. 转到Refactor -> Rename并提供新名称。 You have to do it for each of the folders, say for com, example and myapp. 您必须为每个文件夹执行此操作,例如com,example和myapp。 It will automatically update the imports so that you don't have to worry about it. 它会自动更新导入,因此您不必担心它。 在此输入图像描述

Also update the file build.gradle which is not updated automatically. 还要更新未自动更新的build.gradle文件。

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

After that it will ask for sync do it. 之后它会要求sync这样做。 Then everything will be fine. 那一切都会好的。 I'm able to upload my APK to google store after this. 我可以在此之后将我的APK上传到谷歌商店。 Make sure you have signed your app to upload it to the store. 确保您已将应用程序签名以将其上传到商店。

So, using IntelliJ with Android (Studio) plugin was: I went to AndroidManifest.xml on top and changed package name by right-clicking -> Refactor -> rename. 因此,使用带有Android(Studio)插件的IntelliJ是:我在顶部转到AndroidManifest.xml并通过右键单击 - >重构 - >重命名来更改包名称。 Our then package name had four parts abcd , the new one only abc . 我们当时的包名有四个部分abcd ,新的只有abc 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. 然后我进入生成源的目录( app\\build\\generated\\source\\r\\development\\debug\\a\\b\\c\\d ),右键单击R类并重构 - >将[d]移动到一个包中更高。 Same with BuildConfig in app\\build\\generated\\source\\buildConfig\\development\\debug\\a\\b\\c\\d . app\\build\\generated\\source\\buildConfig\\development\\debug\\a\\b\\c\\d BuildConfig相同。

By using Refactor->Move, IntelliJ updates all references to BuildConfig and R . 通过使用Refactor-> Move,IntelliJ更新对BuildConfigR所有引用。

Finally I updated all applicationId[s] I found in gradle.build I found. 最后我更新了我在gradle.build中找到的所有applicationId [s]。 I hit Clean Project, Make and Rebuild Project. 我点击了Clean Project,Make和Rebuild Project。 Deleted the app from my phone and hit "Play". 从手机中删除了该应用,点击“播放”。 It all worked out so refactoring was easy and quite fast. 这一切都解决了,所以重构很容易,也很快。

  1. In your Android studio , at project panel , there is gear icon "Click on it". 在Android工作室的项目面板上,有一个齿轮图标“点击它”。
  2. Uncheck the Compact Empty Middle Packages 取消选中Compact Empty Middle Packages
  3. Now the package directory has been broken up ie individual directories. 现在包目录已被拆分,即各个目录。
  4. Select the directory you want to rename. 选择要重命名的目录。
  5. Right click on that particular directory. 右键单击该特定目录。
  6. Select refactor and then rename option. 选择重构,然后重命名选项。
  7. The dialog box has been open up,rename it. 对话框已打开,重命名。
  8. After putting new name, click on Refactor. 输入新名称后,单击Refactor。
  9. Then click on Do Refactor at bottom. 然后单击底部的Do Refactor。
  10. Then android studio will update all changes. 然后android studio将更新所有更改。
  11. Now open build.gradle file and update the applicationId & sync it. 现在打开build.gradle文件并更新applicationId并同步它。
  12. Now go to manifest file and rename the package. 现在转到清单文件并重命名包。
  13. Rebuild your Project. 重建您的项目。
  14. You are done :) 你完成了:)

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

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