When I first joined this company, I just happened to catch up with the migration of the project AndroidX. I stepped in some holes and took some notes


The origin of Android Support Library

Android 3.0 (API 11) added Fragment in order to better compatibility with tablets. However, in order to make the lower version of the system also work, there needs to be a downward compatibility, so the Android team launched the Android Support Library.

The following libraries that are familiar to older Androids (the number after v represents the lowest compatible API version, such as 4 for Android 1.6) belong to the Android Support Library:

  • Com. Android. Support: support – v4 to android 1.6, containing fragments, NotificationCompat controls, contain v7 and v11 basis function, early use.
  • Com. Android. Support: appcompat – v7: xx, xx, android 2.1, added a lot of Material compatible classes and Material Design, contains all the v4 content, use at most;
  • Support-v13 → Android 3.2, a version compatible package developed for tablet. Android 3.x system is a dedicated system for tablet, which is rarely used;

Android version updates quickly, now are Android 12, domestic apps are basically compatible with Android 5.1 at least (API 21), this V4, V7 name has no great significance.

0x2, The advent of AndroidX Library

Starting with Android 9.0 (API 28), AppCompat-V7:28.0.0 is the final release of the Support Library, with future new features and improvements coming into the AndroidX Library. There are two main aspects of the upgrade:

  • 1.The package nameThe API package name in the Library is Android.support.And the apis in the AndroidX Library become AndroidX., which means that the apis under the android.* package are shipped with the system, while AndroidX. The apis under the package are distributed with the extension library, and the API is basically independent of the specific version of the operating system.
  • Appcompat-v7 is now the appcompat Library. AndroidX Library is now the appcompat Library. AndroidX Library is now the appcompat Library.
api 'com. Android. Support: appcompat - v7:28.0.0'
api 'androidx. Appcompat: appcompat: 1.0.0'
Copy the code

0x3, Transition from Support to AndroidX

A key migration

AS 3.2 and above provides one-click migration to AndroidX by clicking Refactor → Migrate to AndroidX on the menu bar

Note: One-click migration, compileSdkVersion must be greater than or equal to 28. You need to have at least have compileSdk 28 Set in your module build.gradle to refactor to Androidx.

If the migration fails, repeat steps ①②③④ below for manual migration

Version Requirements

  • Android Studio → Upgrade to 3.2 and above
  • Gradle distributionUrl = version number in Gradle /wrapper/gradle-wrapper.propertie
  • CompileSdkVersion → Upgrade to 28 and above;
  • BuildToolsVersion → Upgrade to 28.0.2 and above;

② migrate AndroidX configuration

Add the following configuration to your project’s gradle.properties file:

# AndroidX is enabled for current projects
android.useAndroidX=true

# Migrate dependencies to AndroidX, usually true
If set to false, the table does not migrate dependencies to AndroidX, which may cause problems if there are third-party dependencies
android.enableJetifier=true
Copy the code

③ Modify the dependent libraries

Refer to the dependency library mapping changes in AndroidX changes, you can directly check the official document or download the mapping CSV file, the modification example is as follows:

Implementation of com. Android. Support: cardview - v7 replace - > implementation androidx. Cardview: cardview implementation Com. Android. Support: the collections and replace implementation androidx. Collection: collection implementation Com. Android. Support: coordinatorlayout replace - androidx. Coordinatorlayout: coordinatorlayoutCopy the code

(4) Repackage the dependency classes

Refer to the class mapping changes in AndroidX changes, you can directly check the official document or download the mapping CSV file, the modification example is as follows:

Import android. Support. V7. App. AlertDialog modified into - > import androidx. Appcompat. App. The AlertDialog import Android. Support. V7. App. AppCompatActivity modified into - > import androidx. Appcompat. App. AppCompatActivityCopy the code

Also, you may need to manually modify the obfuscation file proGuard-rules

0x4. Some questions are collected

① Can Support and AndroidX coexist?

A: No, you can only choose one.

AndroidX Migrate to AndroidX

A: Not necessarily, some registration/pathname conversion may be problematic, and some need to be manually adjusted (XML, Java, KT)

Error in DataBinding (duplicate id error)?

Error checking and handling is stricter in AndroidX, and an error will be reported if an ID with the same name exists in the same XML layout file.

④ Do repeated attribute names in attr. XML cause errors?

A: Custom control to write custom attributes, can not be the same as android existing attributes, such as textSize must use Android :textSize.

Glide annotation is not compatible with AndroidX

A: Glide upgrade to 4.8.0, specific visible official issues

⑥ there is a conflict between the Support library and the androidX library without migrating to androidX.

Run gradlew :app:dependencies on AndroidX. Run gradlew :app:dependencies on AndroidX. The androidx library has been referenced from the previous version. In addition, it is recommended that you use a specific version when referencing dependent libraries and avoid using latest.release or +.

References:

  • Official documentation: Migration to AndroidX

  • Keep hearing about AndroidX, what is AndroidX anyway?

  • Still using Android.support? Time to consider migrating AndroidX!

  • Various issues encountered during the migration to AndroidX