The login
registered
Write an article
Home page
Download the APP

Migrating to AndroidX: tips, tricks, and guides

Love is here

Migrating to AndroidX: tips, tricks, and guides

Original text: medium.com/androiddeve… Jetpack is a set of libraries, tools, and guides that make it easier to write high-quality apps. Jetpack makes coding easier by limiting boilerplate code and simplifying responsible tasks through best practices. Let you focus on your business code. AndroidX is the package name of all the libraries in Jetpack. Think of AndroidX as an open source project for developing, testing, versioning, and releasing the JetPack library. In I/O 2018, we announced the refactoring of the Support Library into the AndroidX namespace, which has now been completed with the completion of Support Library 28 and the release of AndroidX 1.0.

Why make this migration?

Now it’s time to migrate from the Android Support Library to AndroidX. Here are four reasons:

  1. The Android Support Library has done its job. 28.0 is the last release of the Android Support namespace, and the Android Support namespace will not be maintained in the near future. So, if you want to fix bugs or use new features in the Support Library, you need to migrate to AndroidX.
  2. Better package management. With AndroidX, you get standardized and independent version control, as well as more standardized naming and more frequent releases.
  3. Many libraries have been migrated to AndroidX, such as Google Play Services, Firebase, Butterknife, Mockito 2 and SQLDelight.
  4. All newly released Jetpack libraries will use the AndroidX namespace, so if you want to use new features like Jetpack Compose or CameraX, you’ll need to migrate to AndroidX.

The migration to

Before you migrate your code to AndroidX, you need to do these things:

  • Back up your code to the project, if you have a version management tool, you just need to submit your code to the backup, because migration may modify a lot of files in your project, if you don’t want to lose your code.
  • You can create a new branch to perform the migration and then merge the branches after the migration is complete.
  • If you can, try to pause or minimize feature development during your migration (or at least not refactor or introduce new features during the migration) to reduce merge conflicts when your code is merged.

Starting the migration

During migration, it is important to resolve errors that occur so that your project compiles and passes all tests.

Update all Support Library versions to 28

We don’t recommend migrating directly from an older Support Library(like 26 or 27) to AndroidX, because not only do you face namespace changes, you may also face errors due to API changes between the new Library and the old Library. Therefore, I recommend that you first update the Support Library to 28 to resolve any API changes and ensure that your project will compile and pass all tests under version 28 of the Support Library. Support Library28 and AndroidX 1.0 are equivalent binaries, meaning they are just different package namespaces: all APIs are the same. This means that if you migrate from Support Library28 to AndroidX, you’ll need to change very little.

Step 2: Enable Jetifier

Jetifier is used to help migrate third-party libraries you reference using AndroidX. Jetifier will change the bytecode of these third-party libraries to make them compatible with projects using AndroidX. To enable Jetifier in your project, just add the following code to your gradle.properties file:

android.useAndroidX=true
android.enableJetifier=true
Copy the code

Now, when code is automatically imported into the Library, it will be imported into the AndroidX version instead of the old Support Library.

Add your dependencies to this list.

Before starting the migration, you should update the third party library you reference to the latest version of the library, otherwise the compilation may fail. If you are using a library that automatically generates code, Jetifier will not modify it. Therefore, you need to check that your code generation library is compatible with AndroidX. If you want to skip steps 2 and 3, you may encounter some errors:

  • If you are using third-party library code that is not compatible with AndroidX. In this case, a stack trace similar to the one below will show you that it is trying to get an older version of the Support Library:
... Error : Program type already present: android.support.v4.app.INotificationSideChannel$Stub$Proxy | Reason: The Program type already present: android. Support. The v4. App. INotificationSideChannel $Stub $Proxy...Copy the code
  • If your project is only partially migrated, you may run into the error of duplicate classes because it tries to extract the same code from the Support Library and from AndroidX. The stack trace will likely display the following:
... The Duplicate class android. Support. The v4. App. INotificationSideChannel found in modules classes. The jar (androidx. Core: the core: 1.0.0) And classes. The jar (com) android) support: support - compat: 28.0.0)...Copy the code

Update your source code

You have three options for updating your source code to use AndroidX:

  • Android Studio
  • Manual update
  • Bash script

    If you are using Android Studio version 3.2 or higher. You can do this by usingRefactorIn the function menuMigrate to AndroidXOption to update your code. This is the most recommended solution because Android Studio can review your source code during refactoring and make the right decision.

    image.png

    If you are not developing with Android Studio or your project structure is too complex to useMigrate to AndroidXOption cannot be covered. You can useClass maps CSV filesTo find and replace the bash script for replacement. The script should be able to find all source instances with the Android.support class and replace them with AndroidX with the same functionality.

    In particular, the script uses a class-mapped CSV file to replace the package name with grep and sed commands. However, this is a very violent method of migration, and it may not find and replace all code adequately and correctly.

    In addition, updates can also be piecemeal in a purely manual way.

    If you decide to use the manual update method, you can accessMigrating to AndroidXYou can find details about the mapping between the Support Library and AndroidX.

    You can also download this CSV file.

    At this point, you should be able to get your project to compile on AndroidX.

Those nasty surprises

While the tools and processes we’ve discussed here should guide most projects through a smooth migration, we’ve found that in some cases you may need to make changes manually.

Version profile

You need to manually redefine variables for the library version. Before migrating, your build.gradle file might look like this:

Ext. versions = [' drawer ':' 28.0.0 ', 'rview' : '28.0.0] implementation "com. Android. Support: drawerlayout: ${versions. The drawer}" implementation "Com. Android. Support: recyclerview - v7: ${versions. Rview}"Copy the code

After using the migration tool to complete the migration, your code might look like this:

Ext. versions = [' drawer ':' 28.0.0 ', 'rview' : '28.0.0] implementation "androidx. Drawerlayout: drawerlayout: 1.0.0" implementation "Androidx. Recyclerview: recyclerview: 1.0.0"Copy the code

DrawerLayout and RecyclerView use the AndroidX package, but the version number does not use the version number variable, so you need to manually modify it:

Ext. versions = [' drawer ':' 1.0.0 ', 'rview' : '1.0.0] implementation "androidx. Drawerlayout: drawerlayout: ${versions. The drawer}" implementation "Androidx. Recyclerview: recyclerview: ${versions. Rview}"Copy the code

Confuse configuration and build scripts

The migration tool does not automatically update our obfuscation files and any associated built-in scripts. Therefore, if you use these files and they contain the Support Library package name, you will need to manually modify them.

Get the latest stable version

The migration tool extracts the latest version of the AndroidX library. As a result, some of the libraries you are using are Alpha versions. For example, before migrating, the Support Library might be:

Implementation 'com. Android. Support: appcompat - v7:28.0.0'Copy the code

After the migration, you may be using the alpha version of the library for Android:

Implementation 'androidx. Appcompat: appcompat: 1.1.0 - alpha01'Copy the code

So, if you want to use a stable version of the library, you need to manually update the following versions:

Implementation 'androidx. Appcompat: appcompat: 1.0.2'Copy the code

More help and resources

You can find more information about moving to AndroidX with tutorials, tools, and possible problems. This page contains an overview of the AndroidX project, migration guidelines, mapping tables for the old Support Library and new stable or alpha versions of AndroidX, as well as CSV files you need to write migration scripts to. There is also an article here detailing the migration to AndroidX in the Plaid sample project. Finally, there is a problem tracker where you can see the issues the Android team is working on with the migration tool. Of course, you can also use the buttons at the top left of the page to tell us about any problems you find in the migration tool.

Final thoughts

If you haven’t upgraded to AndroidX yet, now is a good time to take advantage of the Jetpack library to simplify development for your projects. As you will see through this article, migration is a simple matter in most cases.

Recommended readingMore highlights

  • This article introduces the migration process of 58.com to AndroidX and the source analysis of Jetifier. “Night of The night” is worth a thousand dollars, flowers have fragrance month… Yu Weiguo Read 1,305 comments 2 likes 4
  • AndroidX is moving from android to Android… Wang Zhangang read 125 comments 0 likes 0
  • What is AndroidX? On this issue directly quote the official words, I believe you will be clear at a glance. AndroidX is Androi… Los Angeles rain at night
  • What is AndroidX? Android technology is iterating fast, with new technologies and terms popping up all over the place. I don’t know when I started hearing Androi… On, Read 126 Comments 0 Likes 0
  • Still using Android.support? Time to consider migrating AndroidX! AndroidX is a new extension from Google’S IO 2018 conference that includes major improvements to the Android support library. And… _ Boxi read 9,253 comments 5 likes 20