Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Android Studio imports the complete package on Reformatting the code

I have a class which uses HashMap and I have imported import java.util.HashMap.

On executing reformat code -> eclipse shortcut used Cntrl+Shift+R

It reformats and convert to import java.util.*

Why android studio imports all classes if we need only one , here HashMap? Isn't it unnecessary.

like image 739
Aada Avatar asked Jul 04 '19 05:07

Aada


People also ask

What is the use of import in Android Studio?

The import process prompts you to migrate any library and project dependencies to Android Studio, and add the dependency declarations to the build. gradle file. For more about migrating library and project dependencies, see Create an Android library.


2 Answers

If any package is having more than N numbers of import of the same package in Android studio, it automatically converts it to * imports for that package (default are 5 for Top-level Symbols and 3 for Java Statics and Enum Members).

So, for example if you're having some N number of imports from package java.util, it gets converted to java.util.*.

Where to find that setting to change it?

  1. Open Settings from File-> Settings in Android Studio (ctrl+alt+s).

  2. Go to Editor -> Code Style -> Java/Kotlin and open imports tab then change that N number to any of your suitable case.

enter image description here

There are other options available as well.

like image 140
Jeel Vankhede Avatar answered Oct 03 '22 05:10

Jeel Vankhede


From Intellij official documentation

When the number of classes that IntelliJ IDEA has imported from the same package reaches the limit (5 by default), the IDE modifies the statements in order to import the entire package instead of importing several single classes from this package.

You can disable this feature like following.

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), select Code Style | Java | Imports.

  2. Make sure that the Use single class import option is enabled.

  3. In the Class count to use import with ‘*’ and Names count to use static import with ‘*’ fields, specify values that definitely exceed the number of classes in a package and the number of names in a class (for example, 999).

like image 25
galcyurio Avatar answered Oct 03 '22 04:10

galcyurio