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

是否包含自动重构?

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


当前回答

这对我来说非常有效,即使是更改所有相关模块中的引用,也可以更改我想在包中修改的文件夹名称,但对我来说,只有完全按照以下步骤操作,这才有效:

A-如果您想更改根文件夹的名称(换句话说,通常在包中用于包含主要活动的文件夹的名称),可以在以下步骤(B到C)之前或之后通过退出“Android Studio”手动重命名此根文件夹。单击“导入项目…”,然后单击项目的新名称(此导入过程将自动使用项目文件重新调整“Gradle”文件)B-在接下来的C步骤之前,不要忘记手动修改要在包中更改的文件夹名称,方法是在以下3个文件中更改它们:“AndroidManifest.xml”、“MainActivity.java”(或您最终为第一个活动选择的名称)和“build.gradle(Module:app)”文件。C-按照上述步骤1至6(“在项目窗格中,单击小齿轮图标…”)->https://stackoverflow.com/a/29092698/2001610 D-仅当您现在想要更改根文件夹的名称时,您可以按照上面所述的“A”过程进行操作。

其他回答

要在Android studio中重命名包名,请单击项目部分中的设置图标并取消选中压缩空中间包,然后包将拆分为多个文件夹名,然后右键单击需要更改名称的文件夹,单击refactor->rename->Type the name you want To change in->refactor-->refactor Directory,然后在整个项目中导入R.java文件。为我工作。

我需要运行文件->使缓存无效/重新启动。。。清除原始包装的残留物

包装有两个目的。一个是在Google Play商店中唯一识别您的应用程序。另一个是为构建项目时生成的R.java类命名包。您可以将第一个目的看作外部包,第二个目的看作内部包。假设您想更改外部包,以便在Play商店中进行识别,有一种方便的方法可以做到这一点。

在Android Studio中,

 choose File -> Project Structure -> Choose your app's module -> Click on the 
 Flavors tab -> change the Application id.

现在,当您构建项目时,您的APK和清单将使用此新的包名称。

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

最好的方法是编写新的包名,然后从旧的包名中拖动。

第二种方法是,如果单击Refactor then move选项,然后重命名包名,它将重命名包名并重新生成。

在Build.gradle中,您必须手动执行,如果重构,则不会在Build.ggradle中重命名。