How to rename package name in Android Studio


If you are working on an Android Project and working with IntelliJ's IDEA Android Studio and wondering how to rename java package name, here is how you can do it,

Steps to Rename package Name: Android Studio
  1. Open your project, make sure you are in Project View
  2. Expand your app module and look for java, when you expand it you will see all your packages,
  3. Right click on the package example com.abc.mypackage and go to Refactor -> Rename ..., you can also use shortcut Shift + F6
  4. You would see a warning window,
    Multiple directories correspond to package
    com.example.myapp:
    
    C:\Users\c2c\AndroidStudioProjects\MyApp\app\src\main\java\com\example\myapp
    C:\Users\c2c\AndroidStudioProjects\MyApp\app\src\androidTest\java\com\example\myapp
    C:\Users\c2c\AndroidStudioProjects\MyApp\app\src\test\java\com\example\myapp
    
    also generated:
    C:\Users\c2c\AndroidStudioProjects\MyApp\app\build\generated\source\buildConfig\debug\com\example\myapp
    
    Do you want to rename the whole package or directory 
    C:\Users\c2c\AndroidStudioProjects\MyApp\app\src\main\java\com\example\myapp only?
    you can select Rename Package
  5. You would see an Rename window, change your package name and and click refactor
  6. Under Refactoring, you can see what code will be changed, click do refactor after validating,
    Directory to be renamed to com.example.myapp1, Package to be renamed to myapp1
        myapp
        myapp
        myapp
        myapp
        myapp
    References in code to directory com.example.myapp, package com.example.myapp (10 references in 9 files)
      (9 usages found)
        Unclassified usage  (5 usages found)
            app  (5 usages found)
                com.example.myapp  (5 usages found)
                    ExampleInstrumentedTest.java  (1 usage found)
                        1 package com.example.myapp;
                    ExampleUnitTest.java  (1 usage found)
                        1 package com.example.myapp;
                    FirstFragment.java  (1 usage found)
                        1 package com.example.myapp;
                    MainActivity.java  (1 usage found)
                        1 package com.example.myapp;
                    SecondFragment.java  (1 usage found)
                        1 package com.example.myapp;
        Usage in Android manifest  (1 usage found)
            app  (1 usage found)
                app\src\main  (1 usage found)
                    AndroidManifest.xml  (1 usage found)
                        3 package="com.example.myapp">
        Usage in Android resources XML  (3 usages found)
            app  (3 usages found)
                menu  (1 usage found)
                    menu_main.xml  (1 usage found)
                        4 tools:context="com.example.myapp.MainActivity">
                navigation  (2 usages found)
                    nav_graph.xml  (2 usages found)
                        10 android:name="com.example.myapp.FirstFragment"
                        20 android:name="com.example.myapp.SecondFragment"
    References in generated code to directory com.example.myapp, package com.example.myapp (10 references in 9 files)  (1 usage found)
        Unclassified usage  (1 usage found)
            app  (1 usage found)
                com.example.myapp  (1 usage found)
                    BuildConfig.java  (1 usage found)
                        4 package com.example.myapp;
  7. Note: You would require to make appropriate changes to build.gradle file manually.
  8. Build your project again and you should be done!

Another way of doing it is by creating a new package instead and copying all your contents there - but here you would require to manually update your AndroidManifest.xml and build.gradle file.