Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
R
RemoveUnusedImg
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
pengjunjing
RemoveUnusedImg
Commits
75fe49b2
Commit
75fe49b2
authored
Mar 04, 2022
by
pengjunjing
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
初次提交
parents
Pipeline
#15476
canceled with stages
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
209 additions
and
0 deletions
+209
-0
.gitignore
.gitignore
+2
-0
build.gradle
build.gradle
+10
-0
ParseUtil.kt
src/main/java/com/laihua/projecthelper/ParseUtil.kt
+77
-0
ReadUtil.kt
src/main/java/com/laihua/projecthelper/ReadUtil.kt
+79
-0
UnuseRes.kt
src/main/java/com/laihua/projecthelper/UnuseRes.kt
+41
-0
No files found.
.gitignore
0 → 100644
View file @
75fe49b2
/build
\ No newline at end of file
build.gradle
0 → 100644
View file @
75fe49b2
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
src/main/java/com/laihua/projecthelper/ParseUtil.kt
0 → 100644
View file @
75fe49b2
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
src/main/java/com/laihua/projecthelper/ReadUtil.kt
0 → 100644
View file @
75fe49b2
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
src/main/java/com/laihua/projecthelper/UnuseRes.kt
0 → 100644
View file @
75fe49b2
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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment