Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple AndroidX Migration Script
#!/usr/bin/env bash
# I've found that the "Migrate to AndroidX" converter in Android Studio doesn't work very
# well, so I wrote my own script to do the simple job of converting package names.
#
# You can download a CSV of package names here: https://developer.android.com/topic/libraries/support-library/downloads/androidx-class-mapping.csv
#
# It'll run faster on a clean build because then there are fewer files to scan over.
#
# Uses `gsed` because I'm on a Mac. Can easily replace with `sed` if you don't have `gsed`.
#
# This isn't perfect; it won't find every conversion issue. You break it you buy it. Viewer discretion is advised.
MAPPING_FILE=path/to/androidx-class-mapping.csv
PROJECT_DIR=path/to/android/project
replace=""
while IFS=, read -r from to
do
replace+="; s/$from/$to/g"
done <<< "$(cat $MAPPING_FILE)"
find $PROJECT_DIR \( -name "*.kt" -o -name "*.java" -o -name "*.xml" \) -type f -not -path '*/\.git*' -print0 | xargs -0 gsed -i "$replace"
@matthewcmckenna

This comment has been minimized.

Copy link

@matthewcmckenna matthewcmckenna commented Nov 14, 2018

Thanks for this!

@cmargonis

This comment has been minimized.

Copy link

@cmargonis cmargonis commented Nov 15, 2018

Thank you!

@ondrj

This comment has been minimized.

Copy link

@ondrj ondrj commented Nov 20, 2018

Great job, thanks a lot!

@vashisthg

This comment has been minimized.

Copy link

@vashisthg vashisthg commented Nov 21, 2018

Thanks Dan Leu
We can add

android.support.design.widget.FloatingActionButton,com.google.android.material.floatingactionbutton.FloatingActionButton
android.support.design.widget.AppBarLayout,com.google.android.material.appbar.AppBarLayout

to the csv if using AppBarLayout and FloatingActionButton

@lwasylkowski

This comment has been minimized.

Copy link

@lwasylkowski lwasylkowski commented Nov 27, 2018

Some more missing classes:

android.support.design.widget.Snackbar,com.google.android.material.snackbar.Snackbar
android.support.design.widget.BottomSheetBehavior,com.google.android.material.bottomsheet.BottomSheetBehavior
android.support.design.widget.TextInputEditText,com.google.android.material.textfield.TextInputEditText
android.support.design.widget.TextInputLayout,com.google.android.material.textfield.TextInputLayout
@Kisty

This comment has been minimized.

Copy link

@Kisty Kisty commented Nov 28, 2018

Thanks for this, Dan!

Android relevant issue for missing mappings: https://issuetracker.google.com/issues/119776865

@AndiMiko

This comment has been minimized.

Copy link

@AndiMiko AndiMiko commented Feb 8, 2019

This string doesn't work on windows 10 or Linux. "xargs: Argument list too long" even with just few files

@AndiMiko

This comment has been minimized.

Copy link

@AndiMiko AndiMiko commented Feb 9, 2019

I wrote a python script which can be used cross-platform in case you have the same issue: https://gist.github.com/AndiMiko/58ecc04a64ac4f89eb5262176ab3fc9e

@carstenhag

This comment has been minimized.

Copy link

@carstenhag carstenhag commented Feb 21, 2019

On Mac run: brew install gnu-sed to get gsed.

@srbkrishnan

This comment has been minimized.

Copy link

@srbkrishnan srbkrishnan commented Apr 7, 2019

Thanks a Ton Dan!

@vaudevillen

This comment has been minimized.

Copy link

@vaudevillen vaudevillen commented Jun 17, 2019

Oh my goodness, thank you!!

@loganj

This comment has been minimized.

Copy link

@loganj loganj commented Jun 19, 2019

Here is a much faster version – replaces find with rg and sed with perl, and I think it works just as well. I terminated the original script after 28 minutes. This rg/perl version completed after about 1 minute.

https://gist.github.com/loganj/7535a13e98be83460f362b63dbd13e07

@dudeinthemirror

This comment has been minimized.

Copy link

@dudeinthemirror dudeinthemirror commented Jun 25, 2019

Thanks Dan. I wrote a script based on your idea with some added enhancements: https://gist.github.com/dudeinthemirror/cb4942e0ee5c3df0fcb678d1798e1d4d

@maxpinto

This comment has been minimized.

Copy link

@maxpinto maxpinto commented Jul 5, 2019

thank you.

@mochadwi

This comment has been minimized.

Copy link

@mochadwi mochadwi commented Nov 8, 2019

Awesome this is what we're looking for!!! Thanks for the people here.

@NurseyitTursunkulov

This comment has been minimized.

Copy link

@NurseyitTursunkulov NurseyitTursunkulov commented Dec 10, 2019

how to use this script?

@draekko

This comment has been minimized.

Copy link

@draekko draekko commented Dec 28, 2019

Did a fork for Linux (i'm on Ubuntu) to work around the xarg too long argument list error, and assumes the csv file is in the same directory as the script. https://gist.github.com/draekko/4019f0342553b6c0cbeca1d185245dd1

Hope this helps someone else, thanks for the original, saved me a lot of work.

@OlivierFreyssinet

This comment has been minimized.

Copy link

@OlivierFreyssinet OlivierFreyssinet commented Mar 26, 2020

Thank you, this saved me hours of pain 🙏

@zmtzawqlp

This comment has been minimized.

Copy link

@zmtzawqlp zmtzawqlp commented Dec 22, 2020

after run the script.sh
It seems that there is an error: android.support.v7.widget.GridLayoutManager => androidx.gridlayout.widget.GridLayoutManager
It should be : android.support.v7.widget.GridLayoutManager => androidx.recyclerview.widget.GridLayoutManager

@nicreuss

This comment has been minimized.

Copy link

@nicreuss nicreuss commented Apr 14, 2021

I've noticed a substantial performance improvement by running gradle clean before running this script.
The comment on line 8 suggests this but it is easy to overlook.

It'll run faster on a clean build because then there are fewer files to scan over.

The script now runs in 18s instead of 2m43s!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment