如何在基于IntelliJ IDEA的新IDE Android Studio中重命名软件包?
是否包含自动重构?
我想进行大规模重构,但我不知道怎么做。我在Eclipse工作了两年,在Eclipse中它是一个一键操作。
如何在基于IntelliJ IDEA的新IDE Android Studio中重命名软件包?
是否包含自动重构?
我想进行大规模重构,但我不知道怎么做。我在Eclipse工作了两年,在Eclipse中它是一个一键操作。
当前回答
在java目录中创建一个新的包,并将项目移动到该包中。就像您当前的项目id是com.testapp.user,您希望将其更改为xyz.testpp.user。然后在src/java目录中创建一个名为xyz的包,并将您的子包testapp.use移动到xyz中。最后,更新build.gradle和manifest项目id。
其他回答
另一个好方法是:首先右键单击Java文件夹,创建一个具有所需名称的新包→ 新→ 包裹
然后,选择并将所有类拖到新包中。AndroidStudio将到处重构包名。
最后,删除旧包。
完成。
非常重要:
如果使用此方法,则必须手动将AndroidManifest.xml和build.gradle文件更改为新包。
IntelliJ IDEA有一个名为“压缩空中间包”的选项。选择“项目”选项卡的选项图标,然后取消/激活该选项。
请参阅:如何更改IntelliJ IDEA中的顶级包名称?
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要点
选择要重构的包。重构(Refactor)→ 移动→ “将xxx移动到新包”。