Skip to content

Instantly share code, notes, and snippets.

@AndiMiko
Created February 8, 2019 02:16
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save AndiMiko/58ecc04a64ac4f89eb5262176ab3fc9e to your computer and use it in GitHub Desktop.
Save AndiMiko/58ecc04a64ac4f89eb5262176ab3fc9e to your computer and use it in GitHub Desktop.
Replaces the pairs in androidx-class-mapping.csv (get it here: https://developer.android.com/jetpack/androidx/migrate) to migrate Android support to AndroidX
import glob
import csv
dictCSV = "C:/path/to/androidx-class-mapping.csv"
projectPath = "C:/path/to/project/src/"
def replace_all(text, dic):
for i, j in dic.items():
text = text.replace(i, j)
return text
with open(dictCSV, mode='r') as infile:
reader = csv.reader(infile)
replaceDict = {rows[0]:rows[1] for rows in reader}
files = []
for ext in ('/**/*.xml', '/**/*.kt', '/**/*.java'):
files.extend(glob.iglob(projectPath + ext, recursive=True))
for filename in files:
print("Replacing in file: " + filename)
try:
with open(filename, 'r') as file :
filedata = file.read()
filedata = replace_all(filedata, replaceDict)
with open(filename, 'w') as file:
file.write(filedata)
except Exception as e:
print("Error reading/writing file. Skipping ...")
@oO0oO0oO0o0o00
Copy link

good!

@magicmass
Copy link

Amazing! Thank you!

@ZhouKanZ
Copy link

ZhouKanZ commented Dec 27, 2021

there are some problem when then replace ocurr , ex :
com.support.car : com.androidx.car
com.support.carABC.com.androidx.carBCD

when i need migrate com.support.carABC, it will be change to com.androidx.carABC

becouse of , the order of csv and python in pyhase

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