Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 590
Randy Sugianto 'Yuku' Avatar asked Jun 28 '19 21:06

Randy Sugianto 'Yuku'


People also ask

How do I change the import order in Intellij?

Open the 'Settings' (or 'Preferences' in mac) window and goto Editor > Code Style > Java. Click on 'Imports' tab. In 'Import Layout' area, you can arrange the import order by selecting the import type and clicking on the arrow (see image below). Once the changes are done apply the new settings.

How to fix imports in IntelliJ?

To optimize imports in a file, you can also press Ctrl+Alt+Shift+L , select Optimize imports, and click Run.

What is the shortcut for importing the methods?

You can simply import all of them by pressing the ALT + ENTER key.


1 Answers

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.

like image 106
ordonezalex Avatar answered Sep 21 '22 08:09

ordonezalex