Nowadays we commonly get this type of error in gradle when we use third party library :
we get this types of error when library is updated and providing use of AndroidX but we are using old one.
To solve this error we have to migrate our project into AndroidX .if you still don't want to use AndroidX use have to do lots of stuff and also downgrade your googlePlayServices and firebase version.
So in this Article we will discuss about what is AndroidX, How to Migrate to AndroidX and when to use AndroidX?
What is AndroidX:
When to use AndroidX:
The support library artifacts are being deprecated and all future development is going into AndroidX, so there's no avoiding this migration. Use AndroidX in your project as soon as possible to use latest features.
Manifest merger failed : Attribute application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0-rc02] AndroidManifest.xml:22:18-91
is also present at [androidx.core:core:1.0.0-rc01] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory). Suggestion: add 'tools:replace="android:appComponentFactory"' to <application> element at AndroidManifest.xml:5:5-19:19 to override.
we get this types of error when library is updated and providing use of AndroidX but we are using old one.
To solve this error we have to migrate our project into AndroidX .if you still don't want to use AndroidX use have to do lots of stuff and also downgrade your googlePlayServices and firebase version.
So in this Article we will discuss about what is AndroidX, How to Migrate to AndroidX and when to use AndroidX?
What is AndroidX:
- The new Android extension libraries (AndroidX) which represents a new era for the Support Library.Starting with the AndroidX refactor, library versions have been reset from 28.0.0 to 1.0.0.
- AndroidX is a major improvement to the original Android Support Library, which is no longer maintained. androidx packages fully replace the Support Library by providing feature parity and new libraries.
- Artifacts within the androidx namespace comprise the Android Jetpack libraries. Like the Support Library, libraries in theandroidx namespace ship separately from the Android platform and provide backward compatibility across Android releases.
Android Jetpack:
Jetpack is a suite of libraries, tools, and guidance to help developers write high-quality apps easier. These components help you follow best practices, free you from writing boilerplate code, and simplify complex tasks, so you can focus on the code you care about.
Jetpack comprises the androidx.* package libraries, unbundled from the platform APIs. This means that it offers backward compatibility and is updated more frequently than the Android platform, making sure you always have access to the latest and greatest versions of the Jetpack components.
Google has developed the Support Library for 7 years, a ton of developers are using it. As in every Android project you build on Android Studio, you must have seen these support libraries in your build.gradle file starting with com.android.support.
com.android.support:appcompat-v7
com.android.support:recyclerview-v7
But after AndroidX came along, these libraries have a starting package like androidx. instead of com.android.support.
androidx.appcompat:appcompat
androidx.recyclerview:recyclerview
Why use AndroidX:
- Unlike the Support Library, androidx packages are separately maintained and updated. The androidx packages use strict Semantic Versioning, starting with version 1.0.0. You can update AndroidX libraries in your project independently.
- Version 28.0.0 is the last release of the Support Library. There will be no more android.support library releases. All new feature development will be in the androidx namespace.
- Most of third party libraries uses AndroidX so Migrate to AndroidX is good for now.
How to Migrate into AndroidX:
If you want to use androidx-namespaced libraries in a new project, you need to set the compile SDK to Android 9.0 (API level 28) or higher and set both of the following Android Gradle plugin flags to true in your gradle.properties file.
android.useAndroidX:
When this flag is set to true, the Android plugin uses the appropriate AndroidX library instead of a Support Library. The flag is false by default if it is not specified.
android.enableJetifier:
When this flag is set to true, the Android plugin automatically migrates existing third-party libraries to use AndroidX dependencies by rewriting their binaries. The flag is false by default if it is not specified.
Note: AndroidX replaces the original support library APIs with packages in the androidx namespace. Only the package and Maven artifact names changed; class, method, and field names did not change.
Prerequisites:
- Android studio version should be Higher than 3.2
- TargetSDK verion and compliedSDK version should be 28 or greater
- If your android studio version is 3.2.x you have to use gradle build version 3.2.1. If you are using android studio version 3.3.1 then your gradle build version should also be 3.3.1.
Now lets move in to the migration process:
- Click Refactor from menu options in Android studio.
- Click Migrate to Android X from Refactor.
3. It will ask us to create a backup zip file and proceed with backup process. Click migrate.
4. Now this will show the refactor window with the list of all the files using android support libraries. Just click Do Refactor.
Most common replacements are:
android.support.v7.widget.RecyclerView→androidx.recyclerview.widget.RecyclerView
androidx.constraintlayout.ConstraintLayout → androidx.constraintlayout.widget.ConstraintLayout
android.support.constraint.ConstraintLayout → androidx.constraintlayout.widget.ConstraintLayout
sometimes this error might be faced if you using ButterKnife library
The given artifact contains a string literal with a package reference ‘android.support.v4.content’ that cannot be safely rewritten. Libraries using reflection such as annotation processors need to be updated manually to add support for androidx.
Solution:
Updating butterknife version to at-least 10.0.0 solved the issue
dependencies {
...
implementation 'com.jakewharton:butterknife:10.0.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'
}
Updating butterknife version to at-least 10.0.0 solved the issue
dependencies {
...
implementation 'com.jakewharton:butterknife:10.0.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'
}
When to use AndroidX:
The support library artifacts are being deprecated and all future development is going into AndroidX, so there's no avoiding this migration. Use AndroidX in your project as soon as possible to use latest features.
Common FAQ's:
Q: Can i use Third Party Libraries after refactoring to AndroidX?
A: Yes all the artifacts of third party library will change into androidX artifacts at runtime .
Q:Is it compulsary to migrate to AndroidX?
A: if you don't want to use new library which is already migrated to androidX then you can still use your old project .
Q: will i lost my previous Project work after migrating to AndroidX?
A: No not at all your all project files remain same but some package and artifacts change
Note: in some cases the refactoring will not change all packages of your projects in this condition you can manually change them .
Q:Can i migrate in lower version Android Studio?
A: for migrating to AndroidX your android studio should be minimum 3.2.x and greater than this.
References:
---- Thank you and if you like this article please share this post .----
Comments
Post a Comment