I updated Android Studio to version 3.2.0.
When I accept any change from list of suggestion or alt + enter.
@androidx.annotation
auto created.@androidx.annotation.Nullable
and android.support.annotation.Nullable
.android.support.annotation
?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
?
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.
To enable annotations in your project, add the support-annotations dependency to your library or app. Any annotations you add then get checked when you run a code inspection or lint task.
The AndroidX library contains the existing support library and also includes the latest Jetpack components. You can continue to use the support library. Historical artifacts (those versioned 27 and earlier, and packaged as android. support. * ) will remain available on Google Maven.
appcompat:appcompat. Official Description: The Support Library is a static library that you can add to your Android application in order to use APIs that are either not available for older platform versions or utility APIs that aren't a part of the framework APIs. Compatible on devices running API 14 or later.
Migrate an existing project using Android Studio With Android Studio 3.2 and higher, you can migrate an existing project to AndroidX by selecting Refactor > Migrate to AndroidX from the menu bar. The refactor command makes use of two flags. By default, both of them are set to true in your gradle.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With