How to customize Android Studio's import ordering in Kotlin to ignore whether they are "static" imports?
Asked Answered
R

2

16

Using the "Optimize Imports" in Android Studio 3.4.1, the imports are ordered similar to this:

import com.walla.walla
import com.willy.willy
import org.koin.android.ext.android.get
import org.koin.androidx.viewmodel.ext.android.viewModel
import kotlin.concurrent.thread // <-- note this line
import kotlin.random.Random

As you can see above, function (a.k.a. "static" import in Java) imports like kotlin.concurrent.thread and kotlin.random.Random are put under other imports.

It is not consistent with the Android Kotlin style guide:

Import statements for classes, functions, and properties are grouped together in a single list and ASCII sorted.

I couldn't find a way to make it such that Android Studio order imports irrespective of whether the import is a class or a function. Is there an option to make it so?

Redfield answered 28/6, 2019 at 21:11 Comment(1)
After try to import many dependency to class, I see that import java... and import kotlin... always in bottom (ASCII sorted itself). Another import on top is in ASCII sorted (look correct follow the document). Still don't know why all import is not in ASCII sorted @@Adjacent
N
11

This seems to be a misunderstanding. In fact, kotlin.concurrent.thread is a function. Therefore, it should be grouped together with the other classes.

UPDATE: I do see that the latest version of IntelliJ 2019.1 (and Android Studio) might not be able to conform to the Android Kotlin style guide. If you have these import statements, then IntelliJ does not sort strictly by ASCII:

import org.apache.commons.lang3.StringUtils
import java.util.Base64
import kotlin.concurrent.thread

Instead, IntelliJ orders them as:

  1. Third-party
  2. Java
  3. Kotlin

I do not see a way to configure IntelliJ or Android Studio to sort them this way:

import java.util.Base64
import kotlin.concurrent.thread
import org.apache.commons.lang3.StringUtils

Perhaps you should submit some feedback to IntelliJ or the authors of the Android Kotlin style guide.

Nosewheel answered 28/6, 2019 at 21:17 Comment(3)
Yes, sorry about the misunderstanding. What I meant by "static" import was indeed function import. The question is how to make AS not put function imports under class imports.Cystectomy
using companion object in kotlin ?Fourthly
@RandySugianto'Yuku', I've updated my answer. I do not think there is a solution with the current versions of IntelliJ and Android Studio. Good luckNosewheel
G
0

Goto Help , Find Action and type Optimize Imports or press Ctrl+Alt+O

Gluteus answered 1/3, 2023 at 17:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.