Commit e2952478 authored by pengjunjing's avatar pengjunjing

feat:添加xml文件的清理功能

parent da3b771c
......@@ -25,6 +25,9 @@ class ParseUtil {
|| readLine.contains("R.drawable")//java kt代码引用
|| readLine.contains("R.mipmap")
|| readLine.contains("R.raw")
|| readLine.contains("R.layout")//布局文件
|| readLine.contains("@layout")
|| readLine.contains("@color")
) {
result.add(readLine)
}
......@@ -36,7 +39,7 @@ class ParseUtil {
/**
* 开始解析未使用的资源文件
*/
fun startParseUnused(readImageRes: MutableList<File>, readCodeFile: MutableList<File>): MutableSet<File> {
fun startParseUnused(readImageRes: List<File>, readCodeFile: List<File>): MutableSet<File> {
val fileNames = Array<String>(readImageRes.size) {
//文件名 ,带后缀,去除后缀
val name = readImageRes[it].name
......
......@@ -8,11 +8,10 @@ import java.io.File
* Description:
*/
class ReadUtil {
//存储结果
private val resResult = mutableListOf<File>()
//读取图片资源
fun readImageRes(file: File): MutableList<File> {
val resResult = mutableListOf<File>()
resResult.clear()
if (file.exists() && file.isDirectory) {
readChildFile(file, ".gif", ".png", ".jpg", ".webp", ".9", resultList = resResult)
......@@ -23,11 +22,11 @@ class ReadUtil {
}
//代码存储结果
private val codeResult = mutableListOf<File>()
/**
* 读取代码文件
*/
fun readCodeFile(file: File): MutableList<File> {
val codeResult = mutableListOf<File>()
codeResult.clear()
if (file.exists() && file.isDirectory) {
readChildFile(file, ".java", ".kt", ".xml", resultList = codeResult)
......@@ -37,10 +36,36 @@ class ReadUtil {
return codeResult
}
private val excludeXml = arrayOf("values", "AndroidManifest")
/**
* 读取xml图片文件
*/
fun readXmlFile(file: File): List<File> {
val result = mutableListOf<File>()
if (file.exists() && file.isDirectory) {
readChildFile(file, ".xml", resultList = result)
} else {
println("资源目录不存在或者传入的不是目录:${file.absolutePath}")
}
return result.filter {
var filterResult = true
for (exclude in excludeXml) {
//排除掉部分xml文件
if (it.absolutePath.contains(exclude,ignoreCase = true)) {
filterResult = false
break
}
}
filterResult
}
}
companion object {
//排除目录
private val excludeDir = arrayOf("build", ".git", ".gradle",".idea")
private val excludeDir = arrayOf("build", ".git", ".gradle", ".idea")
/**
* @param fileType 文件后缀名
......@@ -73,6 +98,4 @@ class ReadUtil {
}
}
\ No newline at end of file
......@@ -10,6 +10,7 @@ fun main(args: Array<String>) {
println(projectRoot.absolutePath)
//要检查资源的目录
val baseFile = File("${projectRoot.absolutePath}${File.separator}laihuaBase${File.separator}src${File.separator}")
//从项目根目录开始查找
// val baseFile = File("${projectRoot.absolutePath}")
println(baseFile.absolutePath + "\n")
val readImageRes = ReadUtil().readImageRes(baseFile)
......@@ -30,6 +31,17 @@ fun main(args: Array<String>) {
//确认要删除资源打开下一行代码,注释掉避免误删除
// it.deleteOnExit()
}
//检查没使用的图片xml/布局xml文件
val readImageXml = ReadUtil().readXmlFile(baseFile)
val startParseUnused = ParseUtil().startParseUnused(readImageXml, readCodeFile)
println("未使用的xml资源总数: ${startParseUnused.size}")
startParseUnused.forEach {
println("未使用的xml资源: ${it.absolutePath}")
//确认要删除资源打开下一行代码,注释掉避免误删除
// it.deleteOnExit()
}
}
private fun calcSize(readCodeFile: MutableList<File>): Float {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment