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

是否包含自动重构?

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


当前回答

转到AndroidManifest.xml在那里,您将找到作为manifest标记属性的包。只需选择要更改的部件,右键单击->重构->重命名

也许早些时候,这很难,但到今天为止,这已经不难了。

您可能还想在中更改应用程序id

defaultConfig {
    applicationId 
}

短而甜

其他回答

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

另一个好方法是:首先右键单击Java文件夹,创建一个具有所需名称的新包→ 新→ 包裹

然后,选择并将所有类拖到新包中。AndroidStudio将到处重构包名。

最后,删除旧包。

完成。

非常重要:

如果使用此方法,则必须手动将AndroidManifest.xml和build.gradle文件更改为新包。

在Android Studio中更改包名的3种方法

我想这是一个新的。我试过了,效果很好。如果上述方法不适用于您。试试这个。在我上面提到的网站上,还有两种其他的图像教程方法。我也检查了一下。

首先,我将讨论更改包名称。在本例中,我们将包名“com.androidride.myapplication”更改为“info.xyz.yourapplication”。

右键单击要更改的包名称,然后选择Refactor->Move。此时会出现一个新对话框,选择将包“com.androidride.myapplication”移动到另一个包,然后单击“确定”。出现警告对话框,单击yes。在新对话框中输入新包名称。但只有“info.xyz”离开你的应用程序。现在,您将看到一个警告对话框,显示Package info.xyz不存在。是否要创建它?单击yes。单击“Do Refactor”。现在,您的包名已更改为info.xyz.myapplication。右键单击info.xyz.myapplication和Refactor->Rename。单击重命名包。重命名对话框:将myapplication更改为您的应用程序。单击Do refactor。删除旧的包名目录并更改build.gradle中的应用程序id,将其更改为info.xyz.yourapplication。这就是全部。

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

对我来说会很好的

Change Following.

步骤1:确保ApplicationId和PackageName相同。例子:

  android {
  defaultConfig {
                     applicationId "com.example.myapplication"
                 }
       }
 and package name the same. package="com.example.myapplication"

步骤2在我的案例中删除iml文件:

  Go To Project> >C:\Users\ashif\Documents\MyApplication  .iml file // delete 
if you forget Project Path Then Go To android Studio Right Click on app and go to  Show in Explorer you get your Project and remove .iml file

步骤3关闭项目

步骤4现在复制您的旧项目并粘贴任何其他根或复制项目文件夹并粘贴任何其他董事。

 Example C:\Users\ashif\Documents/MyApplication 

步骤5

Now open Android Studio and open Existing Project 
C:\Users\ashif\Documents/MyApplication 

试试看会奏效的