Skip to content

Instantly share code, notes, and snippets.

@samueljarosinski
Forked from AndiMiko/migrateAndroidX.py
Last active March 25, 2019 19:03
Show Gist options
  • Select an option

  • Save samueljarosinski/671464646313481570431709446889c9 to your computer and use it in GitHub Desktop.

Select an option

Save samueljarosinski/671464646313481570431709446889c9 to your computer and use it in GitHub Desktop.
import csv
import glob
import os
import shutil
import sys
import urllib.request
if len(sys.argv) != 2:
print('Usage: ' + sys.argv[0] + ' [path to project]')
sys.exit()
projectPath = sys.argv[1]
mappingUrl = 'https://developer.android.com/topic/libraries/support-library/downloads/androidx-class-mapping.csv'
mappingFile = 'androidx-class-mapping.csv'
def replace_all(text, dict):
for i, j in dict.items():
text = text.replace(i, j)
return text
for path in glob.iglob(projectPath + '/**/build', recursive=True):
if os.path.isdir(path):
print("Removing " + path)
shutil.rmtree(path)
if not os.path.isfile(mappingFile):
print('Downloading mapping file...')
urllib.request.urlretrieve(mappingUrl, mappingFile)
with open(mappingFile, mode='r') as mapping:
reader = csv.reader(mapping)
replaceDict = {row[0]: row[1] for row in reader}
files = []
for ext in ('/**/*.xml', '/**/*.kt', '/**/*.java', '/**/*.groovy'):
files.extend(glob.iglob(projectPath + ext, recursive=True))
for filename in files:
print(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. Exception: " + str(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment