When focusing on development, the impact of your IDE’s auto-completion feature is crucial. It significantly boosts productivity by reducing the need to type out full sentences, allowing you to code faster and more efficiently. The auto-completion minimizes errors and helps streamline the development process by predicting and completing code as you type, which saves time and reduces cognitive load during development.
However, you’ve probably encountered situations where auto-completion in your IDE imports the wrong package that you don’t want at that moment or suggests too many unrelated packages with similar class or function names. This can slow you down and cause you to freeze as you spend time sorting through these options to choose the correct one, ultimately interrupting your development flow.
In this article, you’ll explore how to selectively disable auto-completion suggestions in Android Studio (IntelliJ IDE), a featured topic on Dove Letter this week. Dove Letter is a subscription repository where you can learn, discuss, and share new insights about Android and Kotlin. If you’re interested in joining, be sure to check out “Learn Kotlin and Android With Dove Letter.”
Auto-Completion Trap in Our Life
If you’re using Jetpack Compose, you may have encountered a situation where everything seems fine, yet something feels off.
For instance, you might have imported the Modifier object as instructed or out of habit, only to find that it suddenly doesn’t work. If you realize and fix the issue within minutes, that’s a lucky outcome. However, if you spend more than 10 minutes troubleshooting, it becomes a frustrating scenario.
So what’s the issue? You likely imported the wrong package due to an auto-completion suggestion, as shown in the image below, and ended up getting stuck. This is a common trap in development, where auto-completion can mistakenly suggest a similar but incorrect package, leading to confusion and wasted time.
Another common situation you’ve probably encountered, especially if you’ve worked with Kotlin’s Flow, is when the IDE presents you with multiple package suggestions, as shown in the image below. The problem? The Flow
class you need is often buried in the last option, forcing you to scroll through irrelevant choices. This can slow down your workflow and lead to mistakes if you select the wrong package.
Exclude a Class or Package From Completion
There’s an easy way to prevent your IDE from suggesting unrelated packages. This can be done directly within the IDE with just a few steps. If you want to stop seeing suggestions from a specific package, follow this simple method: when the auto-completion suggestion appears in the editor, press Opt + Enter (or Alt + Enter on Windows). The IntelliJ IDE will then ask if you’d like to exclude suggestions from that package, as shown in the image below.
If you select the option to “Exclude ‘java.lang.reflect.Modifier’ from completion,” you’ll see a popup menu confirming the exclusion, as shown in the image below. This will prevent the IDE from suggesting that package in future auto-completion prompts, helping you avoid irrelevant suggestions and streamline your coding experience.
You can also define the scope of the exclusion as follows:
- IDE Scope: This option applies the exclusion across the entire IDE. No matter what project you work on, the IDE will no longer suggest the excluded package. Be cautious with this option, as it might lead to issues in future projects that require different libraries. If the proper package is excluded, it could cause another disaster.
- Project Scope: This is generally the better option for excluding specific packages. The advantage of using project scope is that the settings are project-specific and can be backed up. This ensures that, even if you update your IDE to a new version (e.g., from Android Studio Koala to Ladybug), your exclusion settings will remain intact for that project, unlike IDE scope, which could be lost during immigration.
Job Offers
Configure codeInsightSettings.xml
If you’ve chosen the project scope option to exclude specific packages, you may notice that a file named codeInsightSettings.xml
is generated under the .idea
folder. If not, you can create one. This file contains the excluded packages for your project.
If you open the file, you’ll see entries similar to the example below detailing the packages you’ve excluded from completion suggestions. This allows the settings to remain project-specific and portable, ensuring the same exclusions apply whenever the project is opened, regardless of IDE updates.
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="JavaProjectCodeInsightSettings"> | |
<excluded-names> | |
<name>java.lang.reflect.Modifier</name> | |
</excluded-names> | |
</component> | |
</project> |
If you’re using Jetpack Compose, Kotlin’s Flow, and Dagger or Hilt, you can simplify your development process by copying the codeInsightSettings.xml
file provided below and pasting it into the ./idea/codeInsightSettings.xml
directory of your project. This will prevent unnecessary or irrelevant suggestions from popping up during auto-completion, helping you stay focused and avoid distractions.
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="JavaProjectCodeInsightSettings"> | |
<excluded-names> | |
<name>android.graphics.Canvas</name> | |
<name>android.graphics.Color</name> | |
<name>android.graphics.Paint</name> | |
<name>android.graphics.drawable.Icon</name> | |
<name>android.inputmethodservice.Keyboard.Row</name> | |
<name>android.text.Layout.Alignment</name> | |
<name>android.view.Surface</name> | |
<name>android.widget.Button</name> | |
<name>android.widget.GridLayout.Alignment</name> | |
<name>androidx.core.content.pm.ShortcutInfoCompat.Surface</name> | |
<name>java.lang.reflect.Modifier</name> | |
<name>java.nio.file.WatchEvent.Modifier</name> | |
<name>java.time.format.TextStyle</name> | |
<name>javax.annotation.concurrent.Immutable</name> | |
<name>org.threeten.bp.format.TextStyle</name> | |
<name>org.w3c.dom.Text</name> | |
<name>java.util.concurrent.Flow</name> | |
<name>org.intellij.lang.annotations.Flow</name> | |
<name>jakarta.inject.Inject</name> | |
</excluded-names> | |
</component> | |
</project> |
Conclusion
You’ve learned how to prevent unnecessary or irrelevant auto-completion suggestions in Android Studio (IntelliJ IDE), which can enhance your development efficiency. While your project may function perfectly without these IDE optimizations, small improvements in workflow can add up over time, leading to significant productivity gains in the long run. Developing habits that streamline your coding environment can ultimately have a large impact on your overall performance.
If you have any questions or feedback on this article, you can find the author on Twitter @github_skydoves or GitHub. If you’d like to stay updated with the latest information through articles and references, tips with code samples that demonstrate best practices, and news about the overall Android/Kotlin ecosystem, check out ‘Learn Kotlin and Android With Dove Letter’.
As always, happy coding
This article is previously published on proandroiddev.com