AndroidX is redesigned library to make package names more clear. In AndroidX only package name and maven artifact name changed, classes, methods and field names are not changed. AndroidX is replacement of the original library Android Support Library.
What’s new in AndroidX?
- All packages in AndroidX live in a consistent namespace starting with string AndroidX. Support Library packages have been replaced with AndroidX. i.e : com.android.support. replace with AndroidX.
- AndroidX packages are separately maintained and updated. Support library uses SDK version but AndroidX library uses strict semantic versioning. It’s going to re-version from 28.0.0 → 1.0.0. You can update AndroidX libraries in your project separately.
- All the support library classes are available in AndroidX library.
How to Migrate an existing Project in AndroidX using Android Studio?
In Android Studio 3.2 and higher version, you can directly migrate your existing project into AndroidX. For this, Your Project build.gradle file needs to be set to at least com.android.tools.build:gradle:3.2.0 in order to migrate to AndroidX otherwise you will get this type of error message.
After this follow this step, Android Studio → Refactor Menu → Migrate to AndroidX. It will open an alert window like this,
This refracts all packages automatically and as a result converts your project into AndroidX. For more information, you can refer to this document.
Bugs of migrating
- After migrating your project you will get some minor errors which can be solved easily.
- Other 3rd party dependencies are not converted into AndroidX, but they get converted at run time by Jetifier.
How to use AndroidX in New Project?
If you want to use AndroidX in your project you need to set the compile SDK to API level 28(Android 9.0 Pie) or higher therefore put this flag in your gradle.properties
- android.useAndoridX : When we set this flag to true , Android plugin use the appropriate AndroidX library. Default value of this flag is false.
- android.enableJetifier : When we set this flag to true, Android plugin automatically migrates exiting third-party libraries to use androidX by rewriting their binary. Default value of this flag is false. That means none of your libraries will be re-wrote to AndroidX.
What is Jetifier?
Jetifier will convert support library of all dependencies to AndroidX automatically. If you set enableJetifier to false then your project will have both package of support library and AndroidX, which is not relevant.
For example, if you have RecyclerView.java in your dependency. That uses support library recyclerview-v7.
This class is moved now to AndroidX package, so you need to convert this package to AndroidX package.
For this you need to use flag enableJetifier to true to convert
com.android.support:recyclerview-v7 to androidx.recyclerview:recyclerview
while building the project.
Third-party libraries are not converted to AndroidX in directory, but they get converted at run time by Jetifier.
So for this, you need to Enable Jetifier when you migrate from Support Libraries to AndroidX.
I hope this article will help you. Thanks for reading.