Skip to content

Instantly share code, notes, and snippets.

@dudeinthemirror
Last active July 8, 2021 07:00
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dudeinthemirror/cb4942e0ee5c3df0fcb678d1798e1d4d to your computer and use it in GitHub Desktop.
Save dudeinthemirror/cb4942e0ee5c3df0fcb678d1798e1d4d to your computer and use it in GitHub Desktop.
#! /bin/bash
# shellcheck disable=SC2086
# shellcheck disable=SC2162
# This script provides a workaround for having dependent modules which
# have not been migrated to AndroidX
# see: https://developer.android.com/jetpack/androidx/migrate
# inspired from https://gist.github.com/dlew/5db1b780896bbc6f542e7c00a11db6a0
SCRIPTS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(cd "${SCRIPTS_DIR}"/.. && pwd)"
MAPPING_FILE="$SCRIPTS_DIR/androidx_class_map.csv"
GSED=$(command -v gsed)
if [[ -z "$GSED" ]]; then
echo "ERROR: you need to install gnu-sed first."
echo "run: 'brew install gnu-sed'"
exit 101
fi
replace=""
while IFS=, read -r from to
do
replace+="s/\<$from\>/$to/g;"
done <<< "$(cat $MAPPING_FILE)"
i=0
(find $PROJECT_DIR \( -name "*.kt" -o -name "*.java" -o -name "*.xml" \) -type f ! -path '*/\.git*' ! -path '**/android/app/build/*' ! -path '**/\.idea/*' 2>/dev/null |
while read file
do
grep -E "android.arch|android.databinding|android.support" $file > /dev/null 2>/dev/null
ret=$?
if (( ! ret )); then
$GSED -i.bak "$replace" $file
cmp --silent $file $file.bak
ret=$?
if (( ret ));then
printf "\nDoing file %s\n" $file
else
i=$((i+1))
printf '\r%2d skipped' $i
rm -f $file.bak
fi
fi
done
echo
)
@dudeinthemirror
Copy link
Author

dudeinthemirror commented Jun 25, 2019

This was inspired by the original script published by Dan Lew here: https://gist.github.com/dlew/5db1b780896bbc6f542e7c00a11db6a0
It has a few enhancements:

  • exclude android/app/build and .idea directories when searching for files
  • rather than blindly applying the sed on all files, it filters down the list by looking for the 3 possible prefixes android.arch, android.databinding and android.support
  • it skips the files where no replacement was done
  • creates a .bak copy of the original file (in case you want to verify the correctness of the replacement)
  • errors out and prompts the user to install gsed if it is not found
  • the script assumes that you have a scripts dir under the root of the project and both this script and the .csv file (androix_class_map.csv) reside in this dir
  • here is the .csv mapping file I'm using: https://gist.github.com/dudeinthemirror/5985954c68912982c4e73806704ad282

@atetc
Copy link

atetc commented Jul 18, 2019

Hi, @dudeinthemirror !
My AS is highlighting me androidx.test.InstrumentationRegistry is deprecated.

So, I would recommend to update this line:
android.support.test.InstrumentationRegistry,androidx.test.platform.app.InstrumentationRegistry

@dudeinthemirror
Copy link
Author

hey @atetc, thanks for the heads up! I updated the .csv file.

@alleniver
Copy link

good job,thanks man.

@abd3lraouf
Copy link

abd3lraouf commented Sep 8, 2019

For ubuntu 18.04

https://gist.github.com/AbdElraoufSabri/907163eecbb87e7ca033d01004623c05

Features

  • one command download and execute
  • using sed
  • linux compatible

@kedarmp
Copy link

kedarmp commented Sep 24, 2019

Seems to work great in a multi-project build! Thanks for this!

@kedarmp
Copy link

kedarmp commented Sep 26, 2019

@dudeinthemirror
In general, substrings should come first in the mapping file. For example, the following two should be reversed to prevent GridLayoutManager from being changed to an incorrect import.
image

@dudeinthemirror
Copy link
Author

dudeinthemirror commented Sep 28, 2019

@kedarmp, very good catch, thanks. I updated the sed predicate to look for the exact word boundary \< \>

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