如何在基于IntelliJ IDEA的新IDE Android Studio中重命名软件包?
是否包含自动重构?
我想进行大规模重构,但我不知道怎么做。我在Eclipse工作了两年,在Eclipse中它是一个一键操作。
如何在基于IntelliJ IDEA的新IDE Android Studio中重命名软件包?
是否包含自动重构?
我想进行大规模重构,但我不知道怎么做。我在Eclipse工作了两年,在Eclipse中它是一个一键操作。
当前回答
接受的答案对我不起作用。这里有一个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要点
其他回答
转到AndroidManifest.xml文件。将光标放在软件包名称中,如下所示。不要选择它,只需放置它。然后按Shift+F6,您将看到一个弹出窗口,如下所示。选择重命名包。输入新名称并选择Refactor。(注意:由于我的光标位于“something”上,因此只有something被重命名。)
完成了。
右键单击项目面板上的包。
从上下文菜单中选择Refactor->Rename。
IntelliJ IDEA有一个名为“压缩空中间包”的选项。选择“项目”选项卡的选项图标,然后取消/激活该选项。
请参阅:如何更改IntelliJ IDEA中的顶级包名称?
最好的方法是编写新的包名,然后从旧的包名中拖动。
第二种方法是,如果单击Refactor then move选项,然后重命名包名,它将重命名包名并重新生成。
在Build.gradle中,您必须手动执行,如果重构,则不会在Build.ggradle中重命名。
包装有两个目的。一个是在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和清单将使用此新的包名称。