How do you rename packages in new IDE Android Studio, based on intelijj ?
Is there any automatic refactoring included?
I want to make bulk refactoring but I don't know how. I worked 2 years with eclipse and in eclipse it's one click operation.
转载于:https://stackoverflow.com/questions/16804093/android-studio-rename-package
For example, if you want to change com.example.app
to my.awesome.game
, then:
In your Project pane, click on the little gear icon ( )
Uncheck / De-select the Compact Empty Middle Packages
option
Your package directory will now be broken up in individual directories
Individually select each directory you want to rename, and:
Refactor
Rename
Rename Package
instead of Rename Directorycom
in Android Studio, it might give a warning. In such case, select Rename Allbuild.gradle
- Usually app
or mobile
). Update the applicationId
in the defaultConfig
to your new Package Name and Sync Gradle, if it hasn't already been updated automatically:You may need to change the package=
attribute in your manifest.
Clean and Rebuild.
Done! Anyways, Android Studio needs to make this process a little simpler.
Right-click on the package at the Project Panel.
Choose Refactor -> Rename from the context menu.
If your package name is more than two dot separated, say com.hello.world
and moreover, you did not put anything in com/
and com/hello/
. All of your classes are putting into com/hello/world/
, you might DO the following steps to refactoring your package name(s) in Android Studio or IntelliJ:
com/
, com/hello/
). You can achieve this by first add two files to package com.hello.world, saycom.hello.world.PackageInfo1.java com.hello.world.PackageInfo2.java
then refactor them by moving them to com and com.hello respectively. You will see com and com.hello sitting there at the Project(Alt+1
or Command+1
for shortcut) and rename directories refactoring is waiting there as you expected.
Refactor to rename one or more of these directories to reach your aim. The only thing you should notice here is you must choose the directories rather than Packages when a dialog ask you.
If you've got lots of classes in your project, it will take you a while to wait for its auto-scan-and-rename.
Besides, you need to rename the package name inside the AndroidManifest.xml manually, I guess, such that other names in this file can benefit the prefix.
[ALSO], it might need you to replace all com.hello.world.R
to the new XXX.XXX.XXX.R
(Command+Shift+R
for short)
Rebuild and run your project to see whether it work. And use "Find in Path" to find other non-touch names you'd like to rename.
Another good method is: First create a new package with the desired name by right clicking on the java folder -> new -> package.
Then, select and drag all your classes to the new package. AndroidStudio will refactor the package name everywhere.
Finally, delete the old package.
Done.
One more thing very important
You have to Change manually AndroidManifest.xml and build.gradle file to the new package if you use this method.
Intelli J has an option called "Compact Empty Middle Packages". Select the option-icon of the Project-tab and de/activate this.
The first part consists of creating a new package under "java" folder and selecting then dragging all your source files from the old package to this new package. After that you need to remane the package name in android manifest to the name of the new package.
In step 2, here is what you need to do.You need to change the old package name in "applicationId" under the module "build.gradle" in your android studio in addition to changing the package name in the manifest. So in summary, click on "build.gradle" which is below the "AndroidManifest.xml" and modify the value of "applicationId" to your new package name.
Then, at the very top, under "build". clean your project, then rebuild. It should be fine from here.
Packages serve two purposes. One is to uniquely identify your app in the Google Play Store. The other is to name the package for the R.java class which is generated when you build your project. You can think of the first purpose as the external package and the second as the internal package. Assuming you want to change the external package so you can identify in the Play store, there's a convenient way to do this.
In Android Studio, choose File > Project Structure > Choose your app's module > Click on the Flavors tab > change the Application id. Now, when you build your project, your APK and manifest will use this new package name.
select the package will be refactored, refactor->move ->"move xxx to new package"
I tried the two top-voted solutions but found some issues even though both work to some extent.
After some experiments I found the following method works well for me.
If you just need to change the last part of package name, use the method outlined by GreyBeardedGeek, namely
Right-click on the package in the Project pane. Choose Refactor -> Rename from the context menu
If you need to change the whole package name, do the following.
Right-click on the package in the Project pane. Choose Refactor -> Move from the context menu
This will create a new package folder (when necessary) but will keep the last part of your package name as before. If you need to change the last part, do the rename accordingly.
Note also that you may need to modify package names in e.g. build.gradle, manifest, and/or any xml resource files, or even in your code if hardcoded. After all that, do Sync/Clean/Rebuild project as necessary.
Changing the application ID (which is now independent of the package name) can be done very easily in one step. You don't have to touch AndroidManifest. Instead do the following:
Note this will not change the package name. The decoupling of Package Name and Application ID is explained here: http://tools.android.com/tech-docs/new-build-system/applicationid-vs-packagename
This I believe what you are looking for is Refactor > Move. You can also press F6.
You will get two popups. Make sure you select rename package on both. After that you will get a pop up to place the new package name.
Please note that if your package name is, for example, com.example.android.projectname then this will let you change com.example.android to something else.
One more thing, this will update the applicationId in your build.gradle as well. Just make sure that the checkboxes for "search in comments and strings" and "search for text occurrences" are both checked for it to work.
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.
Be sure to go way back up to Sheharyar's Answer that starts off In Android Studio, you can do this:
because it's GREAT. There are so many Answers and comments that follow it that it would be easy to get confused and give up, but DON'T. That Answer WORKS.
In short, you do THREE THINGS:
Deselect Compact Empty Middle Packages
.
Refactor
Rename
each old directory node by choosing CHANGE PACKAGE (not directory) to match new package name. (Be sure to do a preview of changes.)
Edit build.gradle
file and make APPLICATION_ID
match new package name.
The common mistake that one can make is one cannot rename the package structure i.e it is not possible to change com.name.android to com.Renamed.android when one tries to modify at com.name.android level.
In order to have the same desired change go one level up i.e com.name and here when you refactor change it to Renamed. This will work always
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.
Updated answer: May 2015
OK I have been struggling with cloning & renaming projects in Android Studio, but finally I achieved it. Here are the steps to follow:
That's it...
(1)
Open the file:
app >> manifests >> AndroidManifest.xml
Highlight each part in the package name that you want to modify (don't highlight entire package name) then:
Do these steps in each part of the package name
(2)
Open (Gradle Script) >> (build.gradle(Modul:app))
and update the applicationId to your package name
(3)
open the menu (build) and choose (Rebuild Project)
I found another way that works or an extra step to some of the answers here especially if you want to change the domain as well. Works in android studio 1.4. This is what i did:
so for example if you want to rename "com.example.app" to "com.YourDomain.app", open a file under the package to be renamed, in the package breadcrumbs, set your cursor to "example" part of the domain and hit Shift +F6 and rename package to "YourDomain".
Hope this helps :)
shift+F6
you will get a popup window as shown below select Rename package.That's it done.
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
I recommend to use the Sublime Text(or Notepad++). Replace com.one.lastname -> com.two.newname and com/one/lastname -> com/two/newname in ...\Projects[MyProject]. and don't forget to rename ...\Projects\MyProject\app\src\main\java\com\one\lastname, ...\Projects\MyProject\app\src\test\java\com\one\lastname and ...\Projects\MyProject\app\src\androidTest\java\com\one\lastname! That's all:)
Additional answer after 3 years, of course if any previous not works for you because "something".
Done!
That works very well for me, even for changing the occurrences, in all the modules concerned, for the folders names I want to modify in the package but, for me, it works fine only if I follow exactly the following steps :
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">
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 ( ) and then select the option:
"Compact Empty Middle Packages"
, to see the folders separated in a tree view.
Now select the folder, click right button to open the contextual menu, select Refactor
and then Rename
You will be advised to refactor the package:
Then a window will show the coincidences inside the proyect, select "Do Refactor"
:
We don´t have to change manually the AndroidManifest.xml or build.gradle files, Refactoring the package will do the job!.
The approach used by me for renaming the package name is simple as follows:-
Step 1 : Select the Project option from left menu of Android Studio
Step 2 : Right click on java and add a new package and set the desired package name
Step 3 : Enter you new packagename
Step 4 :Copy all the files from your old package and paste in the new package
Step 5 :Rename the package name in manifest file
Step 6 :Rename the package name in build.gradle file
Step 7 :Then right click the old package and delete it with all its data, and delete that directory as well
Step 8 :Then Rebuild your project
Step 9 :Then you will find some errors of old import packagename in your project Select the old package name in any file and press CTRL + Shift + R , and enter you new package name in replace box, then press find
Step 10 :Then a popup appears like below and select All files option from it
Step 11 :Rebuild your project again, bingo your project packagename has been changed :)
Quick and easy way:
1- open MainActivity.java
or any available java file.
At the top there is the package declaration such as:
package com.
example
.myapp;
select the package portion that you want to change and press Shift+F6. I personally want to change the example
.
In the warning dialog, select Rename package and then insert the desired package name.
2- Open AndroidManifest.xml
and inside <manifest>
tag change the package
to the desired package name.
3- open build.gradle(Module: app)
and change the applicationId
to the desired package name.
I've found a way easier solution to this problem which also changed the generated imports like com.test.testpackagechange.R
and only takes about a minute.
Your first step is to open Android Studio and open the replace all window (Mac: cmd + shift + R, Windows I assume: ctrl + shift + R). Type in your old package name and in the one below your new package name. Click Find. This may take a while because it is also looking through the generated items. If it has over 1000 hits, just click continue.
After you've done that push Replace All to replace your old package name with your new one.
Now close Android Studio and go to Finder on Mac or Windows Explorer on Windows. Change the name of the folders to your new package name, like this:
Now open Android Studio again. Gradle will sync and your package name should be changed to the new one.
I've found this to be the easiest one and the one that covers all areas like generated files.
ctrl+shift+R has always worked for me. Simply replace all and choose All Files.
Please try the following steps:
*******Finally it’s done ******