如何在基于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要点
其他回答
接受的答案对我不起作用。这里有一个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要点
选择选项取消选中“压缩空中间包”选项。选择要更改的目录(我选择了步骤6中显示的“crl”)。移位+F6重命名程序包将目录crl重命名为crl1最后单击下图中标记的Do Refactor按钮在此处输入代码完成更改后
包装有两个目的。一个是在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和清单将使用此新的包名称。
我在这里找到了另一种可行的方法,或者对一些答案采取了额外的步骤,特别是如果你想改变域名的话。它在Android Studio 1.4中工作。这是我所做的:
打开Manifest.xml并将包名称更改为所需的名称。打开appbuild.gradle文件,将defaultConfig中的应用程序Id更改为清单中的相同名称,然后重建项目。如果仍然存在问题,请在包名下打开一个文件,转到包面包屑(即文件开头的包声明)并将光标设置为要更改的域,然后单击“Shift+F6”,会出现一个带有多个使用警告的对话框,单击“重命名包”,然后单击”Do Refactor“,它应该重命名包括R.Java文件在内的所有内容。
例如,如果要将“com.example.app”重命名为“com.YourDomain.app”,请在要重命名的包下打开一个文件,在包面包屑中,将光标设置为域的“example”部分,然后按Shift+F6并将包重命名为“YourDomain”。
我没有看到任何人指出这一点,这就是为什么我要写这个答案。我会把这个答案添加为评论,因为它真的不应该是一个答案,但我在Stackoverflow上没有足够的信誉点来留下评论。
我想将我的包名从com.param.myapp更改为com.pvs.myapp。我在互联网上遵循了每一个解决方案,但忘记将android块build.gradle(:app)中的命名空间“com.param.miapp”更改为“com.pvs.myapp”。我不确定名称空间行是否需要存在,但它给了我错误。AndroidStudio使用旧包名生成类。
记住要检查一下!