Skip to main content

All about Android extension libraries (AndroidX)

Nowadays we commonly get this type of error in gradle when we use third party library :

 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:
  1. Click Refactor from menu options in Android studio.
  2. 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'
}


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

Popular posts from this blog

How to Download Apk file from Url and Install Programmatically

In this post we learn about download apk file from server or website and then install it Programmatically in Phone. Sometimes we have to download external apk file from server and then install if downloading successfully finished.For this we use AsyncTask class  for background process. So here is Code Snippet for this task.Lets Start :- Before this we have to add these Permissions in Manifest.xml file : <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> DownloadNewVersion.java class DownloadNewVersion extends AsyncTask<String,Integer,Boolean> { @Override protected void onPreExecute() { super.onPreExecute(); bar = new ProgressDialog(getActivity()); bar.setCancelable(false); bar.setMessage("Downl...

Auto read OTP using SMS User Consent API in Android

Some times ago Google Play restricts the use of high risk or sensitive permissions, including the SMS or Call Log permission groups. It means you have to justify why you are taking SMS or other high risk or sensitive permissions for your application .For this you may be required to complete the Permissions Declaration Form and receive approval from Google Play. Before this every app even who does not have any purpose for taking SMS or other high sensitive permissions in their app ,They was taking these permissions from users and this is a kind of security beach.  For solution of this problem Google introduce SMS User Consent API . The SMS User Consent API complements the SMS Retriever API by allowing an app to prompt the user to grant access to the content of a single SMS message. When a user gives consent, the app will then have access to the entire message body to automatically complete SMS verification. If your appeal for SMS permission is rejected by Google team you can u...

How to open pdf file url using webview in Android

 Sometimes we have requirement of showing pdf file in our Android Application. Although there are many third party gradle dependencies available online by which you can show your pdf file easily but one of the major drawback of using these libraries are they will increase you apk size .In some cases they will increase apk size upto ~14 MB. Alternatively you can access and show pdf file using assets and from storage options. This can take less size than any third party library. Here we use pdf using url and if you directly open your pdf file url in webview you will get no result because it will start downoad if you open this link in any web browser.  So in this article we will learn how we can open our pdf url using webview and without using any third party library. So lets Start step by Step:- Lets Suppose you have a pdf file url something like   http://yourwebsite.com/files/mydocumentfile.pdf Step 1: So we you this pdf file url to embed in our webview .First of all you h...