|
| 1 | +--- |
| 2 | +title: AndroidX compatibility |
| 3 | +--- |
| 4 | + |
| 5 | +{{site.alert.note}} |
| 6 | + You may be directed to this page if the framework detects a problem in your |
| 7 | + Flutter app involving AndroidX incompatibilities. |
| 8 | +{{site.alert.end}} |
| 9 | + |
| 10 | +Android code commonly uses [support |
| 11 | +libraries](https://developer.android.com/topic/libraries/support-library/) to |
| 12 | +stay backwards compatible. There's two conflicting sets of support libraries |
| 13 | +technically available today, but the |
| 14 | +original libraries are deprecated and no longer supported. |
| 15 | +[AndroidX](https://developer.android.com/jetpack/androidx/) is the only current |
| 16 | +version of the support libraries. It has feature parity with the originals and |
| 17 | +some additional capabilities. |
| 18 | + |
| 19 | +The original support libraries and AndroidX are unfortunately completely |
| 20 | +incompatible with each other. Gradle will crash when trying to build APKs that |
| 21 | +use both sets of libraries. |
| 22 | + |
| 23 | +Some Flutter plugins have been migrated to AndroidX after originally using the |
| 24 | +original suport libraries. Flutter apps that aren't compatible with AndroidX |
| 25 | +will fail to compile when using the latest version of these plugins. |
| 26 | + |
| 27 | +# Fixing AndroidX crashes in a Flutter app |
| 28 | + |
| 29 | +There's two main ways AndroidX can break a Flutter app at compile time. |
| 30 | + |
| 31 | +1. The app is using an AndroidX plugin and its main `gradle.build` file has a |
| 32 | + `compileSdk` below 28. |
| 33 | +2. The app is using both deprecated support and AndroidX code at the same time. |
| 34 | + |
| 35 | +The error messages from Gradle vary. Occasionally the messages will mention |
| 36 | +"package androidx" or "package android.support" directly. However often the |
| 37 | +Gradle error messages aren't obviously related, and will instead talk about |
| 38 | +"AAPT," "AAPT2," or otherwise mention failing at "parsing resources." |
| 39 | + |
| 40 | +Both of these problems need to be fixed by either manually migrating the code or |
| 41 | +downgrading to versions of the plugins that are still using the original support |
| 42 | +libraries. |
| 43 | + |
| 44 | +## How to migrate a Flutter app to AndroidX |
| 45 | + |
| 46 | +{{site.alert.note}} |
| 47 | + It's impossible to fully migrate your app to AndroidX if it's actively using |
| 48 | + some plugins that are still on the previous old support library. If your app |
| 49 | + depends on plugins that are using the old `android.support` packages, you'll |
| 50 | + need to [avoid AndroidX](#avoiding-androidx) instead. |
| 51 | +{{site.alert.note}} |
| 52 | + |
| 53 | +First make sure that `compileSdkVersion` is at least `28` in `app/build.gradle`. |
| 54 | +This property controls the version of the Android SDK that Gradle uses to build |
| 55 | +your APK. It doesn't affect the minimum SDK version that your app can run on. |
| 56 | +[More info here.](https://developer.android.com/studio/build/#module-level) |
| 57 | + |
| 58 | +### Recommended: Use Android Studio to migrate your app |
| 59 | + |
| 60 | +This requires the latest version of Android Studio. |
| 61 | + |
| 62 | +1. Follow the instructions on [this |
| 63 | + page](https://flutter.io/docs/development/tools/android-studio#android-ide) |
| 64 | + for importing a Flutter app into Android Studio so that the IDE can parse the |
| 65 | + Android code. |
| 66 | +2. Follow the instructions on [this |
| 67 | + page](https://developer.android.com/jetpack/androidx/migrate) for migrating |
| 68 | + an app to AndroidX automatically using Android Studio. |
| 69 | + |
| 70 | +### Not recommended: Manually migrate your app |
| 71 | + |
| 72 | +See [this page](https://developer.android.com/jetpack/androidx/migrate) for |
| 73 | +detailed instructions on how to do this. |
| 74 | + |
| 75 | +## Avoiding AndroidX |
| 76 | + |
| 77 | +If you want or need to avoid migrating to AndroidX, you'll need to go through |
| 78 | +and pin your plugin dependencies in your `pubspec.yaml` to the last major |
| 79 | +version from before they were migrated. |
| 80 | + |
| 81 | +These are the last available versions of all the `flutter/plugins` packages that |
| 82 | +are pre AndroidX: |
| 83 | + |
| 84 | +- `android_alarm_manager`: 0.2.3 |
| 85 | +- `android_intent`: 0.2.1 |
| 86 | +- `battery`: 0.3.0 |
| 87 | +- `camera`: 0.2.9+1 |
| 88 | +- `cloud_firestore`: 0.8.2+3 |
| 89 | +- `cloud_functions`: 0.0.5 |
| 90 | +- `connectivity`: 0.3.2 |
| 91 | +- `device_info`: 0.3.0 |
| 92 | +- `firebase_admob`: 0.7.0 |
| 93 | +- `firebase_analytics`: 1.1.0 |
| 94 | +- `firebase_auth`: 0.7.0 |
| 95 | +- `firebase_auth`: 0.7.0 |
| 96 | +- `firebase_core`: 0.2.5+1 |
| 97 | +- `firebase_database`: 1.0.5 |
| 98 | +- `firebase_dynamic_links`: 0.1.1 |
| 99 | +- `firebase_messaging`: 2.1.0 |
| 100 | +- `firebase_ml_vision`: 0.2.1 |
| 101 | +- `firebase_performance`: 0.0.8+1 |
| 102 | +- `firebase_remote_config`: 0.0.6+1 |
| 103 | +- `firebase_storage`: 1.0.4 |
| 104 | +- `google_maps_flutter`: 0.1.0 |
| 105 | +- `google_sign_in`: 3.2.4 |
| 106 | +- `image_picker`: 0.4.12+1 |
| 107 | +- `local_auth`: 0.3.1 |
| 108 | +- `package_info`: 0.3.2+1 |
| 109 | +- `path_provider`: 0.4.1 |
| 110 | +- `quick_actions`: 0.2.2 |
| 111 | +- `sensors`: 0.3.5 |
| 112 | +- `share`: 0.5.3 |
| 113 | +- `shared_preferences`: 0.4.3 |
| 114 | +- `url_launcher`: 4.1.0+1 |
| 115 | +- `video_player`: 0.9.0 |
| 116 | +- `webview_flutter`: 0.2.0 |
| 117 | + |
| 118 | +Note however that this list is not an exhaustive list of all the Flutter plugins |
| 119 | +that use AndroidX, and the AndroidX dependency in your app may be coming from |
| 120 | +another plugin besides these. |
| 121 | + |
| 122 | +# For plugin maintainers: Migrating a Flutter plugin to AndroidX |
| 123 | + |
| 124 | +Migrating a Flutter plugin to AndroidX follows basically the same process as |
| 125 | +[migrating a Flutter app](#How-to-migrate-a-Flutter-app-to-AndroidX), but with |
| 126 | +some additional concerns and some slight changes. |
| 127 | + |
| 128 | +1. Make sure you increment the [major |
| 129 | + version](https://www.dartlang.org/tools/pub/versioning#semantic-versions) of |
| 130 | + your plugin for this change and clearly document it in your plugin's |
| 131 | + changelog. This is a breaking change that requires manual migration from |
| 132 | + users to fix. Pub treats different digits differently depending on if a |
| 133 | + plugin is pre or post 1.0.0. The very first digit is the major version for |
| 134 | + plugins that are at or above 1.0.0. For plugins that are below it, the middle |
| 135 | + digit is considered to the major version. |
| 136 | +2. Plugin code can be automatically migrated with Android Studio the same way |
| 137 | + that Flutter app code can be. Import the `plugin_root/example` app into the |
| 138 | + IDE as if it's a regular Flutter app. Android Studio will also import and |
| 139 | + parse the plugin's Android code. |
0 commit comments