Flutter: AndroidX migration
I recently upgraded firebase_auth and my project stopped compiling because they've migrated to AndroidX, meaning I had to migrate my whole project as well. Here's how I did it.
After upgrading firebase_auth version past , I started getting this compile error:
~/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_auth-0.11.1+5/android/src/main/java/io/flutter/plugins/firebaseauth/FirebaseAuthPlugin.java:9: error: cannot find symbol
import androidx.annotation.NonNull;
^
symbol: class NonNull
location: package androidx.annotation
~/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_auth-0.11.1+5/android/src/main/java/io/flutter/plugins/firebaseauth/FirebaseAuthPlugin.java:10: error: cannot find symbol
import androidx.annotation.Nullable;
^
...
BUILD FAILED in 28s
*******************************************************************************************
The Gradle failure may have been because of AndroidX incompatibilities in this Flutter app.
See https://goo.gl/CP92wY for more information on the problem and how to fix it.
*******************************************************************************************
Finished with error: Gradle task assembleDebug failed with exit code 1
The first thing the page on Flutter migration to AndroidX tells you to do is to set compileSdkVersion
to at least 28 in app/build.gradle
.
Migrating automatically
Then you open the android
subfolder in Android Studio, and click Refactor, Migrate to AndroidX.
However, it required me to migrate gradle to 3.2 at least. To do so, I had to check versions at https://developer.android.com/studio/releases/gradle-plugin#updating-gradle.
To upgrade the gradle dependency to 3.4.0, I modified the gradle wrapper version in android/gradle/wrapper/gradle-wrapper.properties
to 5.1.1:
...
distributionUrl = https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
...
and the build.gradle
:
...
classpath 'com.android.tools.build:gradle:3.4.0'
...
To make sure it downloaded the new gradle, I triggered a build. Now the Gradle plugin complained that I need to update the Kotlin plugin:
The Android Gradle plugin supports only Kotlin Gradle plugin version 1.3.10 and higher.
The following dependencies do not satisfy the required version:
root project 'android' -> org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.30
I checked https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-gradle-plugin to find the latest version and changed build.gradle
to:
buildscript {
ext.kotlin_version = '1.3.31'
...
Now the compilation went a bit further and failed with:
Caused by: org.gradle.internal.resolve.ModuleVersionNotFoundException: Could not find org.jetbrains.kotlin:kotlin-stdlib-jre7:1.3.10.
I had to change this dependency in app/build.gradle
to:
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
...
After that the compilation went a bit further still and failed with the initial error: cannot find symbol class NonNull
error related to Firebase.
Despite trying repeatedly to "Migrate AndroidX automatically", it would tell me there is nothing to migrate. So I had to resort to manual migration.
Manual migration
https://flutter.dev/docs/development/packages-and-plugins/androidx-compatibility and https://developer.android.com/jetpack/androidx/migrate list out the package mappings. I ended up changing the only two dependencies to the Android Support library I had in app/build.gradle
:
dependencies {
...
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
...
and appending to gradle.properties
:
android.enableJetifier=true
android.useAndroidX=true
Finally the project compiled! Hopefully Flutter updates the new project templates so the migration is already done for us.