用Python 快速一键迁移至AndroidX
类映射
下表列出了从旧支持库软件包到新 androidx 软件包的当前映射关系。您也可以下载包含这些映射信息的 CSV 格式文件。
项目的java/xml文件过多,如果是单纯的匹配和替换,整个过程很缓慢。所以强烈推荐FlashText:语料库数据快速清理利器。 flashtext
import csv
import os
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED
from flashtext import KeywordProcessor
keyword_processor = KeywordProcessor()
FOLDER = "D:\\WorkHomeMac\\projectCode\\vx-station"
MAX_WORKERS = 30
# 读文件内容
def read_file(file):
with open(file, encoding='UTF-8') as f:
read_all = f.read()
f.close()
return read_all
# 写内容到文件
def rewrite_file(file, data):
with open(file, 'w', encoding='UTF-8') as f:
f.write(data)
f.close()
executor = ThreadPoolExecutor(max_workers=MAX_WORKERS)
all_task = []
def get_filelist(dir):
for home, dirs, files in os.walk(dir):
if home.find("build") > 0:
continue
# print(home)
if home.find("src\\main\\java") > 0 or home.endswith("layout") or home.endswith("layout-port"):
# if home.endswith("\\layout") or home.endswith("\\layout-port"):
# if home.find("\\_2C\\src\\p_") > 0:
if home == dir:
continue
print(home)
for filename in files:
if not (filename.endswith(".java") or filename.endswith(".xml")):
continue
# print(filename)
fullname = os.path.join(home, filename)
print(fullname)
all_task.append(executor.submit(replace_file, fullname))
pass
def replace_file(fullname):
print(fullname)
content = read_file(fullname)
new_content = keyword_processor.replace_keywords(content)
rewrite_file(fullname, new_content)
pass
if __name__ == '__main__':
with open("androidx-class-mapping.csv", "r", encoding="utf-8") as csvfile:
read = csv.reader(csvfile)
for class_mapping in read:
keyword_processor.add_keyword(class_mapping[0], class_mapping[1])
get_filelist(FOLDER)
wait(all_task, return_when=ALL_COMPLETED)
pass