Android Studio adds both #androidx.annotation and #android.support.annotation problem - android

I updated Android Studio to version 3.2.0.
When I accept any change from list of suggestion or alt + enter.
Problem is #androidx.annotation auto created.
Thus suggestions method have two annotation #androidx.annotation.Nullable and android.support.annotation.Nullable.
I don't want to manually remove this unwanted suggestion, so what can I do to have only android.support.annotation?
Also androidx.annotation package is not added in my app. and I don't want to add androidx.annotation.
See example.
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
public class LoginActivity extends AppCompatActivity {
#Override
public void onCreate(#androidx.annotation.Nullable #Nullable Bundle savedInstanceState, #androidx.annotation.Nullable #Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
}
}
Is there some IDE setting to remove auto generation of androidx.annotation?
Update
I could get rid of this problem with migrating to androidx (from official doc), but I am not able to migrate all projects to androidx currently. So need a solution.

AndroidX is the new extension libraries for backward compatibility support. In future new feature backward compatibility support will be addressed in AnddroidX. As stated in this blog https://android-developers.googleblog.com/2018/05/hello-world-androidx.html
The stable release of 28.0.0 will be the final feature release packaged as >android.support. All subsequent feature releases will only be made available as >androidx-packaged artifacts.
https://developer.android.com/topic/libraries/support-library/revisions
Revision 28.0.0 Production
(September 21, 2018)
This is the stable release of Support Library 28.0.0 and is suitable for use >in production. This will be the last feature release under the >android.support packaging, and developers are encouraged to migrate to >AndroidX.
Moving your app from android.support to androidx-packaged dependencies
Refer to this link, https://developer.android.com/jetpack/androidx/migrate
If you depend on a library that references the older Support Library, Android Studio will update that library to reference androidx instead via dependency translation. Dependency translation is automatically applied by the Android Gradle Plugin 3.2.0-alpha14, which rewrites bytecode and resources of JAR and AAR dependencies (and transitive dependencies) to reference the new androidx-packaged classes and artifacts. We will also provide a standalone translation tool as a JAR.
So, In Step 1(dependency translation): In your gradle.properties file set the android.useAndroidX flag to true and the android.enableJetifier flag to true.
android.useAndroidX=true
android.enableJetifier=true
in Step 2(source refactoring): With Android Studio 3.2 and higher, you can quickly migrate an existing project to use AndroidX by selecting Refactor > Migrate to AndroidX from the menu bar.

Just add annotationProcessor 'androidx.annotation:annotation:1.1.0' as a dependency in your build.gradle(Module: app) file.

Android Studio has a preference option "Exclude from import and completion" that you could use to suppress the suggestion of androidx packages:
In this screenshot, for example, I've added the java.time package to this list because I want my autocomplete to suggest org.threeten.bp.LocalDate but never java.time.LocalDate.
You should be able to add androidx.annotation to this list to solve your problem.

Android Studio can only import files which are included in your classpath. This includes Androidx libraries - thankfully, if the classes are not present, Studio won't suggest them (which is why the rest of us don't see this issue in our projects).
That being said, the implication here is that you have updated your gradle file to include the package containing androidx.annotation.NonNull
com.android.support:support-annotations became androidx.annotation:annotation:1.0.0 according to the migration guide, so you can look for this group in your module Gradle file.
The simplest fix is to remove that package from Gradle, then Clean and Rebuild.
Note: If you want to stick with Support annotations then the latest and greatest version is 28 as mentioned here

Using Android Studio 3.3.1, I was able to replicate the problem. My project had just been created, with no AndroidX dependency. When I created the first fragment and overrode onCreateView(), the 2 annotations #androidx.annotation.Nullable and #Nullable were automatically added by Android Studio to the method (no prompt given with a possible choice of import). The only annotation import automatically added was import android.support.annotation.Nullable;
If you do not wish to migrate to AndroidX, here is a fix that worked for me:
Manually delete all the #androidx.annotation.Nullable annotation(s), leaving only the original #Nullable annotation(s).
Do File > Invalidate Caches / Restart
When a new Fragment will be created in the app, only the original Support Library's #Nullable annotation will be added.

Using Android Studio Arctic Fox (equivalent to version 4.3). I did not migrate to AndroidX for my project. What worked for me is adding
implementation 'androidx.annotation:annotation:1.1.0'
to the app-level build.gradle.

As stated on the support library release note.
This is the stable release of Support Library 28.0.0 and is suitable
for use in production. This will be the last feature release under the
android.support packaging, and developers are encouraged to migrate to
AndroidX.
You could still use support's class path. (remember to clean and build)
But highly recommend you to migrate to AndroidX as 28.0.0 of support is the last release.
(remember, NOT BOTH)

I will start by saying I am a very novice slapstick hobbyist, but this same issue got me stuck and its been resolved for some time and I could not find a solution.
Everyone talks about adding dependencies to your build.griddle files, and if that works great, for me it never took until I changed the actual import listings (API 28) and changing them from...
import android.support.annotation.Nullable;
-to-
import androidx.annotation.Nullable;
I had to do a lot of improvisation to find the new directories because they didn't seem to be listed anywhere I could easily find.
This was also the case for Fragment, FragmentManager, DialogueFragment, and many others. I hope this helps someone suffering like I was!

Since this problem has emerged for me after updating to Android studio 4.2.1 (or maybe 4.2?), I am adding the way to fix it here, for anyone who stumbles upon this question, as I did.
I started getting double nullability annotations, as described in this question, recently.
To fix it, do the following:
Go to Android studio preferences
Go to Editor > Inspections
Go to Java > Probable bugs > Nullability problems > #NotNull/#Nullable problems
Press the "Configure Annotations" button
Select "androidx.annotation.Nullable" in the top box, and press the checkmark button below the box, titled "Select annotation used for code generation"
Do the same for "androidx.annotation.NonNull" in the bottom box.
Press OK
Android studio should now only use the androidx annotations when generating code.

Related

can't import android.support.v4.app notificationcompat in android studio?

I was learning android notification tutorial using this link but when I started using
Notification notification = new NotificationCompat.Builder(this, App.CHANNEL_1_ID);
this code, I can't import android.support.v4.app. NotificationCompat , how to do that?
It only imports
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.app.Notification;
Do I need to add something in build.gradle?
I am using Android Studio 2021.1.1, is that the problem?
androidx is new support library for the replacement v4.support library. And androidx is recommended. I suggest you to use search filter to find tutorial later than 2020~2021.
However you can still use v4.support library by checking "Use legacy android.support library" option while creating a project.
While this is not enough, because latest build tools will not support deprecated libraries, you have to keep compleSdkVersion 28 or below. You are also required to downgrade your Android Gradle Plugin to 3.6.4 and gradle wrapper to 6.0
Make sure these lines are not present in gradle.properties
android.useAndroidX=true
android.enableJetifier=true
Now you can follow the above tutorial.
Use
android.arch.core:core
android.arch.core:common
instead of
androidx.arch.core:core-common
androidx.arch.core:core
Here is the full list of equivalent artifacts of support library to androidx
I do not recommend you to follow above tutorial find latest tutorial. Tech is rapidly changing, following an outdated tutorial might slow your learning process and keep you behind.

Suddenly after implementing firebase many import statements becomes unused and can't resolve many symbols

I'm new to Android, Was learning from youtube from following playlist
https://www.youtube.com/watch?v=d1T0ptCAs8A&list=PLraJmOvF9eeGyVClKdBaVU6O_XqQjNboS&index=5
In 4th video it was teaching to add Firebase, but it was old method as I got some new option on Firebase(Firebase was saying It needs only classpath 'com.google.gms:google-services:4.2.0')
suddenly after adding this line in build.graddel in project I saw that my previously working code have some errors
I tried Invalidate cashes and restart, cleaned projects and rebuild.
It seems you are mixing support library and androidx
either use androidx(recommended) or support library
follow https://developer.android.com/jetpack/androidx/migrate guide and after this make sure to make appropriate changes in layout xml files also.
Remove the dependencies with androidx starting. Because androidx is a different library than support library. You should only include one of them.

Does AndroidX need be supported explicitly

I am learning flutter, and while playing around with flutter's Geolocator 3.0.0 package. I came across an issue that wouldn't let me compile the app as soon as I add the reference to this package in pubspec.yaml file. A quick google search showed me the right github issue. The fix mentioned in the github issue (migrating app to support AndroidX - by updating compileSdkVersion in build.gradle file to 28, and adding android.useAndroidX=true and android.enableJetifier=true in gradle.properties) does work.
Main question: I am not sure if this (migrating project to support AndroidX) will need to be done for all flutter projects explicitly or is there a way to ensure all new flutter projects support AndroidX.
PS:I come from C# background, and I know nearly nothing about android / iOS app development.
You need to explicitly migrate a Flutter app to Android X by using Migrate to AndroidX... button located under Refactor tab of Android Studio. Manual migration is not recommended. If a plugin requires Android X, you need to perform this migration.
For the related article on Flutter.io, see: https://flutter.io/docs/development/packages-and-plugins/androidx-compatibility
Re your main question:
' I am not sure if this (migrating project to support AndroidX) will need to be done for all flutter explicitly or is there a way to ensure all new flutter projects support AndroidX'
I have never found the 'Refactor/Migrate to AndroidX' to work. Not even on new projects. I'm not sure it does anything at all. I always had to do manually even after just creating a new project. I follow the manual instructions but also delete any old versions of gradle in c:/....gradle/wrapper/dists/gradle-???. The current one, and only one to have available is gradle-5.1.1-all.

Flutter Android Support Library Not Recognized

I am writing a Flutter wallpaper app and am calling Android Specific code and I need to ask the user for some permissions, I need to import ContextCompat for that but there is not option to import in the quick menu, so I read online and manually did it.
import android.support.v4.content.ContextCompat
The problem is that Android Studio can't resolve the support symbol, I also read some forums online about that as well. The most common answer I found was to include add the android support library in the Gradle, which I have already tried but did not work.
Any help would be dearly appreciated.
com.android.support:appcompat-v7 should automatically take care of the dependency of ContextCompat
e.g. adding following in the dependency -
implementation 'com.android.support:appcompat-v7:26.1.0'
Here the version 26.1.0 would depend on your compile version.

The import com.google.android.gms.ads cannot be resolved

I am getting error when i want to import google-play-service in Eclipse.
i have import google-play-service-project-lib in Eclipse
i have read all possible Q&A but nothing is work.
I have it added as Library and JAR manually
Then I have tried to clean my project ...
Is there anybody who can solve this problem?
You can use this way. First of all close projects which are using google-play-services library. Whenever your mouse over the com.google.android.gms import that can not be resolved and towards the bottom of the popup menu, select the "Fix project setup" option as below. Then it'll prompt to import the google-play-services library.
Firstly, try to download latest version of Google Play Services, then:
Remove the existing library to add again manually,
Go to your java build path,
Under project tab add your library and then,
Under order & export tab tick your library,
Apply settings.
After all, just clean the project and library, it should work.
By the way, you can also check this question.
To me it was just a matter of updating the dependencies.
dependencies {
implementation 'com.google.android.gms:play-services-ads:20.6.0'
}
I concluded that if it's already on the latest version, refresh the Gradle may solve the issue - or, as already recommended by Umit Kaya, remove the existing library and add it back.

Categories

Resources