Skip to content

Instantly share code, notes, and snippets.

@loganj

loganj/script.sh

Forked from dlew/script.sh
Last active Mar 3, 2021
Embed
What would you like to do?
Simple AndroidX Migration Script
#!/usr/bin/env bash
# Original by Dan Lew
#
# 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.
#
# Changes by Logan Johnson:
# - replaced find with rg, which finds matching files much, much faster
# - replaced sed with perl, which does the replacement much, much faster
# Original ran for 28 minutes on my laptop before I killed it. This version completes in just over a minute.
# As Dan said: not perfect, won't find every issue, good luck.
MAPPING_FILE=androidx-class-mapping.csv
PROJECT_DIR=../register
from_pkgs=""
replace=""
while IFS=, read -r from_pkg to_pkg
do
from_pkgs+=" -e $from_pkg"
replace+="; s/$from_pkg/$to_pkg/g"
done <<< "$(tail -n +2 $MAPPING_FILE)"
rg --files-with-matches -t java -t kotlin -t xml -F $from_pkgs $PROJECT_DIR | xargs perl -pi -e "$replace"
@magicmass

This comment has been minimized.

Copy link

@magicmass magicmass commented Aug 13, 2019

Hi,

Trying to run your script and just wondering whether i need to change these values:

from_pkgs=""
replace=""
If you could let me know whether I need to that would be great?

Thanks

@loganj

This comment has been minimized.

Copy link
Owner Author

@loganj loganj commented Aug 13, 2019

Nope! You'll need to download the CSV file referenced on line 7, and then you may need to edit MAPPING_FILE= and PROJECT_DIR= on lines 21 and 22.

@mutablestudio

This comment has been minimized.

Copy link

@mutablestudio mutablestudio commented Oct 15, 2019

@loganj thx for this, blazing fast!

@mochadwi

This comment has been minimized.

Copy link

@mochadwi mochadwi commented Nov 8, 2019

Thanks, @loganj :) Fast is everything nowadays!!!

@OlivierFreyssinet

This comment has been minimized.

Copy link

@OlivierFreyssinet OlivierFreyssinet commented Mar 26, 2020

Thanks a lot, saved me from a lot of pain!

@DastanIqbal

This comment has been minimized.

Copy link

@DastanIqbal DastanIqbal commented May 5, 2020

Mac user don't forget to install rg brew install ripgrep

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