Android studio is making used import statement as unused? - android

I have just starting working my android studio project in another pc with same android studio version and it automatically makes used import statement as unused in the code and mark evey code related to particular import statement red. How can i fix this issue , its very annoying. but when i run the project ,it works fine without any error.
I have tried all those tricks including deleting .ide ,.gradle and even restarting with invalidate cash ,restard and disabling automatic import as well.
In the below screenshot , android studio just grayout all import statment i am using below and mark all those classes and its related method red in the code. how can i fix this issue

MyAppObserver is unused import because you import it in the same file it's declared. As for others I am not sure because I can't see full file structure.

Your interface of the same name is causing a conflict.
interface MyAppObserver
Rename your interface and the imports should work correctly, and the red marks should go away.

Try to clean then rebuild project.
If this still exists then close Android studio then re-launch it again.
sometimes I face the same situation but it got fixed after clean and rebuild

This happens sometimes. You will need to clean the project. If that doesn't work try invalidate caches/restart.
First, go to File, then choose: Invalidate Caches/Restart...
Then this dialog pops up, choose Invalidate and Restart.
Most of the times when IDE doesn't behave as it should, you can try this.

You don't need to import the interface as it is inside the same file.
You can make a separate file for the interface.

Related

HiltAndroidApp/HiltViewModel "Unresolved Reference" in IDE but it stills builds without error

So I followed the steps from https://developer.android.com/training/dependency-injection/hilt-android to add Dagger Hilt to my project.
It builds without error, but Android Studio shows errors for #HiltAndroidApp and also for #HiltViewModel, but some of them such as #AndroidEntryPoint are there
I have tried invalidate caches/restart
I have tried deleting .idea, .gradle.
I have tried opening an older project which was working with Hilt, and now that one also shows this error (and also builds without any problem)
I have tried using the latest version of Hilt available, 2.39.1, and also with 2.38.1 as it's what's used on the developer.android.com hilt documentation page
I'm out of ideas
I faced the same issue with #Inject. I tried invalidating cache and restart and everything but nothing worked.
The import path for #Inject was javax.inject.Inject.
When I tried to go to the "inject" package by hovering over "inject" from the import statement and CTRL + clicking it, it took me to some gradle cache folder and there was an interface named "Inject" inside it, so ideally the interface was there but android studio wasn't detecting it.
Then I went through some other folders inside the gradle cache with long random names and stuff, I found another package with the same structure (javax.inject) but it was empty. I deleted that empty package manually and the issue was fixed automatically. So I guess all this time android was considering the package with nothing inside it (that I deleted) and not the other one which had the Inject interface from cache. Deleting the empty package from cache fixed it for me.
I tried to recreate the issue to answer it with screenshots and stuff but I am not able to recreate it now. Sorry about that.

My android studio no longer formats code or predicts which classes I need imported. How can I fix this?

Previously, when a class was not imported, Android suggested I import them AND used to give resources a special format as in "R.id.aLayout". It has suddenly stopped and I am completely stuck, importing all these classes manually will be a real pain.
Please check if Power Save Mode is on or off in Android Studio .
If it is on turn it off.
Open File > Invalidate Caches/Restart & click on Invalidate and Restart
Note: Save your project before doing this.
Cheers :)

Errors when importing someone else's projects - Android/Eclipse

I have been using Eclipse for about 6 months now, and I noticed that I rarely can import someone else's project into Eclipse. I get so many errors in many files. For example, I tried to import a friend's project that I know works just fine, but I get a lot of "R" errors, like Eclipse can't recognize "R" as a field, and then other weird ones like ActionBarActivity is not recognized or an entire imports like android.support.v4.app.Fragment that visually are in the library but are not being recognized by Eclipse as being there, and I check them and they are definitely marked "Is library" and added properly to the project. Anyone know why this is or how to fix? It's frustrating because to learn, sometimes you need to see someone else's app run first, then look at their code, but you can't. Thanks in advance.
My usual way of importing a project is to do File -> Import -> General -> Existing Projects into Workspace. Also, I have tried cleaning the project, but it doesn't work.
try this File -> Import -> Android -> Existing Android Code into WorkSpace ->then browse-> select your project->make sure the checkBox"copy the project into workSpace" is marked
Check your Build Path. In the Android Section, try using some other API level click on Apply and after your project finishes with the building workspace, again revert back to the original API and click on OK.
Also check for any Build Path errors.
So thanks to the help of everyone of this post, and others on another post, the problem was finally fixed and took several steps.
Turns out that the ActionBarActivity error had a quick fix (hover over it, you get quick fix options) of importing the appcompat_v7 library. I never chose that before, because I thought it was already installed, hence the conflict with android-support-v4.jar (meaning there were doubles of that library), but I chose to import it anyway and that error went away.
But I discovered that android-support-v4.jar was actually a component of appcompat_v7, so that was causing the "Jar mismatch!" error in the console. So I deleted android-support-v4.jar from the libs folder, and the jar mismatch went away.
Lastly, my layout files were all not being recognized, even though I knew the variable names were all correct. So the error was from when I tried to refresh my imports in the very beginning of the problem (using shift+command+O), thinking it was an import problem (which it was), but at that time, refreshing did nothing. BUT... Eclipse took away import com.laurengariepy.android.gridimagesearch.R; and replaced it with android.R;, which is what was causing all the layout errors. So once I put that com.laurengariepy.android.gridimagesearch.R; back, then all errors went away.
So the take-away to this story, is be careful with Eclipse version 22.6. The auto-import of the appcompat_v7 library will cause a lot of trouble when you try to import other people's projects, because they were likely not created in an IDE with appcompat_v7 already there.

The import com.example.myfirstapp.R can not be resolved

I have been working my way through the Tab menu tutorial and have come across multiple errors. After doing some research on here I found the common solution was to change the Import android.R to import com.example.myfirstapp.R.
This is giving me the error that this file cannot be resolved. Can anyone tell me why I can't import this file? I have a hunch from researching on here that eclipse has not built the file, but I have no idea how to build it as I am new to coding as this is just a hunch and may be completely wrong.
package com.example.myfirstapp;
[Edit}
i have removed the "com.example.myfirstapp.R" and have CMD-SIFT-O to restore the imports but i still have the following errors.
You don't need the com.example.myfirstapp.R unless it is in a library. If it is in com.example.myfirstapp, remove it.
Also, trying cleaning your project and rebuilding it.
Make sure you don't have any files, pictures, etc in your drawable or assets folder with capital letters, spaces, or dashes. Just use plane old lowercase. Also, try to clean your project. Go to project > clean. Then restart eclipse. Hope this helps. Also eclipse won't reconize any changes in XML files without saving first. Make sure to save after you add a button, ect.

Import statements are missing in android eclipse and throws "project contains error(s), please fix it before running it" error

Yesterday I have created a simple android application using eclipse (ADT bundle) and when I run it, it works fine. Today, when I try to open it, it shows errors
Your project contains error(s), please fix it before running it
, and
cannot resolved variable.
After I go through my android application project, I found out that, all the import statement were gone. So I manually key in all the import statement and it works fine. This is second time I have encountered this kind of problem.Does anyone know why when I close the project and reopen it on next day, all the import statements are missing?
That shouldn't be the case usually. You can try the auto import of the library needed instead of manually inserting the library one by one by ctrl+shift+O. Another common problem is that you included the library but it still cannot reference it correctly. Something like cannot reference R.id.xxx, that can be solved by making sure that you don't have import android.R in your code.
Hope that helps :)

Categories

Resources