Renaming modules in Android Studio?
AndroidGradleAndroid StudioAndroid Problem Overview
I picked the wrong name of a specific module which I imported to the Gradle project in Android Studio.
In this face I want to rename module Facebook1
to Facebook
.
Is this possible in Gradle project and how to do it?
Android Solutions
Solution 1 - Android
Android Studio >= 1.4.1
- Right click the module -> Refactor -> Rename.
- Enter the new name of the module, click OK.
Older versions of Android Studio
The following worked for me, tested in Android Studio (AS) 1.1 & 1.0.2, directly after importing a project.
Under the 'Android' tree view (ATV),
-
Right click the module name:
app
, Refactor -> Rename (tomyApp
, click ok) -
Open
settings.gradle
-
Change
':app'
to':myApp'
, and save -
You will be prompted to 'Sync Now', do it (the prompt is a blue link above the file)
(Your project will disappear from ATV, but don't worry, just exit AS)
-
From a file manager (outside of Android Studio), open your project root directory, and rename the folder
app
tomyApp
-
Re-open your project in AS, and you will see your module and Gradle scripts re-appear under ATV! :)
Congratulations - your module has now been renamed! (whew, time for some tea)
Solution 2 - Android
In the project view on the left in Android Studio,
- right click on the module whose name you want to change
- select 'refactor' -> 'rename'
- choose the rename module option
- follow step 1 - 2 and then rename the directory too
Solution 3 - Android
Android Studio 1.3.2,
1. switch Project view on the left in Android Studio
2. right click module which want to change -> Refactor -> Rename
3. do both "Rename directory" and "Rename module"
4. open settings.gradle, change module name
5. Build -> Clean Project
Solution 4 - Android
You can do it manually, with or without changing the folder structure (consider not change it for a minor impact on version controlling)
Changing folder name accordingly
1- open settings.gradle
and change the name of your module (exemple from ':facebook1'
to ':facebook'
)
2- change the folder name accordingly (from xxx/facebook1
to xxx/facebook
)
3- If other Modules have dependencies on it: open the corresponding build.gradle
and change dependency (from compile project(':facebook1')
to compile project(':facebook')
)
Without changing folder name
1- open settings.gradle
and change the name of your module (exemple from ':facebook1'
to ':facebook'
)
2- add a new line on "settings.gradle":
project(':facebook').projectDir = new File(settingsDir, '/facebook1')
3- If other Modules have dependencies on it: open the corresponding build.gradle
and change dependency (from compile project(':facebook1')
to compile project(':facebook')
)
Solution 5 - Android
> As of Android Studio 3.4.1
Rename module
- In Project window, right-click module and select
Refactor > Rename
- Choose
Rename module
and pressOK
- Enter new module name and press
OK
- Important
settings.gradle
is updated automatically. However, you must manually update anybuild.gradle
dependencies to reflect new module name:
dependencies {
// manually update this after renaming the module
implementation project(':newmodulename')
}
Rename package to reflect new module name
- In Project window, right-click desired package and select
Refactor > Rename
- If there are other src sets with same package name (
test
orandroidTest
), a dialog will ask if you want to rename them as well. If so, selectRename package
- Enter new package name and press
Refactor
- Select
Do Refactor
in the Refactoring Preview
> Tip: Select File > Invalidate Caches / Restart
to fix any unresolved resource references caused by renaming a module.
Solution 6 - Android
In AndroidStudio 2.2.2 renaming the module alone via refactor/rename worked for me. (32-bit linux Mint)
The rename dialog presents two options: "directory" or "module" (radio buttons so mutually exclusive, at least in one pass). I wasn't sure which to pick so I ended here looking for an answer. One answer said "do both" as two steps, another answer said "do module, then directory" as two steps. So I chose "module" first. To my surprise that was all that was needed, both module and directory were changed. settings.gradle was updated by the refactor/rename in the one step as well.
"gradle synce" and "clean build" were also triggered. Upon build to my tablet, no complaint about the name change.
just adding this answer in case someone else with 2.2.2 or later is unsure which to pick. Also to re-iterate what others have said, back up your project first.
Solution 7 - Android
- Close Project and rename module folder
oldName
tonewName
then open Project and change module nameinclude ':oldName'
toinclude ':newName'
insettings.gradle
Solution 8 - Android
This is working for me in Android Studio 1.2.1.1
Premise: I do NOT use the refactor function of Android Studio to rename the module
These are the steps I perform:
- Rename the folder of the module (directly in the file system)
- Modify the setting.gradle file accordingly (directly in the file system)
- Open the project in Android Studio
That's all.
Solution 9 - Android
Different people seem to have success with different methods, but when I wanted to rename my project from:
com.example.myname.projectname
to
com.company.projectname
I tried everything and the only way I got it to work in the end was:
- Exit Android Studio
- back everything up! (find your project folder and make a copy of it)
- Find and replace all instances of com.example.myname.projectname with com.company.projectname
Note that there are five places (actually it probably depends on your project, I had five) where you need to rename folders, as it creates subfolders called com\example\myname\projectname:
project\app\src\main\java
project\app\src\androidTest\java
project\app\build\generated\source\buildConfig\debug
project\app\build\generated\source\buildConfig\test
project\app\build\generated\source\r\debug\
Also, in project.idea\ there is a file called workspace.xml - in that file you'll need to find the text com\example\myname\projectname
Otherwise, do a search through the project directory to find "com.example.myname.projectname" and replace with "com.company.projectname"
-
Open Android Studio again. if the initial project directory name is the same (possibly if the project name itself is the same?) it will reopen if you had it open last time, otherwise you should be able to open as normal.
-
Go to File - Project Structure (CONTROL+ALT+SHIFT+S).
-
Under "Flavors", change the application ID to the new name.
-
Excit Android Studio and restart it.
-
Now you should be able to build your project.
-
Go have a drink, that was a lot more effort than it should have been!
One really important note, you can only have ASCII letters in your package name (you may be able to have numbers, but not as the first character). Java itself allows numbers to start a package name, but Android does not: https://stackoverflow.com/questions/28666297/package-names-in-android-studio-when-dev-name-starts-with-a-number/28666328#28666328 I was trying to rename
My workaround (and from a look around the apps on my phone it seems to be what others are doing) is if you wanted a number at the start of either your company name or app name, write the word - instead of com.123company.55project perhaps use com.onetwothreecompany.fiftyfiveproject