Remove unused imports in Android Studio

Android Studio

Android Studio Problem Overview


I recently started Android Studio IDE to make my android apps. I find shortkey to remove unused import in Android Studio is not working (Ctrl+Shift+O)

What is shortcut key to do same in Android Studio?

Android Studio Solutions


Solution 1 - Android Studio

Simple, right click on your project in Android Studio, then click on the Optimize Imports that should work.

screenshot

Update

To do same thing which I described above, you can do same just pressing Ctrl+Alt+O, it will optimize imports of your current file and your entire project depends on your selection in a dialog.

enter image description here

Solution 2 - Android Studio

You can do it on the fly. You don't need to call (Ctrl+Shift+O) or "Project/Optimize Imports..." each time.

Just set this checkbox in Settings -> Editor -> General -> Auto Import -> Optimize Imports on the fly.

enter image description here

On OSX: Preferences -> Editor -> General -> Auto Import -> Optimize imports on the fly

Solution 3 - Android Studio

Press Ctrl + Alt + O.

A dialog box will appear with a few options. You can choose to have the dialog box not appear again in the future if you wish, setting a default behavior.

enter image description here

Solution 4 - Android Studio

It is very Simple Just Follow the below step.

  1. Switch your project in Project Mode.

enter image description here

  1. Then right-click on project name.
  2. The final step is to select the Optimize imports from popup menu.

Enjoy!!

Solution 5 - Android Studio

Sorry for the late answer.. For mac users command + option + o Try this.. It is working for me..

Solution 6 - Android Studio

Ctrl+Alt+O works pretty well and removes unused imports

Solution 7 - Android Studio

On Mac use control + option + O

Solution 8 - Android Studio

there is also an Android Studio (1.4) setting to optimze imports on the fly: see Settings->Editor->General->Auto Import.

Solution 9 - Android Studio

I think Ctrl + Alt + O works when the import is unused but a valid import. However, say you try to import a class from a package that does not exist or no longer exists (which can happen during refactoring), the shortcut command does not work (atleast it didn't for me). If you have more than one file like this, things can get problematic.

To solve this problem, click on Analyse -> Inspect code -> (select your module / project). Let it perform the analysis. Go down to Imports -> Unused imports. Click on the "Delete unnecessary import" button that appears on the right.

Solution 10 - Android Studio

Press Alt + Enter with the cursor on top of the import. The Optimize imports menu will show. Press Enter again. Your unused imports will be removed.

enter image description here

Solution 11 - Android Studio

Since Android Studio 3+, this can be done by open the option "Optimize imports".

Alt+Enter the select "Optimize imports".

enter image description here

This must be enough to removed the unused imports.

enter image description here

Solution 12 - Android Studio

On MacOS (Monterey) + Android Studio (Arctic Fox 2020.3.1): To Remove unused imports, follow below procedures.

Android Studio -> Code -> Optimize Imports

Shortcut key: Option + Command + O

Please see screenshot for more clarification. Thanks.

android studio screenshot on mac

Solution 13 - Android Studio

you can use Alt + Enter in Android Studio as Shortcut Key

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionandroidView Question on Stackoverflow
Solution 1 - Android StudioAjay SView Answer on Stackoverflow
Solution 2 - Android Studioyital9View Answer on Stackoverflow
Solution 3 - Android StudioJason HartleyView Answer on Stackoverflow
Solution 4 - Android StudioDeepak guptaView Answer on Stackoverflow
Solution 5 - Android StudioSimon ChiusView Answer on Stackoverflow
Solution 6 - Android StudioSamirView Answer on Stackoverflow
Solution 7 - Android StudioKavya ShravanView Answer on Stackoverflow
Solution 8 - Android StudioaxdView Answer on Stackoverflow
Solution 9 - Android StudioRaghuveerView Answer on Stackoverflow
Solution 10 - Android Studiolive-loveView Answer on Stackoverflow
Solution 11 - Android StudioJorgesysView Answer on Stackoverflow
Solution 12 - Android StudioMd. ArifView Answer on Stackoverflow
Solution 13 - Android StudioErumView Answer on Stackoverflow