用Python 快速一键迁移至AndroidX

13 篇文章 0 订阅
5 篇文章 0 订阅

用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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值