Autolayout
自动化转换smallestWidth
适配解决方案gradle脚本代码如果我们的项目以前使用的是鸿洋大神的
Autolayout
现在因为这个适配方案停止维护的原因,我们期望将这个框架转换为smallestWidth
适配方案,那么,是是不是还在考虑使用人工的方式一个文件一个文件的方式装换呢,,效率太低这个转的脚本或许会解决实际问题
下面是脚本的实际代码,你只需要考本这个脚本代码到项目下的任意位置,然后创建名字为
codeSwitchAutolayout2smallestWidth.gradle
的文件,如下图所示,配置需要执行动作的根目录,点击执行gradle任务即可
如果你对这个脚本比较感兴趣,或者希望扩展这个脚本可以看看我以前的文章
Android自动化构建——通往大神的毕竟之路
当然,你也可以下载完整Demo体验一把
下载完整演示代码,下载完整演示代码,下载完整演示代码
注意
任务codeReplacePx2qb_xml
是将xml布局文件代码中的 "px"
转换为 "@dimen/qb_px_"
,你需要替换 file 的路径为项目中的layout路径。
任务codeReplaceAtolayout_java
是将java代码文件代码中的 "import com.zhy.autolayout.AutoRelativeLayout;"、"import com.zhy.autolayout.AutoLinearLayout;"、"import com.zhy.autolayout.AutoFrameLayout;"
分别转换为 ", "import android.widget.RelativeLayout;"、"import android.widget.LinearLayout;"、"import android.widget.FrameLayout;"
,你需要替换 file 的路径为项目中的java代码路径。
task codeReplacePx2qb_xml(group: "recode", description: "代码替换脚本将xml中的px替换为qb_px") {
doFirst {
def fileNum = 0
def folderNum = 0
def fileSuccessNum = 0
def fileFailNum = 0
println "脚本开始执行"
File file = new File("G:\\house\\app\\android\\Demo\\app\\src\\main\\res\\layout")
if (file.exists()) {
LinkedList list = new LinkedList()
//组装数据
if (file.isDirectory()) {
folderNum++
for (File file2 : file.listFiles()) {
if (file2.isDirectory()) {
folderNum++
for (File file3 : file2.listFiles()) {
folderNum++
if (file3.isDirectory()) {
folderNum++
for (File file4 : file3.listFiles()) {
if (file4.isDirectory()) {
folderNum++
} else {
list.add(file4)
}
}
} else {
list.add(file3)
}
}
} else {
list.add(file2)
}
}
} else {
list.add(file)
}
for (File fileOperate : list) {
if (fileOperate.isDirectory()) {
System.out.println("文件夹:" + fileOperate.getAbsolutePath() + "\n\n\n");
folderNum++
} else {
fileNum++
System.out.println("正在替换文件:" + fileOperate.getAbsolutePath())
if (fileOperate.getAbsolutePath().toLowerCase().endsWith(".xml")) {
LinkedList lins = new LinkedList();
try {
FileInputStream fis = new FileInputStream(fileOperate.getAbsolutePath().toString());
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
String content = line
if (content != null && content.length() > 0) {
content = content.replaceAll("com.zhy.autolayout.Auto", "")
if (content.matches(".*=\"\\d+px\".*") && content.contains("px\"")) {
String temp1 = content.find("\"\\d+px\"")
System.out.println("-----------需要替换的变量--------\n" + temp1)
String result = "\"@dimen/qb_px_" + temp1.find("\\d+") + "\""
System.out.println("-----------替换后的变量--------\n" + result)
if (!temp1.equals("1px") && !temp1.equals("0px")) {
content = content.replace(temp1, result)
System.out.println("----------替换后的结果---------\n" + content + "\n")
}
System.out.println("-------------------\n" + content)
}
lins.add(content)
} else {
lins.add(content)
}
}
fileOperate.withWriter('UTF-8') { writer ->
lins.each { lineTemp ->
writer.writeLine(lineTemp)
}
}
System.out.println("----------执行成功---------\n" + fileOperate.getAbsolutePath())
fileSuccessNum++
} catch (IOException e) {
System.out.println("-------------------\n" + e.toString())
System.out.println("----------执行成功---------\n" + fileOperate.getAbsolutePath())
fileFailNum++
e.printStackTrace();
}
}
}
}
} else {
System.out.println("文件不存在!");
}
System.out.println("文件夹共有:" + folderNum + ",文件共有:" + fileNum + ",执行成功的文件有" + fileSuccessNum + ",执行失败的文件有" + fileFailNum);
}
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
task codeReplaceAtolayout_java(group: "recode", description: "代码替换脚本") {
doFirst {
def fileNum = 0
def folderNum = 0
def fileSuccessNum = 0
def fileFailNum = 0
println "脚本开始执行"
File file = new File("G:\\house\\app\\android\\Demo\\app\\src\\main\\java\\com\\zbc\\house\\customview")
if (file.exists()) {
LinkedList list = new LinkedList()
//组装数据
if (file.isDirectory()) {
folderNum++
for (File file2 : file.listFiles()) {
if (file2.isDirectory()) {
folderNum++
for (File file3 : file2.listFiles()) {
folderNum++
if (file3.isDirectory()) {
folderNum++
for (File file4 : file3.listFiles()) {
if (file4.isDirectory()) {
folderNum++
} else {
list.add(file4)
}
}
} else {
list.add(file3)
}
}
} else {
list.add(file2)
}
}
} else {
list.add(file)
}
for (File fileOperate : list) {
if (fileOperate.exists()) {
fileNum++
System.out.println("正在替换文件:" + fileOperate.getAbsolutePath());
if (fileOperate.getAbsolutePath().toLowerCase().endsWith(".java")) {
LinkedList lins = new LinkedList();
try {
FileInputStream fis = new FileInputStream(fileOperate.getAbsolutePath().toString());
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
String content = line
if (content != null && content.length() > 0) {
// System.out.println("------------替换前-------\n" + content)
content = content.replaceAll("import com.zhy.autolayout.AutoRelativeLayout;", "import android.widget.RelativeLayout;")
content = content.replaceAll("import com.zhy.autolayout.AutoLinearLayout;", "import android.widget.LinearLayout;")
content = content.replaceAll("import com.zhy.autolayout.AutoFrameLayout;", "import android.widget.FrameLayout;")
content = content.replaceAll("AutoRelativeLayout", "RelativeLayout")
content = content.replaceAll("AutoLinearLayout", "LinearLayout")
content = content.replaceAll("AutoFrameLayout", "FrameLayout")
lins.add(content)
} else {
lins.add(content)
}
}
fileOperate.withWriter('UTF-8') { writer ->
lins.each { lineTemp ->
writer.writeLine(lineTemp)
}
}
System.out.println("----------执行成功---------\n" + fileOperate.getAbsolutePath())
fileSuccessNum++
} catch (IOException e) {
fileFailNum++
System.out.println("-------------------\n" + e.toString())
System.out.println("----------执行失败---------\n" + fileTemp.getAbsolutePath())
e.printStackTrace();
}
}
}
}
System.out.println("文件夹共有:" + folderNum + ",文件共有:" + fileNum + ",执行成功的文件有" + fileSuccessNum + ",执行失败的文件有" + fileFailNum);
}else {
System.out.println("文件不存在" )
}
}
}