Unused import directive android studio

Kotlin Android Studio Unused Import Directive

Coming from Swift/iOS to android Kotlin and trying to understand why my imports are becoming «unused».

I am using the book «Android Studio Development Essentials». I think the problem is because in the book they want me to create a a «Java Source Language» activity, yet the code in the book is for Kotlin and not Java (I believe a mistake in the book) because none of the statements in the example for the «Java Source Language» activity have the semi-colon «;». So I decided to create a Kotlin activity instead.

Im pretty sure for a «Java Source Language» that the code for the imports should have semi-colons yet in the book they do not.

ActivityA

ActivityB

1 Answer 1

When an import is unused that means the import classes that said import bring to the classpath are not used in the current file/class.

Let’s look at two examples from your Screenshot:

View : In your 1st screenshot, you have a reference to View in the onClick(View view) method.

In your second screenshot, that code is gone, so there’s no need to keep the View import if you’re not going to use it.

Intent : The 1st sshot shows a constructor invocation for Intent ( var i = Intent(. ). ), said invocation is gone in the 2nd sshot; no need to keep the package either.

Keep in mind Kotlin does a lot of magic and syntax sugaring behind and not-so-behind the scenes, so even though you may be using Views and Intents, the fact that you don’t need to type those, means you don’t need the imports either.

If Java (or Kotlin) needs to access those, you will need to import it and you will know when that’s the case because your code will not compile.

Now the last one synthetic. is part of the Kotlin Extensions (if I am not mistaken) and it’s basically referencing your widgets/views from the layout.

In that case, activity_b.xml likely. You no longer reference that in your code, so, import is gone.

I recommend you do a control + alt + o (replace control with command if you’re on macOS, and remember alt is called option on Mac hardware), quite often. It’s an Optimize Imports feature of Android Studio that will clean it up for you 🙂

Источник

Remove unused imports in Android Studio

I recently started Android Studio IDE to make my android apps. I find shortkey to remove unused import in Android Studio is not working ( Ctrl + Shift + O )

Читайте также:  Как разблокировать android с компьютера

What is shortcut key to do same in Android Studio?

12 Answers 12

Simple, right click on your project in Android Studio, then click on the Optimize Imports that should work.

Update

To do same thing which I described above, you can do same just pressing Ctrl + Alt + O , it will optimize imports of your current file and your entire project depends on your selection in a dialog.

You can do it on the fly. You don’t need to call ( Ctrl + Shift + O ) or «Project/Optimize Imports. » each time.

Just set this checkbox in Settings -> Editor -> General -> Auto Import -> Optimize Imports on the fly .

On OSX: Preferences -> Editor -> General -> Auto Import -> Optimize imports on the fly

Press Ctrl + Alt + O .

A dialog box will appear with a few options. You can choose to have the dialog box not appear again in the future if you wish, setting a default behavior.

It is very Simple Just Follow the below step.

  1. Switch your project in Project Mode.

  1. Then right-click on project name.
  2. The final step is to select the Optimize imports from popup menu.

Sorry for the late answer.. For mac users command + option + o Try this.. It is working for me..

Ctrl + Alt + O works pretty well and removes unused imports

there is also an Android Studio (1.4) setting to optimze imports on the fly: see Settings->Editor->General->Auto Import.

On Mac use control + option + O

I think Ctrl + Alt + O works when the import is unused but a valid import. However, say you try to import a class from a package that does not exist or no longer exists (which can happen during refactoring), the shortcut command does not work (atleast it didn’t for me). If you have more than one file like this, things can get problematic.

To solve this problem, click on Analyse -> Inspect code -> (select your module / project). Let it perform the analysis. Go down to Imports -> Unused imports. Click on the «Delete unnecessary import» button that appears on the right.

Press Alt + Enter with the cursor on top of the import. The Optimize imports menu will show. Press Enter again. Your unused imports will be removed.

Источник

Kotlin Android Studio Unused Import Directive

Coming from Swift/iOS to android Kotlin and trying to understand why my imports are becoming “unused”.

I am using the book “Android Studio Development Essentials”. I think the problem is because in the book they want me to create a a “Java Source Language” activity, yet the code in the book is for Kotlin and not Java (I believe a mistake in the book) because none of the statements in the example for the “Java Source Language” activity have the semi-colon “;”. So I decided to create a Kotlin activity instead.

Читайте также:  Speedhack raid shadow legends для андроид

Im pretty sure for a “Java Source Language” that the code for the imports should have semi-colons yet in the book they do not.

ActivityA

ActivityB

Answer

When an import is unused that means the import classes that said import bring to the classpath are not used in the current file/class.

Let’s look at two examples from your Screenshot:

View :
In your 1st screenshot, you have a reference to View in the onClick(View view) method.

In your second screenshot, that code is gone, so there’s no need to keep the View import if you’re not going to use it.

Intent :
The 1st sshot shows a constructor invocation for Intent ( var i = Intent(. ). ), said invocation is gone in the 2nd sshot; no need to keep the package either.

Keep in mind Kotlin does a lot of magic and syntax sugaring behind and not-so-behind the scenes, so even though you may be using Views and Intents, the fact that you don’t need to type those, means you don’t need the imports either.

If Java (or Kotlin) needs to access those, you will need to import it and you will know when that’s the case because your code will not compile.

Now the last one synthetic. is part of the Kotlin Extensions (if I am not mistaken) and it’s basically referencing your widgets/views from the layout.

In that case, activity_b.xml likely. You no longer reference that in your code, so, import is gone.

I recommend you do a control + alt + o (replace control with command if you’re on macOS, and remember alt is called option on Mac hardware), quite often. It’s an Optimize Imports feature of Android Studio that will clean it up for you 🙂

Источник

Android Studio not recognizing import statements

I have a multitude of problems stemming from one central issue: Android Studio isn’t recognizing imports.

For example, in my class «ActivityAbout» I have the following import statements:

However, in the IDE (using the first import statement as an example), the import android.content. is appearing grayed out, and when I hover over it, it says «unused import statement» the Intent part is appearing red, and when hovering over that, it says «cannot resolve symbol Intent.» This occurs for each of my import statements, with the first part being grayed out, and the last part being in red.

Now, this provides a multitude of issues in my code, as anything relating to those import statements appears red and when hovering over it, I get the message «cannot resolve symbol ____»

Читайте также:  Free apk mod android

Here’s the kicker- my app still compiles and runs upon opening it on my phone.

I suppose my main issue is that it is incredibly annoying, and auto imports doesn’t work either. (I have checked off ‘show import popup’ in the settings, which is the answer I found for other people having issues with auto import not working). My other concern is that this would make my app ineligible for release on the play store. I’m still looking into the publishing process, so I don’t know if that is actually an issue or not.

My question (if no one has an answer for how to fix this) is: is this a problem with the IDE itself or the code?

Источник

import kotlinx.android.synthetic.main.activity_main is not working

Import kotlinx greyed out

I think i try nearly everything. Reinstall Android Studio, Invalide Cache, new Project same Problem.

i just can’t find the Solution

14 Answers 14

Check «build.gradle(:app)» file,

if kotlin extension is missing, add kotlin-android-extensions as shown below and click on «Sync now»

  • File | Invalidate Caches / Restart
  • Deleting .idea folder
  • Clean
  • Re-import the project

OR just remove apply plugin: ‘kotlin-android-extensions’ , sync gradle plugin and then I added it again.

Here is a step by step answer:

  • From right side of the Android studio click on Gradle
  • Right click on the app and click Open Gradle Config
  • New source opening in plugins part and then add this:

Result: now you can import kotlinx.android.synthetic.main.activity_main.*

Synthetics are now deprecated from Google. Try to avoid using them as it might lead to null pointer exceptions and unexpected behaviour on your app.

In build.gradle (:app) , add:

To set listeners:

Just add below line in your build.gradle(Module:YourProjectName.app) inside the plugins section on top:

Mostly first two lines are already there just need to add 3rd one and sync project

For me it was just adding the apply plugin: ‘kotlin-android-extensions’ to app’s build.gradle, press sync gradle files and i was able to get synthetics

Kotlin Android Extensions is depreciated. Migrate to Jetpack view binding. See below: https://developer.android.com/topic/libraries/view-binding/migration

Simple friend, you forgot the asterisk.

It just happened to me.

remove plugins and added them two of them. id ‘kotlin-android-extensions’ id ‘kotlin-android’

should be added. restart the project.

So the problem as I have found is in gradle plugins, then you need to restart, rebuild your project.

Hope this help. maybe is related with the new way to get views from layouts

Источник

Оцените статью