Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control when Imports are replaced by wildcard in Android Studio in Kotlin files

I noticed that for up to 4 imports from the same package, auto-import will simply append the import in the list:

import android.support.v7.widget.AppCompatImageButton
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.Toolbar

But adding a 5th import will make auto-import switch to a wildcard:

import android.support.v7.widget.*

Is there a way to keep auto-import on but tell it to not do that, or change the threshold? I don't mind having a long list of imports, it feels... familiar.

like image 801
MPelletier Avatar asked Sep 20 '17 20:09

MPelletier


2 Answers

You can set this under the Kotlin "Code Style" section in Preferences:

Imports

like image 195
fal Avatar answered Sep 16 '22 20:09

fal


The Android Kotlin Style Guide says:

Wildcard imports (of any type) are not allowed.

You can set your Android Studio preferences to remove all wildcard imports from Kotlin files.

  1. Open Android Studio Preferences: Editor > Code Style > Kotlin
  2. Select Use single name import for Top-level Symbols
  3. Select Use single name import for Java Statics and Enum Members
  4. Remove all packages from Packages to Use Import with by clicking the minus - symbol until it says Nothing to show

Screenshot of Android Studio 4.1.2 Settings: No Kotlin Wildcard Imports

like image 27
Chris Cartland Avatar answered Sep 17 '22 20:09

Chris Cartland