20

I want to remove the middle part from the package name. Ex: com.example.app to com.app

I have tried few things but not able to remove the middle part of the package name without errors.

SkyWalker
  • 855
  • 2
  • 14
  • 36

7 Answers7

23

I think that the only you can achieve that is this:

  1. Press Ctrl+Shift+R, set Text to find to com.example.app, Replace with to com.app and hit Find, All Files.

  2. After that open the directory com in file manager, move app up in hierarchy, so it becomes com/app and remove example directory.

aga
  • 27,954
  • 13
  • 86
  • 121
  • What is the command for this in Mac? for searching in all files – Lucky_girl Oct 10 '19 at 14:56
  • Yeah, sorry, I was working on the Linux machine at the time of writing, so I didn't know any Mac shortcuts. @NeeK thanks for adding this info! – aga Jul 30 '20 at 10:18
11

There is a much better way to do this. com.example.app >> com.app

In Android Studio,

1) Open your project pane (tab on left top) select Android option.

2) In the same pane click on the gear icon and uncheck "Compact Entity Middle Packages".

3) Expand your package hierarchy in Project pane.

4) Now drag the package app (the one you want to move) on the package com (where you want to move).

5) You will see a red boundary around the com package now drop package app.

6) A popup should appear select "Move package", next enter com package path, next select do refactor.

Done!!

Please note, different Studio versions have different options but drag and drop method remains the same

Milind Gaikwad
  • 175
  • 1
  • 7
4

Android Studio indirectly (but easily) supports this requirement;
e.g. Change java.X.Y.myPrecious to java.myPrecious [assume packages X and Y are empty]
(a) Select Android from dropdown heading in Project window.
(b) Using the 'gear/cog' tool uncheck Compact Empty Middle packages
(c) Drag myPrecious to java (be patient if necessary)
(d) Right-click/Delete X
(e) Clean and Rebuild the project making any obvious changes to the Manifest if necessary.

Bad Loser
  • 3,065
  • 1
  • 19
  • 31
0

click on Setting,in setting click compack Empty middle package then click on your package :-> Refactor->Rename ->ok

Munna Sharma
  • 109
  • 1
  • 1
0

-if you package is not devided into more then 3 sub directories best way is to just rename them. Refector -> Rename -> done.

-But when you what to remove the empty one you shoud set structure to android-> compact middle package -> now move you files to the desired folder -> remove the empty ones now -> Done

  • if the package don't have files then you can easily delete, otherwise you can create other folder for all files, then delete this one – Gaurav Mandlik Aug 08 '22 at 12:43
-1

Click with right button in your packet and find REFACTOR.Inside It you have the option Rename and after clicking a window will appear to confirm you with warnings.

Try it!

-1

Basically, You need to move the entire below files to precedent package.

Suppose..

Package: com.app.example.food
if you want to delete example from package.

Move com.app.example."food" directory to com."app" directory directly

Done.

shaiban
  • 331
  • 5
  • 12