MPelletier
MPelletier

Reputation: 16687

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.

Upvotes: 13

Views: 5213

Answers (2)

Chris Cartland
Chris Cartland

Reputation: 3278

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

Upvotes: 8

fal
fal

Reputation: 3075

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

Imports

Upvotes: 21

Related Questions