Rename package in Android Studio

2018-12-31 01:09发布

How do you rename packages in the new IDE Android Studio, based on IntelliJ IDEA?

Is there an automatic refactoring included?

I want to make bulk refactoring, but I don't know how. I worked two years with Eclipse and in Eclipse it's a one-click operation.

30条回答
墨雨无痕
2楼-- · 2018-12-31 01:32

Right click on package -> refactor and change the name. You can also change it in manifest. Sometimes if u change package name but after creating apk it shows different package name at that time check "applicationId" in build.gradle file.

查看更多
公子世无双
3楼-- · 2018-12-31 01:32

Best way to write the new package name and drag from older package name. Second way if you click Refactor then move option then rename the package name , It will rename the package name then rebuild.

In Build.gradle You have to do manually , If u Refactor then it will not rename in Build.gradle.

查看更多
萌妹纸的霸气范
4楼-- · 2018-12-31 01:32

Additional answer after 3 years, of course if any previous not works for you because "something".

  • Go to project path>where you located java files and package
  • Create new folders, for example com>super_package>activities
  • Android Studio will refresh project structure
  • Move all Java files to new folders
  • Remove old folders
  • Edit>Find>Replace in path:
  • change old package name to new
  • change also in manifest and build gradle

Done!

查看更多
伤终究还是伤i
5楼-- · 2018-12-31 01:33

I have seen the top voted answers but i found is a little bit different to do this, i try to do the most complete tutorial.

From the Android Studio click over the gear icon ( Gears icon ) and then select the option: "Compact Empty Middle Packages", to see the folders separated in a tree view.

introducir la descripción de la imagen aquí

Now select the folder, click right button to open the contextual menu, select Refactor and then Rename

introducir la descripción de la imagen aquí

You will be advised to refactor the package:

introducir la descripción de la imagen aquí

Then a window will show the coincidences inside the proyect, select "Do Refactor":

introducir la descripción de la imagen aquí

We don´t have to change manually the AndroidManifest.xml or build.gradle files, Refactoring the package will do the job!.

查看更多
路过你的时光
6楼-- · 2018-12-31 01:33
  1. Select the package name in Java folder.
  2. Shift+F6
  3. Change package name and Click OK.

Your package name will be changed from all the java files and Manifest file. You have to manually change the package name from build.gradle

查看更多
后来的你喜欢了谁
7楼-- · 2018-12-31 01:35

After you follow one of these techniques to get your package renamed, you might start seeing errors.

If / when your R.java is not getting generated properly, you'll get a lot of errors in your project saying error: cannot find symbol class R and also error: package R does not exist.

Remember to check your Application manifest xml. The manifest package name must match the actual package name. It seems the R.java is generated from the Application Manifest and can cause these errors if there's a mismatch.

Remember to check the package attribute matches in your <manifest package="com.yourcompany.coolapp">

查看更多
登录 后发表回答