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
4ff48c04
Commit
4ff48c04
authored
Aug 01, 2022
by
pengjunjing
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:支持viewBinding代码检测
parent
1ddde7b6
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
15 deletions
+43
-15
ParseUtil.kt
src/main/java/com/laihua/projecthelper/unuse/ParseUtil.kt
+40
-13
UnuseRes.kt
src/main/java/com/laihua/projecthelper/unuse/UnuseRes.kt
+3
-2
No files found.
src/main/java/com/laihua/projecthelper/unuse/ParseUtil.kt
View file @
4ff48c04
...
@@ -11,17 +11,21 @@ import java.io.FileReader
...
@@ -11,17 +11,21 @@ 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"
,
"R.raw"
,
"R.raw"
,
"R.layout"
,
"R.layout"
,
"@layout"
,
"@layout"
,
"@color"
,
"@color"
,
"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
{
...
@@ -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
])
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
])
useSet
.
add
(
readImageRes
[
i
])
// println("检测到了viewBinding代码, ${parseFileLine} 使用了资源:${resFileName}")
continue
@inFor
continue
@inFor
}
}
}
}
}
}
}
}
...
...
src/main/java/com/laihua/projecthelper/unuse/UnuseRes.kt
View file @
4ff48c04
...
@@ -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}"
)
...
...
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