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
e2952478
Commit
e2952478
authored
Mar 07, 2022
by
pengjunjing
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:添加xml文件的清理功能
parent
da3b771c
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
10 deletions
+48
-10
ParseUtil.kt
src/main/java/com/laihua/projecthelper/ParseUtil.kt
+4
-1
ReadUtil.kt
src/main/java/com/laihua/projecthelper/ReadUtil.kt
+32
-9
UnuseRes.kt
src/main/java/com/laihua/projecthelper/UnuseRes.kt
+12
-0
No files found.
src/main/java/com/laihua/projecthelper/ParseUtil.kt
View file @
e2952478
...
...
@@ -25,6 +25,9 @@ class ParseUtil {
||
readLine
.
contains
(
"R.drawable"
)
//java kt代码引用
||
readLine
.
contains
(
"R.mipmap"
)
||
readLine
.
contains
(
"R.raw"
)
||
readLine
.
contains
(
"R.layout"
)
//布局文件
||
readLine
.
contains
(
"@layout"
)
||
readLine
.
contains
(
"@color"
)
)
{
result
.
add
(
readLine
)
}
...
...
@@ -36,7 +39,7 @@ class ParseUtil {
/**
* 开始解析未使用的资源文件
*/
fun
startParseUnused
(
readImageRes
:
MutableList
<
File
>,
readCodeFile
:
Mutable
List
<
File
>):
MutableSet
<
File
>
{
fun
startParseUnused
(
readImageRes
:
List
<
File
>,
readCodeFile
:
List
<
File
>):
MutableSet
<
File
>
{
val
fileNames
=
Array
<
String
>(
readImageRes
.
size
)
{
//文件名 ,带后缀,去除后缀
val
name
=
readImageRes
[
it
].
name
...
...
src/main/java/com/laihua/projecthelper/ReadUtil.kt
View file @
e2952478
...
...
@@ -8,11 +8,10 @@ import java.io.File
* Description:
*/
class
ReadUtil
{
//存储结果
private
val
resResult
=
mutableListOf
<
File
>()
//读取图片资源
fun
readImageRes
(
file
:
File
):
MutableList
<
File
>
{
val
resResult
=
mutableListOf
<
File
>()
resResult
.
clear
()
if
(
file
.
exists
()
&&
file
.
isDirectory
)
{
readChildFile
(
file
,
".gif"
,
".png"
,
".jpg"
,
".webp"
,
".9"
,
resultList
=
resResult
)
...
...
@@ -23,11 +22,11 @@ class ReadUtil {
}
//代码存储结果
private
val
codeResult
=
mutableListOf
<
File
>()
/**
* 读取代码文件
*/
fun
readCodeFile
(
file
:
File
):
MutableList
<
File
>
{
val
codeResult
=
mutableListOf
<
File
>()
codeResult
.
clear
()
if
(
file
.
exists
()
&&
file
.
isDirectory
)
{
readChildFile
(
file
,
".java"
,
".kt"
,
".xml"
,
resultList
=
codeResult
)
...
...
@@ -37,10 +36,36 @@ class ReadUtil {
return
codeResult
}
private
val
excludeXml
=
arrayOf
(
"values"
,
"AndroidManifest"
)
/**
* 读取xml图片文件
*/
fun
readXmlFile
(
file
:
File
):
List
<
File
>
{
val
result
=
mutableListOf
<
File
>()
if
(
file
.
exists
()
&&
file
.
isDirectory
)
{
readChildFile
(
file
,
".xml"
,
resultList
=
result
)
}
else
{
println
(
"资源目录不存在或者传入的不是目录:${file.absolutePath}"
)
}
return
result
.
filter
{
var
filterResult
=
true
for
(
exclude
in
excludeXml
)
{
//排除掉部分xml文件
if
(
it
.
absolutePath
.
contains
(
exclude
,
ignoreCase
=
true
))
{
filterResult
=
false
break
}
}
filterResult
}
}
companion
object
{
//排除目录
private
val
excludeDir
=
arrayOf
(
"build"
,
".git"
,
".gradle"
,
".idea"
)
private
val
excludeDir
=
arrayOf
(
"build"
,
".git"
,
".gradle"
,
".idea"
)
/**
* @param fileType 文件后缀名
...
...
@@ -73,6 +98,4 @@ class ReadUtil {
}
}
\ No newline at end of file
src/main/java/com/laihua/projecthelper/UnuseRes.kt
View file @
e2952478
...
...
@@ -10,6 +10,7 @@ fun main(args: Array<String>) {
println
(
projectRoot
.
absolutePath
)
//要检查资源的目录
val
baseFile
=
File
(
"${projectRoot.absolutePath}${File.separator}laihuaBase${File.separator}src${File.separator}"
)
//从项目根目录开始查找
// val baseFile = File("${projectRoot.absolutePath}")
println
(
baseFile
.
absolutePath
+
"\n"
)
val
readImageRes
=
ReadUtil
().
readImageRes
(
baseFile
)
...
...
@@ -30,6 +31,17 @@ fun main(args: Array<String>) {
//确认要删除资源打开下一行代码,注释掉避免误删除
// it.deleteOnExit()
}
//检查没使用的图片xml/布局xml文件
val
readImageXml
=
ReadUtil
().
readXmlFile
(
baseFile
)
val
startParseUnused
=
ParseUtil
().
startParseUnused
(
readImageXml
,
readCodeFile
)
println
(
"未使用的xml资源总数: ${startParseUnused.size}"
)
startParseUnused
.
forEach
{
println
(
"未使用的xml资源: ${it.absolutePath}"
)
//确认要删除资源打开下一行代码,注释掉避免误删除
// it.deleteOnExit()
}
}
private
fun
calcSize
(
readCodeFile
:
MutableList
<
File
>):
Float
{
...
...
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