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

是否包含自动重构?

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


当前回答

IntelliJ IDEA有一个名为“压缩空中间包”的选项。选择“项目”选项卡的选项图标,然后取消/激活该选项。

请参阅:如何更改IntelliJ IDEA中的顶级包名称?

其他回答

第一部分包括在java文件夹下创建一个新包,然后选择并将所有源文件从旧包拖到这个新包。之后,您需要将android manifest中的包名称重命名为新包的名称。在步骤2中,这是您需要做的。除了更改清单中的包名之外,您还需要更改android studio中模块build.gradle下applicationId中的旧包名。总之,单击“AndroidManifest.xml”下面的build.gradle并将applicationId的值修改为新的包名。然后,在顶部,建造不足。清理项目,然后重建。从这里开始应该很好。

一个人可能犯的常见错误是无法重命名包结构,即当试图在com.name.android级别进行修改时,无法将com.name.aandroid更改为com.Renamed.android。

为了获得相同的所需更改,请升级一个级别,即com.name,在重构时将其更改为Renamed。这将永远有效。

右键单击包名称->Refactor->重命名->输入包名称->单击“Refactor”。不要编辑“app”文件夹下除“build.gradle”以外的任何构建文件。

从重命名android工作室项目-Vishnu Sivan

接受的答案对我不起作用。这里有一个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要点

对我来说,我只是更改了库模块的目录名而不是包名,并单击了底部的Do refactor(示例:从com.reg.register->com.reg1.register)。

之后,确保更新了buildgradle文件和manifest文件中的新目录名。

重建项目。