如何在基于IntelliJ IDEA的新IDE Android Studio中重命名软件包?
是否包含自动重构?
我想进行大规模重构,但我不知道怎么做。我在Eclipse工作了两年,在Eclipse中它是一个一键操作。
如何在基于IntelliJ IDEA的新IDE Android Studio中重命名软件包?
是否包含自动重构?
我想进行大规模重构,但我不知道怎么做。我在Eclipse工作了两年,在Eclipse中它是一个一键操作。
当前回答
右键单击package->refactor并更改名称。
您也可以在清单中更改它。有时,如果您更改包名,但在创建.apk文件后,它会显示不同的包名。此时,检查build.gradle文件中的“applicationId”。
其他回答
右键单击项目面板上的包。
从上下文菜单中选择Refactor->Rename。
例如,如果您希望将包从com.example.a更改为com.example.b
1) 将项目样式文件夹从android更改为package(在项目端)
2) 右键单击文件夹顶部,然后单击路径中的替换
3) “在项目中选择”按钮
4) 在第一输入write com.example.a
5) 在第二输入中写入com.example.b
6) 当您看到警告时,选择“全部替换”以替换值
完成替换后,您可以看到您的包名为com.example.b
ctrl+Shift+R一直对我有效。只需替换所有文件并选择所有文件。
接受的答案对我不起作用。这里有一个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要点
如果您的包以mypackageold.something.test开头,则不能更改为mypackagenew.somethingnew.testnew。
在这种情况下,如果我按照上面接受的回答步骤,结果将是mypackageold.something.testnew。
只有当您的包以com….to New包开头时,上述接受的答案才有效。
因此,如果您想从根级别更改包
更改清单中的Required包名称,移除旧软件包并在Android标签下装入新软件包,将所有文件从旧包拖到新包,在Manifest中,还可以更改这些文件的包名,清洁项目,打开build.gradle并更新applicationId。