24

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.

Community
  • 1
  • 1
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • try adding `android.useAndroidX=false` & `android.enableJetifier=false` in **gradle.properties** file in your project. – Jeel Vankhede Sep 25 '18 at 11:48
  • 1
    @JeelVankhede You can see document - `The flag is false by default if it is not specified.`, so this is unnecessary lines. Any way, I tried this also. – Khemraj Sharma Sep 25 '18 at 11:51
  • Its not about each file bro, I am getting this just when I am coding now. when I am accepting changes. Like `onCreate()` and any suggestion. You can try this too in your updated Android Studio. – Khemraj Sharma Sep 25 '18 at 11:55
  • @Khemraj sorry I misunderstood , but I am not getting `@androidx.annotation.Nullable` in my android studio its 3.2.0 too – Manohar Sep 25 '18 at 12:00
  • 1
    @Redman Enjoy the party :D – Khemraj Sharma Sep 25 '18 at 12:09
  • 1
    @Mohsen Thanks for help but I don't know how this is related to my question. – Khemraj Sharma Oct 02 '18 at 10:03
  • I suggested that because of : `Is there some IDE setting to remove auto generation of androidx.annotation?` But the point is, if `@androidx.annotation` automatically creates, then this is because you're actually already migrated to `AndroidX` and that's why this generates automatically. And in your codes, you actually have **two different annotations** from **support annotations** and **androidx**. Please clarify me If i'm not getting your point. – ʍѳђઽ૯ท Oct 02 '18 at 10:09
  • 1
    @Mohsen 1. I did not migrate to androidx, I just updated AS. 2. I tried this setting as well, there is no change. 3. My project does not have dependency for androidx annotations. – Khemraj Sharma Oct 02 '18 at 10:23
  • 1
    @Mohsen If you want see this issue, just update your Android Studio, and create new project, then type oncreate() and accept suggestion. – Khemraj Sharma Oct 02 '18 at 10:23
  • Just did what you said. It did not created another `@androidx.annotation` And there was just `android.support.annotation`. You probably should report the issue. That's a bug. However, I'm alreading using v3.2 and it's a little bit buggy so far. – ʍѳђઽ૯ท Oct 02 '18 at 10:30
  • 1
    @Mohsen I just tried with fresh project again. Its happens with me , yes it is bug of IDE. I will report this issue. I wanted know if there is a quick workaround for this issue. – Khemraj Sharma Oct 02 '18 at 10:40
  • @Mohsen I'd like to hear your explanation how you think braces and wrapping are related to annotation imports – Nick Cardoso Oct 05 '18 at 14:41
  • @NickCardoso Didn't get the OP question at first. Anyways Remove that part. Thanks – ʍѳђઽ૯ท Oct 05 '18 at 14:51

9 Answers9

13

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.

Refactor to Androidx from menu

Community
  • 1
  • 1
K.Anam
  • 141
  • 1
  • 5
  • 3
    Why do I get an error: **Cannot find a version of 'androidx.annotation:annotation' that satisfies the version constraints** – IgorGanapolsky Dec 30 '18 at 15:37
  • 2
    [Migrating to AndroidX](https://developer.android.com/jetpack/androidx/migrate) in this page under **Artifact mappings** search for **Old build artifact** for example in your case, **'com.android.support:support-annotations'**. then replace with respecting **AndroidX build artifact** here it is, androidx.annotation:annotation:1.0.0 in your app gradle dependencies **implementation 'androidx.annotation:annotation:1.0.0'** – K.Anam Jan 10 '19 at 10:17
  • How do it without Android studio? – red-devil Nov 04 '19 at 23:28
8

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

Itiel Maimon
  • 834
  • 2
  • 9
  • 26
5

Android Studio has a preference option "Exclude from import and completion" that you could use to suppress the suggestion of androidx packages:

enter image description here

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.

Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • 3
    It is still same bro. I excluded `androidx.annotation` and when I type some method, and override it from suggestion, it adds both `androidx.annotation` and `android.support.annotation`. – Khemraj Sharma Oct 08 '18 at 09:22
4

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

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
1

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.

ellem
  • 47
  • 1
  • 1
  • 6
1

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.

lollalolla
  • 454
  • 5
  • 10
0

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)

Fung
  • 961
  • 1
  • 8
  • 17
0

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!

0

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:

  1. Go to Android studio preferences
  2. Go to Editor > Inspections
  3. Go to Java > Probable bugs > Nullability problems > @NotNull/@Nullable problems
  4. Press the "Configure Annotations" button
  5. Select "androidx.annotation.Nullable" in the top box, and press the checkmark button below the box, titled "Select annotation used for code generation"
  6. Do the same for "androidx.annotation.NonNull" in the bottom box.
  7. Press OK

Android studio should now only use the androidx annotations when generating code.

mhswtf
  • 165
  • 1
  • 7