Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio does not rearrange import in lexicographic order

Given the following imports

import javax.inject.Inject
import kotlinx.android.synthetic.main.fragment.*

Android Studio rearrange to the following when pressing control + option + o

import kotlinx.android.synthetic.main.fragment.*
import javax.inject.Inject

Which is not in lexicographic order. My pre-commit hook running ktlint then fails because of this. Is there any way to fix this issue?

like image 953
Benjamin Avatar asked Mar 24 '20 11:03

Benjamin


2 Answers

The latest IntelliJ IDEA / Android Studio might not be able to conform to full specifications in the Android Kotlin style guide. So, currently, there is no straight-forward way of arranging the imports lexicographic order in IntelliJ IDEA / Android Studio.

For now, you may want to disable this check for Klint by disabling 'import ordering' in the .editorconfig file in the root of your project:

[*.{kt,kts}]
disabled_rules = import-ordering

This will disable the check in your project.

like image 163
Jeffrey Nyauke Avatar answered Sep 30 '22 17:09

Jeffrey Nyauke


IntelliJ IDEA / Android Studio doesn't let you order imports alphabetically, neither in Java nor in Kotlin.
However Java features an Import layout section, which can give you some control. But still it's impossible to have an exact ordering, as you want it.

enter image description here

This isn't available for Kotlin is now available in Kotlin too. You can look at https://youtrack.jetbrains.com/issue/KT-10974

like image 32
LppEdd Avatar answered Sep 30 '22 18:09

LppEdd