First part : 1. first unfold all compacted middle packages At the android studio, the package name you want to edit and click on the setting (gear) icon at the project navigation window. and uncheck " Compact Middle Packages ", it will unfold all directory package
2. right-click on the package name part you want to change in the directory structure. and click on the " Refactor " and it will open submenu and click on the Rename and it asks for you want to Rename Package or Rename Directory, click on the Rename Package it will popup where you can enter a new package name and click Refactor button again
3. then Android studio find all occurrence of the change and show you at the bottom window and click on the Do Refactor
Second part :
now open your build.gradle (module) and change applicationId to the new package name . and also go to AndroidManifiest.xml file and check package name is changed or not, if not do change then click on the sync now button so Android studio builds Gradle again.
now go to Build >> Clean Project - it will clean project Build >> Rebuild Project - it will rebuild project again