Android Studio does not rearrange import in lexicographic order - android

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?

Its strange behavior but if you delete all from Import aliases separately you will get lexicographical order
Before:
After:

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.

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.
This isn't available for Kotlin is now available in Kotlin too. You can look at https://youtrack.jetbrains.com/issue/KT-10974

You could disable import ordering for that file.
I had this:
import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import java.io.Serializable
import kotlinx.android.parcel.Parcelize
Ktlint reordered it that way, but check continued to fail:
Had to add the ignore in the first line:
import android.os.Parcelable // ktlint-disable import-ordering
import com.google.gson.annotations.SerializedName
import java.io.Serializable
import kotlinx.android.parcel.Parcelize

If you are in IntelliJ/Android Studio, usually just clicking Code -> Optimize Imports is enough to fix it
That’s just a linting error if I recall correctly

I know this is old, and I did manage to fix the issue by adding // ktlint-disable import-ordering but eventually I found a better solution.
If you have command line ktlint installed, you can run the ktlint --android -F terminal command on your android source code folder and it will automatically fix this issue. The --android part is very important, using the ktlint -F command, without the android part, will fix some ktlint issues but not his one.

You can run this command on your android studio terminal. This will automatically reformat all the ktlint issues.
Cmd -
./gradlew ktlintFormat

I ran into a rare case where my android project was using ktlint v0.47.1. Everything passed when I ran the command ./gradlew ktlint locally; and ./gradlew ktlintFormat didn't find anything to change.
However, it kept failling the lint checks with the PR because it was using the latest version v0.48.2 in the Github action setup. There are fixes for importing in v0.48.
I updated ktlint to v0.48.2 in the project in order to find and fix the lexicographic order issues.

Related

Android Studio 3.3.2 use fully qualified name when using "import" action

I'm having some trouble with Android Studio 3.3.2 and Kotlin on Mac. Every time I use the "import" action, the fully qualified name is used in-place instead of the import statement. You can see the behavior in this video:
https://i.imgur.com/Mraz7zs.mp4
I already tried the suggestions given in this other question and they don't seem to work. I also tried reinstalling Android Studio and deleting its configuration, but neither worked.
This behavior for importing generated Data Binding classes occurs when you try to import a class while the project is not yet built. First click on "Rebuild project" and then import the class. Then the import works correctly.

ZXing imports disappearing

I am trying to install zxing in android studio but i am running into some trouble. I have looked around at lots of different posts and tutorials and they have all said to just add the following lines into the build.gradle dependencies
compile 'com.google.zxing:core:3.1.0'
compile 'com.google.zxing:android-integration:3.1.0'
I have done this and synced everything but when I try and add an import like
import com.google.zxing.integration.android.IntentResult;
at the top of my project it instantly disappears without an errors or warning.
Has anyone else had this problem or know what I am doing wrong?
Your IDE will remove the import if you have some auto-cleaning unused import statements. It depends on the IDE to configure this option but both Android Studio and Eclipse have this feature.
I suggest you to start using this IntentResult and then it will keep the import statement.

How do I build a Gideros project for Android with Ant?

That's it, actually. I prefer not to use Eclipse, and want to use the command line.
I've tried to just build it without doing anything special, but it gave me errors like:
package com.giderosmobile.android.player does not exist
cannot find symbol AudioDevice
etc.
Also, if it's possible, I'd like to use another IDE and use the command line for the entire process. Is this possible? If yes, how?
This is my first question here on SO, so please correct me if I'm asking it wrong.
Well first, I noticed that there was no included ant build file, and it was missing several other things. So first things first...
{sdkdir}/tools/android update project --path [locationToProject]
Will generate your build files and everything else.
But it looks like from trying to build this failed because it wants to import location
com.giderosmobile.android.player.*
You might be missing that dependency or the package is named incorrectly...
[Edit]
I swapped this line:
import com.giderosmobile.android.player.*;
with
import com.giderosmobile.android.player.AudioDevice;
import com.giderosmobile.android.player.JavaNativeBridge;
And it built just fine using 'ant debug'. Hope that helps!
[Edit 2]
Move "gideros.jar" into the libs folder

Android Dev - R.layout.main , Variable 'R' cannot be resolved

We are new to Android and we are trying to develop a new application. We installed all the basic setup of Android. We tried to run the Android Sample projects in Eclipse. But while to compile the following lines and a few other lines that uses the variable 'R' throws up an error.
setContentView(R.layout.activity_main);
'R cannot be resolved to a variable'. I do not understand what is causing the error. Anyone help me out in this issue.
check that the import for R class is not
import com.android.R;
and you should make it
import yourpackagename.R;
then recompile
Please try to clean the project, and try compiling again.
If not solved, refer to R cannot be resolved - Android error
If you have 64 bit Linux platform without ia32 libraries, then this problem could also occur. It took me quite some time to pin this one down, seeing that the problem is not clearly linked with the inability to execute 32 bit code.
Just make the import part import com.android.R.layout instead of com.android.R
In my case i forgot to re-install the SDK. When you re-install your operating system, you should re-extract (re-install) your SDK Zip file.
Also check that you have your android libraries mapped correctly in the settings for Eclipse (if you are using eclipse to develop).
Check if your resorce name (for example template.xml) has ONLY small caps letters.
import com.packagename.R* will worl
Change import to
import yourpackagename.R;
If error is still there then
go to gen folder->packagename->R.java
and remove error line some times it gave error on drawable files.
setContentView(R.layout.activity_main)
Change the SDK and delete the project and create new ... it will definitely work!

Disappearing import statement in Android code

I am using "Hello, Android" 1.5& 1.6 with the 2.1 android Java build. I typed:
import android.os.Bundle;
It disappeared as soon as I finished typing!! Help, please.?
By default Eclipse hides all import statements under a single line.
Click the + button next to the import you can see and it should show them all.
Settings -> Editor -> General -> Auto Import.
You will find two options.
1. Add unambiguous imports on the fly.
2. Optimize imports on the fly.
When we type any class or annotation name and if the class is unambiguous and it is still not imported then IDE will automatically add the imports for us if Add unambiguous imports on the fly is selected. If the imports are ambiguous then IDE prompts us which class/interface need to add for import statement.
If "Optimize imports on the fly" option is turned on, when you import something then IDE will check whether it is used in the code or not. If the answer is "No" then IDE optimizes the imports and removes it. For solving this issue uncheck the "Optimize imports on the fly "option.
To learn more check the below links,
Remove unused imports in Android Studio
https://intellij-support.jetbrains.com/hc/en-us/community/posts/206223159-Java-import-hiding-disappearing

Categories

Resources