Commit 4ff48c04 authored by pengjunjing's avatar pengjunjing

feat:支持viewBinding代码检测

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