如何在基于IntelliJ IDEA的新IDE Android Studio中重命名软件包?

是否包含自动重构?

我想进行大规模重构,但我不知道怎么做。我在Eclipse工作了两年,在Eclipse中它是一个一键操作。


当前回答

更新答案:2015年5月

好吧,我一直在努力在Android Studio中克隆和重命名项目,但最终我实现了这一目标。以下是需要遵循的步骤:

复制项目文件夹,重命名并用Android Studio打开从资源管理器重命名模块目录重命名projectName.iml和内容重命名idea/.name内容在项目窗格中,单击小齿轮图标->取消选中“压缩空中间包”为新的包名重构src目录(重命名包,“不重命名目录”)在build.gradle中重命名应用程序idsettings.gradle重命名模块

就是这样。。。

其他回答

还有一个很好的键盘快捷键,只需单击n选择要重命名的类/包,然后按ALT+两次R或Shift+F6

它反映了Android Studio中的:->Refactor菜单和Rename功能。重命名现有包后,它会在底部显示此反射/重命名的效果,如果您单击Refector按钮,它将更改整个项目中使用的类名/包名。

选择要重构的包。重构(Refactor)→ 移动→ “将xxx移动到新包”。

接受的答案对我不起作用。这里有一个python脚本,可以直接在源文件上更改包名。

"""
Notes:
1 - This script it for python3
2 - Create a backup first
3 - Place on the root folder of the android project you want to change
4 - run `python change_android_studio_pkg_name.py`
5 - Type the old package name
6 - Type the new package name
7 - Clean and Rebuild on Android Studio
8 - SRC folder names won't be changed but app will work
"""

import os, glob, traceback
from pathlib import Path

def rwf(fp, mode="r", data="" ):
    if mode == "r":
        with open(fp, "r", encoding='utf8') as f:
            return(f.read())
    elif mode == "w":
        with open(fp, "w", encoding='utf8') as f:
            f.write(data)

old_pkg = input("Old PKG\n").strip()
new_pkg = input("New PKG\n").strip()

if not old_pkg or not new_pkg:
    exit("Args Missing")

dirname, filename = os.path.split(os.path.abspath(__file__))
file_types = ['xml', 'txt', 'json', 'gradle', 'java']
_fs =  glob.iglob(f"{dirname}/**", recursive=True)
for file_path in _fs:
    ext = Path(file_path).suffix.replace(".", "").lower()
    if os.path.isfile(file_path) and ext in file_types:
        try:
            content = rwf(file_path)
            new_content = content.replace(old_pkg, new_pkg)
            rwf(file_path, "w", new_content)
        except:
            print(traceback.format_exc())
            pass

蟒蛇3要点

首先,您需要转到左侧显示的设置图标。单击该下拉列表并取消选中压缩空中间包

转到AndroidManifest.xml

选择所有包,然后右键单击重构,然后根据需要重命名。然后转到build.gradle,然后使用Android Manifest中的packagename更改Applicationid。

这对我来说很有效

一定要去Sheharyar的伟大答案。随之而来的答案和评论太多,很容易让人感到困惑和放弃,但不要。这个答案有效。

简而言之,你要做三件事:

(1) 取消选择压缩空中间包装。

(2) 然后,通过选择“更改包”(而不是“更改目录”)来重命名每个旧目录节点,以匹配新的包名称。(请务必预览更改。)

(3) 编辑build.gradle文件,使APPLICATION_ID与新的包名匹配。