If you are developing in Flutter and have not run into the following problem, pay attention, because it will happen to you (sooner or later). You are browsing the Flutter packages website, looking for one that will cater to your needs. Once you find one, you inspect its README.md page and you see the following:
For those of you that are unaware, AndroidX is the new support library that Google has been pushing for some time now to replace the older support libraries that are found inside Android Studio. What does this have to do with me, you say.
Well, in short, everything.
I Just Wanted To Develop In Flutter
Getting your head around the whole support libraries issue can seem irrelevant, but it is something you need to understand. This is because, whatever your application is doing, the code you are writing relies on the support libraries. Support libraries are there to help you:
- Support multiple API versions by providing backwards compatibility
- Include libraries that do not come in the standard framework API so you can develop more features
- Debug and proof your application (I.E. Multidex support)
As I stated earlier, since the release of Android 9 (API level 28), the newer version of the support libraries is called AndroidX. You can still use the older support libraries, but as stated on their website, all new development will happen in AndroidX libraries. So, if you are developing a new application, you might as well already use the new support libraries. And if you already have an application, you seriously need to consider migrating it to use AndroidX.
Migrating - A New Project
To migrate a new Flutter project, you can use the built in functionality found inside Android Studio. An option inside the top menu, Refactor->Migrate to AndroidX is supposed to do the hard work for you. But if it were that easy, would I be writing this article?
When you create a new Flutter project, your compileSdkLevel will already be set to 28, but if you try migrating with the method mentioned above, you will get the following popup:
This is an annoying bug in Android Studio, which will hopefully be fixed. We are left with doing the migration ourselves.
- Open the Android part of your Flutter application by switching to the Project view hierarchy and selecting Flutter -> Open Android module in Android Studio
2. Once opened, Android Studio will ask you to upgrade your gradle version:
3. After it finishes doing so, you will see this warning:
Yet again, this is something that needs to be fixed, but we will persevere and move on. The next steps are similar to what is needed to be done to migrate an existing project, so just follow on through to the next section.
Migrating — An Older Project
- Open your gradle.properties file and add the following two lines:
2. Open your gradle-wrapper.properties file and change the distributionUrl to this:
3. Upgrade the gradle version to 3.3.0 in your application level gradle file (disregard if you updated gradle as part of migrating a new project):
4. Replace all the libraries that relied on the older support libraries with their AndroidX counterparts. For example:
A Hard Truth
Before you can go along, happily skipping, enjoying your triumphant victory over the platform, you need to face a harsh truth:
It’s impossible to fully migrate your app to AndroidX if it’s actively using any plugins that rely on the old support library. If your app depends on plugins that use the old
android.supportpackages, you’ll need to avoid using AndroidX.
Taken directly from Flutter’s documentation, the above sentence completely obliterates your ability to interleave packages that use different versions of the support library. Where will this happen to you? If for instance you want to get a user’s location, you will notice that the permissions package relies on the older support libraries, but the location package uses the newer AndroidX libraries. You can always try to use the version of a package before it moved to AndroidX, but then you might face bugs that have been fixed in later versions or other compatibility issues.
All in all, what you are left with is a sort of game of mix and match, where you have to choose what packages you use wisely.