Commit 75fe49b2 authored by pengjunjing's avatar pengjunjing

初次提交

parents
Pipeline #15476 canceled with stages
/build
\ No newline at end of file
plugins {
id 'java-library'
id 'org.jetbrains.kotlin.jvm'
}
java {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}
\ No newline at end of file
package com.laihua.projecthelper
import java.io.File
import java.io.FileReader
import java.util.ArrayList
/**
* Author: pengjunjing
* Date: 2022/3/4
* Description:解析
*/
class ParseUtil {
/**
* 返回引用了图片资源的该行文本内容
*/
private fun parseFile(file: File): MutableList<String> {
val reader = FileReader(file)
val readLines = reader.readLines()
val result = mutableListOf<String>()
for (readLine in readLines) {
//判断该行是否引用了图片资源
if (readLine.contains("@drawable") ||//xml引用
readLine.contains("R.drawable")//java kt代码引用
) {
result.add(readLine)
}
}
return result
}
/**
* 开始解析未使用的资源文件
*/
fun startParseUnused(readImageRes: MutableList<File>, readCodeFile: MutableList<File>): MutableSet<File> {
val fileNames = Array<String>(readImageRes.size) {
//文件名 ,带后缀,去除后缀
val name = readImageRes[it].name
if (name.contains(".")) {
name.substring(0, name.indexOf("."))
} else {
name
}
}
val use = mutableListOf<String>()
val useSet = mutableSetOf<File>()
readCodeFile.forEach { file ->
val parseFileLines = parseFile(file)
if (parseFileLines.isNotEmpty()) {
//有包含资源的代码行
outFor@ for (i in fileNames.indices) {
inFor@ for (parseFileLine: String in parseFileLines) {
if (parseFileLine.contains(fileNames[i])) {
//有在使用该资源
use.add(fileNames[i])
useSet.add(readImageRes[i])
continue@inFor
}
}
}
}
}
val unusedSet = mutableSetOf<File>()
unusedSet.addAll(readImageRes)
unusedSet.removeAll(useSet)
return unusedSet
}
}
\ No newline at end of file
package com.laihua.projecthelper
import java.io.File
/**
* Author: pengjunjing
* Date: 2022/3/4
* Description:
*/
class ReadUtil {
//存储结果
private val resResult = mutableListOf<File>()
//读取图片资源
fun readImageRes(file: File): MutableList<File> {
resResult.clear()
if (file.exists() && file.isDirectory) {
readChildFile(file, ".gif", ".png", ".jpg", ".webp", ".9", resultList = resResult)
} else {
println("资源目录不存在或者传入的不是目录:${file.absolutePath}")
}
return resResult
}
//代码存储结果
private val codeResult = mutableListOf<File>()
fun readCodeFile(file: File): MutableList<File> {
codeResult.clear()
if (file.exists() && file.isDirectory) {
readChildFile(file, ".java", ".kt", ".xml", resultList = codeResult)
} else {
println("资源目录不存在或者传入的不是目录:${file.absolutePath}")
}
return codeResult
}
companion object {
//排除目录
private val excludeDir = arrayOf("build", ".git", ".gradle")
/**
* @param fileType 文件后缀名
*/
private fun readChildFile(file: File, vararg fileType: String, resultList: MutableList<File>) {
val listFiles = file.listFiles()
if (listFiles != null && listFiles.isNotEmpty()) {
//该目录下存在文件or文件夹
out@ for (it in listFiles) {
if (it.isDirectory) {
for (s in excludeDir) {
if (it.name.equals(s)) {
//排除指定目录
continue@out
}
}
//目录递归遍历
readChildFile(it, *fileType, resultList = resultList)
} else if (it.isFile) {
for (s in fileType) {
if (it.name.lowercase().endsWith(s.lowercase())) {
//文件后缀名匹配
resultList.add(it)
}
}
}
}
}
}
}
}
\ No newline at end of file
package com.laihua.projecthelper
import java.io.File
fun main(args: Array<String>) {
println("开始读取文件")
val projectRoot = File("")
val projectRootF = File(projectRoot.absolutePath)
println(projectRoot.absolutePath)
//要检查资源的目录
val baseFile = File("${projectRoot.absolutePath}${File.separator}laihuaBase${File.separator}src${File.separator}")
println(baseFile.absolutePath + "\n")
val readImageRes = ReadUtil().readImageRes(baseFile)
val readCodeFile = ReadUtil().readCodeFile(projectRootF)
val lengthMb = calcSize(readCodeFile)
val lengthMbRes = calcSize(readImageRes)
println("读取到图片资源数量:${readImageRes.size},占用空间:${lengthMbRes}")
println("读取到代码资源数量:${readCodeFile.size},占用空间:${lengthMb}")
val unusedSet = ParseUtil().startParseUnused(readImageRes, readCodeFile)
val unusedSetMbRes = calcSize(readImageRes)
println("未使用的资源总数: ${unusedSet.size} 占用空间:${unusedSetMbRes}")
unusedSet.forEach {
println("未使用的资源: ${it.name}")
//确认要删除资源打开下一行代码,注释掉避免误删除
// it.deleteOnExit()
}
}
private fun calcSize(readCodeFile: MutableList<File>): Float {
var totalLength = 0L
for (file in readCodeFile) {
totalLength += file.length()
}
return totalLength / 1024.0f / 1024
}
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