├── result.png ├── README.md └── migrate.py /result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yuweiguocn/MigrateToAndroidX/HEAD/result.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MigrateToAndroidX 2 | 3 | 用于帮助迁移到AndroidX。 4 | 5 | ## 前言 6 | 通过Android Studio提供的Migrate to AndroidX功能并不能很好地处理所有文件,所以才有了此脚本。原理:通过Android官方网站提供的类映射关系,扫描工程中所有文件进行替换。 7 | 8 | ## 使用 9 | 10 | 在gradle.properties文件中添加以下属性: 11 | ``` 12 | android.useAndroidX=true //表示启用androidx 13 | android.enableJetifier=true //会对依赖库进行迁移 14 | ``` 15 | 对工程进行Sync确保依赖库已经改为androidx。 16 | 17 | 打开终端在工程根目录执行以下命令: 18 | ``` 19 | git clone git@github.com:yuweiguocn/MigrateToAndroidX.git 20 | python MigrateToAndroidX/migrate.py 21 | ``` 22 | 运行结果: 23 | 24 | ![](./result.png) 25 | 26 | ## 不支持 27 | 28 | - 不支持依赖替换 29 | - 不支持多行导包替换 -------------------------------------------------------------------------------- /migrate.py: -------------------------------------------------------------------------------- 1 | import os 2 | import re 3 | import json 4 | 5 | curpath=os.getcwd() 6 | datapath=curpath+"/MigrateToAndroidX/source.html" 7 | count=0 8 | 9 | skips=["/.git/","/.gradle/","/.idea/","/build/generated/","/build/intermediates/","/build/kotlin/","/build/outputs/","/build/tmp/"] 10 | fileSuffix=[".java",".kt",".gradle",".xml",".pro",".txt",".cfg"] 11 | 12 | def replaceTag(name): 13 | name=name.replace("","") 14 | name=name.replace("","") 15 | return name 16 | 17 | def getMapData(): 18 | result=[] 19 | table={} 20 | for line in iter(open(datapath)): 21 | name=line.lstrip().rstrip() 22 | if(name.startswith("android.") or name.startswith("com.android.")): 23 | table={} 24 | table["from"]=replaceTag(name) 25 | elif(name.startswith("lifecycle") or name.startswith("core") or name.startswith("paging") or name.startswith("persistence")): 26 | table={} 27 | table["from"]="android.arch."+replaceTag(name) 28 | elif(name.startswith("androidx") or name.startswith("com.google")): 29 | table["to"]=replaceTag(name) 30 | result.append(table) 31 | return result 32 | 33 | def isSupportMigrate(path): 34 | for dir in skips: 35 | if(path.__contains__(dir)): 36 | return False 37 | for file in fileSuffix: 38 | if(path.__contains__(file)): 39 | return True 40 | return False 41 | 42 | def skipDir(path): 43 | for dir in skips: 44 | if(path.__contains__(dir)): 45 | return True 46 | return False 47 | 48 | 49 | def getAllFiles(path): 50 | files = [] 51 | dirs = os.listdir(path) 52 | for filepath in dirs: 53 | if (os.path.isdir(path+"/"+filepath) and not skipDir(path+"/"+filepath)): 54 | print("Scanning "+path+"/"+filepath) 55 | for file in getAllFiles(path+"/"+filepath): 56 | if(isSupportMigrate(file)): 57 | files.append(file) 58 | elif(isSupportMigrate(path+"/"+filepath)): 59 | files.append(path+"/"+filepath) 60 | return files 61 | 62 | def replaceAndroidX(line,configs): 63 | for config in configs: 64 | if(config.__contains__("from") and line.__contains__(config["from"])): 65 | line=line.replace(config["from"],config["to"]) 66 | return line 67 | 68 | 69 | def replaceSupport(file,configs): 70 | content="" 71 | needReWrite=False 72 | for line in iter(open(file)): 73 | if(line.__contains__("android.support") or line.__contains__("android.arch") or line.__contains__("android.databinding") or line.__contains__("android.test")): 74 | needReWrite=True 75 | content = content + replaceAndroidX(line,configs) 76 | else: 77 | content = content + line 78 | if(needReWrite): 79 | global count 80 | count+=1 81 | print("Migrating "+file) 82 | fo=open(file, "w") 83 | fo.write(content) 84 | fo.close 85 | 86 | 87 | def main(): 88 | configs=getMapData() 89 | # print(configs) 90 | configs.sort(key=lambda x: x['from'],reverse=True) 91 | print(configs) 92 | files=getAllFiles(curpath) 93 | for file in files: 94 | replaceSupport(file,configs) 95 | print("Total migrate files="+str(count)) 96 | main() --------------------------------------------------------------------------------