Commit 4ff48c04 authored by pengjunjing's avatar pengjunjing

feat:支持viewBinding代码检测

parent 1ddde7b6
......@@ -11,17 +11,21 @@ import java.io.FileReader
class ParseUtil {
//引用的代码,xml引用,java kt代码引用,布局文件
private val refCode =
listOf<String>("@drawable",
"@mipmap",
"R.drawable",
"R.mipmap",
"R.raw",
"R.layout",
"@layout",
"@color",
"R.anim",
"@anim")
private val refCode = listOf<String>("@drawable",
"@mipmap",
"R.drawable",
"R.mipmap",
"R.raw",
"R.layout",
"@layout",
"@color",
"R.anim",
"@anim")
/**
* 支持viewBinding
*/
private val bindingCode = "Binding"
//代码引用方式
private val refCallCode: List<String> = refCode.map {
......@@ -49,8 +53,14 @@ class ParseUtil {
for (s in refCode) {
if (readLine.contains(s)) {
result.add(readLine)
continue
}
}
//viewBind的代码
if (readLine.contains(bindingCode)) {
result.add(readLine)
continue
}
}
return result
......@@ -82,16 +92,33 @@ class ParseUtil {
//有包含资源的代码行
outFor@ for (i in fileNames.indices) {
inFor@ for (parseFileLine: String in parseFileLines) {
val resFileName = fileNames[i]
//资源引用的方式
for (s in refCallCode) {
//拼接成 @mipmap/xxx R.layout.xxx 的形式
val s1 = s + fileNames[i]
val s1 = s + resFileName
if (parseFileLine.contains(s1)) {
//有在使用该资源
use.add(fileNames[i])
use.add(resFileName)
useSet.add(readImageRes[i])
continue@inFor
}
}
//检查viewBinding引用
//资源完整文件名,包含后缀
val resFileFullName = readImageRes[i]
if (resFileFullName.name.endsWith(".xml", true) && parseFileLine.contains(bindingCode)) {
// 去除下划线,拼接成格式: KtAccountActivityBindPhoneBinding 小写
val replace = resFileName.replace("_", "") + bindingCode
if (parseFileLine.contains(replace, true)) {
//有在使用该资源
use.add(resFileName)
useSet.add(readImageRes[i])
// println("检测到了viewBinding代码, ${parseFileLine} 使用了资源:${resFileName}")
continue@inFor
}
}
}
}
......
......@@ -8,7 +8,8 @@ import java.io.File
const val realDelFile = false
/**
* 未使用资源删除工具,多运行几次,直到结果数量连续几次为0个
* 未使用资源删除工具,多运行几次,直到结果数量连续几次为0个才算真正清除.
* 因为可能存在xml引用了图片一类的文件,先要把xml删除,再检测图片.目前没有依赖传递
*/
fun main(args: Array<String>) {
println("开始读取文件")
......@@ -32,7 +33,7 @@ fun main(args: Array<String>) {
println("读取到代码资源数量:${readCodeFile.size},占用空间:${lengthMb}")
val unusedSet = ParseUtil().startParseUnused(readImageRes, readCodeFile)
val unusedSetMbRes = calcSize(readImageRes)
val unusedSetMbRes = calcSize(unusedSet.toMutableList())
println("未使用的资源总数: ${unusedSet.size} 占用空间:${unusedSetMbRes}")
unusedSet.forEach {
println("未使用的资源: ${it.name}")
......
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