I. Project Introduction:
First of all, Android x has a support library. Due to various historical reasons, there are v4 and V7 versions in the support library. In order to unify the name of the dependency library, but also in order to more accurate import dependency library, reduce the volume of application package, Google 2018 IO conference launched AndroidX, currently new projects will also choose AndroidX by default. It is predictable that more and more third-party libraries will support AndroidX in the future, so it is inevitable that AndroidX will be upgraded.
In this project review, I will talk about how to upgrade the company’s project from the support library version 27 to Androidx, including the whole migration process and stomp pit record.
Ii. Project Background
Since the company’s project has been iterated since 2016, and the support library used is version 27, I wanted to try Jetpack Compose, but there was no way. And many open source projects only maintain the AndroidX version, which affects the development mood. Hence the decision to migrate to AndroidX.
Moving to AndroidX is not just about trying something new. Google’s support library is stuck at version 28, and many open source projects have stopped supporting the support library. In other words, there is no way to fix old bugs and experience new features by using the Support library all the time.
Iii. Practice Process:
Now that you have an idea, go ahead!
1. Start over
First of all, safety awareness is fully pulled, and a separate branch is pulled for migration.
Since the Android support library version in the project is 27, we should first follow the migration idea provided by Google: upgrade the version library to 28 first. If there is no problem running on the 28 version, we can upgrade to AndroidX. This is because there are only naming differences between AndroidX and version 28 of the Support library.
2. Change the day
Add the following two sentences to the gradle.properties file.
android.useAndroidX=true
android.enableJetifier=true
Copy the code
The first sentence here is easy to say, using AndroidX as the name implies.
The second sentence indicates that the dependency library in your project will be replaced with the AndroidX version. However, this feature is a bit radical, and it’s actually replacing the Beta version on AndroidX, not the stable version, so we’ll have to wait and see.
3. Go two ways
Once you have completed the work before the migration, the main part comes, one-click migration:
Press Refactor -> Migrate to AndroidX.
Have a cup of milk tea and your project will automatically migrate to AndroidX. (Click on a bunch of default options later.)
Is it that simple? It’s not.
4. It’s hard to part
After a long wait, I found that the one-click migration to AndroidX offered by Android Studio didn’t work as well as I thought. Here are a few other issues:
- You will also need to manually change the support component in the XML layout file and Java file to the AndroidX version, as shown in the replacement lookup table on the official website.
- You need to replace the third-party library version with one that supports AndroidX. The important thing to note here is that the above mentioned
android.enableJetifier=true
Will useBeta
Version, so you need to carefully choose your own appropriateStable version
. - If your project is component-based or config.gradle is used for project configuration, you will need to modify the dependency libraries in the file.
- Finally, modify the support related in the confused file and replace it with AndroidX.
At this point, the migration task is complete.
Iv. Summary thinking:
For AndroidX, there’s only one word: smells good!
The idea of moving to AndroidX has been around for a long time, but it never took off. This was mainly because the business was busy and it was difficult to migrate during development. I happened to have more leisure recently, and I was determined to solve this pain point, so this article was born. And through this migration, some of the new features of AndroidX and have a further understanding, can be counted as a harvest.
In and of itself, the migration to AndroidX is not that difficult. However, more important for developers is the improvement in development efficiency after migration, and the safety of not worrying about the support library being abandoned
Reference article:
- Google official Migration Guide
This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign