Android 在 Eclipse 中完全重命名项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22935907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Completely renaming a project in Eclipse
提问by NiVeR
I have done Refactor -> Rename in Eclipse and the project has been renamed successfully in Eclipse. But when I copy/paste it in a folder, it still keeps the old name.
我已经在 Eclipse 中完成了 Refactor -> Rename 并且该项目已在 Eclipse 中成功重命名。但是当我将其复制/粘贴到文件夹中时,它仍然保留旧名称。
How can I completely rename it?
我怎样才能完全重命名它?
回答by Lucifer
Open your .project
file from the project folder and change following value in it.
.project
从项目文件夹中打开您的文件并更改其中的以下值。
You need to change to Project name in it.
您需要更改其中的项目名称。
Another way,
其它的办法,
Copy Old project from the Project explorer , and paste it over there, It will ask for new name, give a new name , and done.
从项目资源管理器复制旧项目,并将其粘贴到那里,它会要求新名称,给出一个新名称,然后完成。
回答by Yogesh
1) Right click on your package -> refactor -> rename. select Update references and rename subpackages.
1) 右键单击您的包 -> 重构 -> 重命名。选择更新引用并重命名子包。
2) Change packagename in AndroidMenifest.xml
2) 在 AndroidMenifest.xml 中更改包名
package=”com.example.new_package_name”
3) In resources->values->string.xml change app_name to "new_name"
3) 在资源->值->string.xml 中将 app_name 更改为“new_name”
<string name="app_name">"new_name"</string>
Hope that will work!
希望这会奏效!
回答by Hyman
TrueStudio is also Eclipse based, it probably works in a similar way. However refactoring the project name does not work. The way I clone one project to another is by using Ctrl-C/Ctrl-V on the main folder, and then rename the new folder to the desired name. Now we only need to change a directory name and some file names. And we need to modify the content of a couple of files. After that, the project can be opened in TrueStudio.
TrueStudio 也是基于 Eclipse 的,它可能以类似的方式工作。但是重构项目名称不起作用。我将一个项目克隆到另一个项目的方法是在主文件夹上使用 Ctrl-C/Ctrl-V,然后将新文件夹重命名为所需的名称。现在我们只需要更改一个目录名和一些文件名。我们需要修改几个文件的内容。之后,可以在 TrueStudio 中打开项目。
In short, see below example:
简而言之,请参见以下示例:
Rename Project:
Copy the project directory
“Nucleo-H743ZI_Hyman_01-”
“Nucleo-H743ZI_Hyman_010 - Copy”
Rename it to the new name
“Nucleo-H743ZI_Hyman_010 - Copy”
“Nucleo-H743ZI_Hyman_011_tcp”
Rename this directory
“Nucleo-H743ZI_Hyman_011_tcp\TrueSTUDIO\Nucleo-H743ZI_Hyman_010”
“Nucleo-H743ZI_Hyman_011_tcp\TrueSTUDIO\Nucleo-H743ZI_Hyman_011_tcp”
Rename these files
“Nucleo-H743ZI_Hyman_011_tcp\Nucleo-H743ZI_Hyman_010.ioc”
“Nucleo-H743ZI_Hyman_011_tcp\Nucleo-H743ZI_Hyman_011_tcp.ioc”
“Nucleo-H743ZI_Hyman_011_tcp\TrueSTUDIO\Nucleo-H743ZI_Hyman_011_tcp\Nucleo-H743ZI_Hyman_010.elf.launch”
“Nucleo-H743ZI_Hyman_011_tcp\TrueSTUDIO\Nucleo-H743ZI_Hyman_011_tcp\Nucleo-H743ZI_Hyman_011_tcp.elf.launch”
Change these files
“Nucleo-H743ZI_Hyman_011_tcp\.mxproject” (3 occurrences)
“Nucleo-H743ZI_Hyman_011_tcp\Nucleo-H743ZI_Hyman_011_tcp.ioc” (2 occurrences)
“Nucleo-H743ZI_Hyman_011_tcp\TrueSTUDIO\Nucleo-H743ZI_Hyman_011_tcp\.cproject” (3 occurrences)
“Nucleo-H743ZI_Hyman_011_tcp\TrueSTUDIO\Nucleo-H743ZI_Hyman_011_tcp\.project” (1 occurrence)
“Nucleo-H743ZI_Hyman_011_tcp\TrueSTUDIO\Nucleo-H743ZI_Hyman_011_tcp\Nucleo-H743ZI_Hyman_011_tcp.elf.launch” (5 occurrences)
Note: also get rid of absolute paths in Nucleo-H743ZI_Hyman_011_tcp\TrueSTUDIO\Nucleo-H743ZI_Hyman_011_tcp\.project
Open project
File -> Open Project from File System…
Directory: “Nucleo-H743ZI_Hyman_011_tcp\TrueSTUDIO\Nucleo-H743ZI_Hyman_011_tcp”
Click OK and Finish
A python script placed in the root directory of the project can do this:
一个放在项目根目录下的python脚本可以做到这一点:
chproj.py
# Changed the name of an TrueStudio project
import os
import sys
def inplace_change(filename, old_string, new_string):
# Safely read the input filename using 'with'
with open(filename) as f:
s = f.read()
if old_string not in s:
print '"{old_string}" not found in {filename}.'.format(**locals())
return
# Safely write the changed content, if found in the file
with open(filename, 'w') as f:
print 'Changing "{old_string}" to "{new_string}" in {filename}'.format(**locals())
s = s.replace(old_string, new_string)
f.write(s)
# Getting the current work directory (cwd)
dir_abs = os.getcwd()
dir_abs_split=dir_abs.split('\')
n = len(dir_abs_split)
dst_dir = dir_abs_split[n-1]
new_name = dst_dir
print dir_abs
# Get original name
#src_dir = os.listdir(ts_dir)[0]
#old_name = src_dir
#print "old_name: " + old_name
mxproject_filename = dir_abs + "\.mxproject"
with open(mxproject_filename) as f:
content = f.readlines()
second_line = content[1]
#print second_line
second_line_split=second_line.split('/')
n=len(second_line_split)
old_name = second_line_split[n-2]
print "old_name: " + old_name
print "new_name: " + new_name
ioc_filename_old = dir_abs + "\" + old_name + ".ioc"
ioc_filename_new = dir_abs + "\" + new_name + ".ioc"
ts_dir = dir_abs + "\TrueSTUDIO"
ts_name_old = ts_dir + "\" + old_name
ts_name_new = ts_dir + "\" + new_name
elf_launch_old = ts_dir + "\" + new_name + "\" + old_name + ".elf.launch"
elf_launch_new = ts_dir + "\" + new_name + "\" + new_name + ".elf.launch"
cproject = ts_dir + "\" + new_name + "\.cproject"
project = ts_dir + "\" + new_name + "\.project"
print "Change path in " + project
new_path = "PARENT-2-PROJECT_LOC"
old_path = dir_abs.replace("\", "/")
old_path = old_path.replace("c:", "C:")
print old_path
print new_path
if os.path.isfile(project):
# file exists
print "Modify file " + project
inplace_change(project, old_path, new_path)
if (new_name == old_name):
print "Nothing else to change"
sys.exit(0)
print "Rename directories and files:"
#os.rename(src, dst)
if os.path.isdir(ts_name_old):
# dir exists
print "Rename directory " + ts_name_old + " to " + ts_name_new
os.rename(ts_name_old, ts_name_new)
#os.rename(src, dst)
if os.path.isfile(ioc_filename_old):
# file exists
print "Rename file " + ioc_filename_old + " to " + ioc_filename_new
os.rename(ioc_filename_old, ioc_filename_new)
if os.path.isfile(elf_launch_old):
# file exists
print "Rename file " + elf_launch_old + " to " + elf_launch_new
os.rename(elf_launch_old, elf_launch_new)
print "Replace strings in files:"
if os.path.isfile(cproject):
# file exists
print "Modify file " + cproject
inplace_change(cproject, old_name, new_name)
if os.path.isfile(project):
# file exists
print "Modify file " + project
inplace_change(project, old_name, new_name)
inplace_change(project, old_path, new_path)
if os.path.isfile(ioc_filename_new):
# file exists
print "Modify file " + ioc_filename_new
inplace_change(ioc_filename_new, old_name, new_name)
if os.path.isfile(elf_launch_new):
# file exists
print "Modify file " + elf_launch_new
inplace_change(elf_launch_new, old_name, new_name)
if os.path.isfile(mxproject_filename):
# file exists
print "Modify file " + mxproject_filename
inplace_change(mxproject_filename, old_name, new_name)
Any comments or suggestions for improvements are welcome!
欢迎提出任何改进意见或建议!
回答by Josi
COPY AND PASTE ANDROID PROJECT and CREATE a NEW PROJECT is neat. Follow the following steps below:
复制和粘贴 ANDROID 项目并创建一个新项目是整洁的。请按照以下步骤操作:
A. If you are using Eclipse and all you need to do is first open the project that you want to copy(DON"T FORGET TO OPEN THE PROJECT THAT YOU NEED TO COPY), then clone(copy/paste) your Android project within the explorer package window on the left side of Eclipse. Eclipse will ask you for a new project name when you paste. Give it a new project name. Since Eclipse project name and directory are independent of the application name and package, the following steps will help you on how to change package names. Note: there are two types of package names. (The main package as it indicated in Manifest file and sub-package which holds all java files)
A. 如果您使用的是 Eclipse 并且您需要做的就是首先打开要复制的项目(不要忘记打开您需要复制的项目),然后在其中克隆(复制/粘贴)您的 Android 项目Eclipse左侧的资源管理器包窗口,粘贴时Eclipse会要求你输入一个新的项目名称,给它一个新的项目名称。由于Eclipse项目名称和目录与应用名称和包无关,所以下面的步骤会帮助您更改包名。注意:包名有两种类型。(Manifest 文件中指示的主包和包含所有 java 文件的子包)
1. After you get done above step, to change Application package name(main package), follow the following steps:
First right click your project
Then go to "Android tools"
Then select "Rename Application package"
Enter a new name in a dialogue window , and hit OK.
Then It will show you in which part of your project the Application name will be changed. It will show you that
the Application name will be changed in manifest, and in most relevant Java files. Hit "OK"
YOU DONE in this part, but make sure you rebuild your project to take effect.
To rebuild your project, go to ""project" >>> "Clean" >>>> select a Project from a projects list, and hit "OK"
Finaly, you can run your new project.
2. To change src package(sub package) names of packages within Src folder follow the following steps:
First you need to create new package: (src > right click > new > package).
Example of creating package: com.myCompany.executable
Follow these steps to move the Java files from the old that has already been copied package in part A to your new package.
Select the Java files within that old package
Right click that java files
select "Refactor" option
select "Move" option
Select your preferred package from the list of packages in a dialogue window. Most probably you need to select the new one you just created and hit "OK"