如何在基于IntelliJ IDEA的新IDE Android Studio中重命名软件包?
是否包含自动重构?
我想进行大规模重构,但我不知道怎么做。我在Eclipse工作了两年,在Eclipse中它是一个一键操作。
如何在基于IntelliJ IDEA的新IDE Android Studio中重命名软件包?
是否包含自动重构?
我想进行大规模重构,但我不知道怎么做。我在Eclipse工作了两年,在Eclipse中它是一个一键操作。
当前回答
我找到了一个更容易解决这个问题的方法,它也改变了生成的导入,比如com.test.testpackagechange.R,只需要一分钟。
第一步是打开Android Studio并打开替换所有窗口(Mac:cmd+shift+R,我假设Windows为:ctrl+shift+R)。输入您的旧软件包名称和新软件包名称下面的名称。单击查找。这可能需要一段时间,因为它也在查看生成的项目。如果点击次数超过1000次,只需单击“继续”。
完成后,按下“全部替换”,用新的包名替换旧的包名。
现在关闭Android Studio并转到Mac上的Finder或Windows上的Windows Explorer。将文件夹的名称更改为新的程序包名称,如下所示:
现在再次打开Android Studio。Gradle将同步,您的包名应更改为新的包名。
我发现这是最简单的一个,它涵盖了所有领域,比如生成的文件。
其他回答
接受的答案对我不起作用。这里有一个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突出显示要修改的包名称中的每个部分(不要突出显示整个包名称),然后:鼠标右键单击→ 重构(Refactor)→ 重命名→ 重命名程序包键入新名称并按(Refactor)在包名称的每个部分执行这些步骤。打开(Gradle脚本)>>(build.Gradle(Modul:app))并将applicationId更新为包名打开菜单(构建)并选择(重建项目)。
对我来说会很好的
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
试试看会奏效的
我找到了一个更容易解决这个问题的方法,它也改变了生成的导入,比如com.test.testpackagechange.R,只需要一分钟。
第一步是打开Android Studio并打开替换所有窗口(Mac:cmd+shift+R,我假设Windows为:ctrl+shift+R)。输入您的旧软件包名称和新软件包名称下面的名称。单击查找。这可能需要一段时间,因为它也在查看生成的项目。如果点击次数超过1000次,只需单击“继续”。
完成后,按下“全部替换”,用新的包名替换旧的包名。
现在关闭Android Studio并转到Mac上的Finder或Windows上的Windows Explorer。将文件夹的名称更改为新的程序包名称,如下所示:
现在再次打开Android Studio。Gradle将同步,您的包名应更改为新的包名。
我发现这是最简单的一个,它涵盖了所有领域,比如生成的文件。
更新答案:2015年5月
好吧,我一直在努力在Android Studio中克隆和重命名项目,但最终我实现了这一目标。以下是需要遵循的步骤:
复制项目文件夹,重命名并用Android Studio打开从资源管理器重命名模块目录重命名projectName.iml和内容重命名idea/.name内容在项目窗格中,单击小齿轮图标->取消选中“压缩空中间包”为新的包名重构src目录(重命名包,“不重命名目录”)在build.gradle中重命名应用程序idsettings.gradle重命名模块
就是这样。。。