Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DiDi
Booster
提交
c7e2bd97
B
Booster
项目概览
DiDi
/
Booster
通知
31
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
Booster
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c7e2bd97
编写于
5月 03, 2019
作者:
J
johnsonlee
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improve shrink transformer #8
上级
5d934449
变更
21
隐藏空白更改
内联
并排
Showing
21 changed file
with
198 addition
and
128 deletion
+198
-128
booster-android-gradle-api/src/main/kotlin/com/didiglobal/booster/gradle/TransformInvocation.kt
...tlin/com/didiglobal/booster/gradle/TransformInvocation.kt
+34
-1
booster-android-gradle-api/src/main/kotlin/com/didiglobal/booster/util/ComponentHandler.kt
...in/kotlin/com/didiglobal/booster/util/ComponentHandler.kt
+0
-0
booster-android-gradle-api/src/main/kotlin/com/didiglobal/booster/util/FileFinder.kt
...src/main/kotlin/com/didiglobal/booster/util/FileFinder.kt
+0
-9
booster-android-gradle-api/src/main/kotlin/com/didiglobal/booster/util/TextFormatter.kt
.../main/kotlin/com/didiglobal/booster/util/TextFormatter.kt
+0
-0
booster-gradle-base/README.md
booster-gradle-base/README.md
+0
-6
booster-gradle-base/build.gradle
booster-gradle-base/build.gradle
+0
-9
booster-gradle-plugin/build.gradle
booster-gradle-plugin/build.gradle
+0
-1
booster-gradle-plugin/src/main/kotlin/com/didiglobal/booster/gradle/BoosterTransformInvocation.kt
...m/didiglobal/booster/gradle/BoosterTransformInvocation.kt
+3
-0
booster-kotlinx/src/main/kotlin/com/didiglobal/booster/kotlinx/Wildcard.kt
...rc/main/kotlin/com/didiglobal/booster/kotlinx/Wildcard.kt
+2
-0
booster-kotlinx/src/main/kotlin/com/didiglobal/booster/kotlinx/concurrent.kt
.../main/kotlin/com/didiglobal/booster/kotlinx/concurrent.kt
+16
-0
booster-transform-lint/build.gradle
booster-transform-lint/build.gradle
+1
-1
booster-transform-shared-preferences/build.gradle
booster-transform-shared-preferences/build.gradle
+0
-1
booster-transform-shrink/README.md
booster-transform-shrink/README.md
+20
-0
booster-transform-shrink/build.gradle
booster-transform-shrink/build.gradle
+1
-1
booster-transform-shrink/src/main/kotlin/com/didiglobal/booster/transform/shrink/RCollector.kt
...lin/com/didiglobal/booster/transform/shrink/RCollector.kt
+0
-25
booster-transform-shrink/src/main/kotlin/com/didiglobal/booster/transform/shrink/ShrinkTransformer.kt
.../didiglobal/booster/transform/shrink/ShrinkTransformer.kt
+115
-70
booster-transform-spi/src/main/kotlin/com/didiglobal/booster/transform/TransformContext.kt
...tlin/com/didiglobal/booster/transform/TransformContext.kt
+5
-0
booster-transform-thread/build.gradle
booster-transform-thread/build.gradle
+0
-1
booster-transform-toast/build.gradle
booster-transform-toast/build.gradle
+0
-1
booster-transform-usage/build.gradle
booster-transform-usage/build.gradle
+1
-1
settings.gradle
settings.gradle
+0
-1
未找到文件。
booster-android-gradle-api/src/main/kotlin/com/didiglobal/booster/gradle/TransformInvocation.kt
浏览文件 @
c7e2bd97
...
@@ -5,10 +5,14 @@ import com.android.build.gradle.AppExtension
...
@@ -5,10 +5,14 @@ import com.android.build.gradle.AppExtension
import
com.android.build.gradle.BaseExtension
import
com.android.build.gradle.BaseExtension
import
com.android.build.gradle.LibraryExtension
import
com.android.build.gradle.LibraryExtension
import
com.android.build.gradle.api.BaseVariant
import
com.android.build.gradle.api.BaseVariant
import
com.didiglobal.booster.kotlinx.execute
import
com.didiglobal.booster.kotlinx.head
import
com.didiglobal.booster.util.FileFinder
import
org.gradle.api.Project
import
org.gradle.api.Project
import
org.gradle.api.internal.AbstractTask
import
org.gradle.api.internal.AbstractTask
import
org.objectweb.asm.ClassReader
import
org.objectweb.asm.tree.ClassNode
import
java.io.File
import
java.io.File
import
java.net.URLClassLoader
/**
/**
* Represents the booster transform for
* Represents the booster transform for
...
@@ -51,3 +55,32 @@ val TransformInvocation.compileClasspath: Collection<File>
...
@@ -51,3 +55,32 @@ val TransformInvocation.compileClasspath: Collection<File>
*/
*/
val
TransformInvocation
.
runtimeClasspath
:
Collection
<
File
>
val
TransformInvocation
.
runtimeClasspath
:
Collection
<
File
>
get
()
=
compileClasspath
+
project
.
getAndroid
<
BaseExtension
>().
bootClasspath
get
()
=
compileClasspath
+
project
.
getAndroid
<
BaseExtension
>().
bootClasspath
/**
* Returns the application id
*/
val
TransformInvocation
.
applicationId
:
String
get
()
{
val
packages
=
variant
.
scope
.
symbolListWithPackageName
.
filter
{
it
.
length
()
>
0
}.
map
{
it
.
head
()
!!
}.
toSet
()
return
variant
.
scope
.
javac
.
map
{
classes
->
val
base
=
classes
.
toURI
()
FileFinder
(
classes
)
{
file
->
file
.
name
==
"BuildConfig.class"
&&
file
.
inputStream
().
use
{
bytecode
->
ClassNode
().
also
{
klass
->
ClassReader
(
bytecode
).
accept
(
klass
,
0
)
}.
fields
.
any
{
it
.
name
==
"APPLICATION_ID"
&&
it
.
desc
==
"Ljava/lang/String;"
&&
packages
.
contains
(
it
.
value
)
}
}
}.
execute
().
map
{
base
.
relativize
(
it
.
toURI
()).
path
.
let
{
path
->
path
.
substring
(
0
,
path
.
lastIndexOf
(
'/'
))
}.
replace
(
'/'
,
'.'
)
}.
toSet
()
}.
flatten
().
single
()
}
booster-
gradle-base
/src/main/kotlin/com/didiglobal/booster/util/ComponentHandler.kt
→
booster-
android-gradle-api
/src/main/kotlin/com/didiglobal/booster/util/ComponentHandler.kt
浏览文件 @
c7e2bd97
文件已移动
booster-
gradle-base
/src/main/kotlin/com/didiglobal/booster/util/FileFinder.kt
→
booster-
android-gradle-api
/src/main/kotlin/com/didiglobal/booster/util/FileFinder.kt
浏览文件 @
c7e2bd97
...
@@ -36,13 +36,4 @@ class FileFinder(private val roots: Collection<File>, private val filter: (File)
...
@@ -36,13 +36,4 @@ class FileFinder(private val roots: Collection<File>, private val filter: (File)
return
result
+
tasks
.
flatMap
{
it
.
join
()
}
return
result
+
tasks
.
flatMap
{
it
.
join
()
}
}
}
fun
execute
():
Collection
<
File
>
{
val
pool
=
ForkJoinPool
()
try
{
return
pool
.
invoke
(
this
)
}
finally
{
pool
.
shutdown
()
}
}
}
}
booster-
gradle-base
/src/main/kotlin/com/didiglobal/booster/util/TextFormatter.kt
→
booster-
android-gradle-api
/src/main/kotlin/com/didiglobal/booster/util/TextFormatter.kt
浏览文件 @
c7e2bd97
文件已移动
booster-gradle-base/README.md
已删除
100644 → 0
浏览文件 @
5d934449
# booster-gradle-base
This module provides utilities based on Android gradle plugin.
本模块提供了基于 Android gradle 插件的工具类。
booster-gradle-base/build.gradle
已删除
100644 → 0
浏览文件 @
5d934449
apply
from:
'../gradle/booster.gradle'
dependencies
{
compile
gradleApi
()
compile
project
(
':booster-kotlinx'
)
compile
'com.google.auto.service:auto-service:1.0-rc4'
compileOnly
'com.android.tools.build:gradle:3.0.0'
testCompileOnly
'com.android.tools.build:gradle:3.0.0'
}
booster-gradle-plugin/build.gradle
浏览文件 @
c7e2bd97
...
@@ -3,7 +3,6 @@ apply from: '../gradle/booster.gradle'
...
@@ -3,7 +3,6 @@ apply from: '../gradle/booster.gradle'
dependencies
{
dependencies
{
kapt
'com.google.auto.service:auto-service:1.0-rc4'
kapt
'com.google.auto.service:auto-service:1.0-rc4'
compile
project
(
':booster-android-gradle-api'
)
compile
project
(
':booster-android-gradle-api'
)
compile
project
(
':booster-gradle-base'
)
compile
project
(
':booster-task-spi'
)
compile
project
(
':booster-task-spi'
)
compile
project
(
':booster-transform-spi'
)
compile
project
(
':booster-transform-spi'
)
compile
project
(
':booster-transform-util'
)
compile
project
(
':booster-transform-util'
)
...
...
booster-gradle-plugin/src/main/kotlin/com/didiglobal/booster/gradle/BoosterTransformInvocation.kt
浏览文件 @
c7e2bd97
...
@@ -15,6 +15,7 @@ import com.android.build.gradle.internal.publishing.AndroidArtifacts.ArtifactSco
...
@@ -15,6 +15,7 @@ import com.android.build.gradle.internal.publishing.AndroidArtifacts.ArtifactSco
import
com.android.build.gradle.internal.publishing.AndroidArtifacts.ArtifactType.AAR
import
com.android.build.gradle.internal.publishing.AndroidArtifacts.ArtifactType.AAR
import
com.android.build.gradle.internal.publishing.AndroidArtifacts.ArtifactType.JAR
import
com.android.build.gradle.internal.publishing.AndroidArtifacts.ArtifactType.JAR
import
com.android.build.gradle.internal.publishing.AndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH
import
com.android.build.gradle.internal.publishing.AndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH
import
com.didiglobal.booster.kotlinx.execute
import
com.didiglobal.booster.kotlinx.ifNotEmpty
import
com.didiglobal.booster.kotlinx.ifNotEmpty
import
com.didiglobal.booster.transform.ArtifactManager
import
com.didiglobal.booster.transform.ArtifactManager
import
com.didiglobal.booster.transform.Klass
import
com.didiglobal.booster.transform.Klass
...
@@ -61,6 +62,8 @@ internal class BoosterTransformInvocation(private val delegate: TransformInvocat
...
@@ -61,6 +62,8 @@ internal class BoosterTransformInvocation(private val delegate: TransformInvocat
override
val
klassPool
=
KlassPoolImpl
(
runtimeClasspath
)
override
val
klassPool
=
KlassPoolImpl
(
runtimeClasspath
)
override
val
applicationId
=
delegate
.
applicationId
override
fun
hasProperty
(
name
:
String
):
Boolean
{
override
fun
hasProperty
(
name
:
String
):
Boolean
{
return
project
.
hasProperty
(
name
)
return
project
.
hasProperty
(
name
)
}
}
...
...
booster-kotlinx/src/main/kotlin/com/didiglobal/booster/kotlinx/Wildcard.kt
浏览文件 @
c7e2bd97
...
@@ -113,6 +113,8 @@ class Wildcard(private val pattern: String, private val ignoreCase: Boolean = fa
...
@@ -113,6 +113,8 @@ class Wildcard(private val pattern: String, private val ignoreCase: Boolean = fa
else
->
false
else
->
false
}
}
override
fun
toString
()
=
this
.
pattern
private
fun
checkIndexOf
(
str
:
String
,
strStartIndex
:
Int
,
search
:
String
):
Int
{
private
fun
checkIndexOf
(
str
:
String
,
strStartIndex
:
Int
,
search
:
String
):
Int
{
val
endIndex
=
str
.
length
-
search
.
length
val
endIndex
=
str
.
length
-
search
.
length
if
(
endIndex
>=
strStartIndex
)
{
if
(
endIndex
>=
strStartIndex
)
{
...
...
booster-kotlinx/src/main/kotlin/com/didiglobal/booster/kotlinx/concurrent.kt
0 → 100644
浏览文件 @
c7e2bd97
package
com.didiglobal.booster.kotlinx
import
java.util.concurrent.ForkJoinPool
import
java.util.concurrent.ForkJoinTask
/**
* Execute this task
*
* @author johnsonlee
*/
fun
<
T
>
ForkJoinTask
<
T
>.
execute
():
T
{
val
pool
=
ForkJoinPool
()
val
result
=
pool
.
invoke
(
this
)
pool
.
shutdown
()
return
result
}
booster-transform-lint/build.gradle
浏览文件 @
c7e2bd97
...
@@ -2,7 +2,7 @@ apply from: '../gradle/booster.gradle'
...
@@ -2,7 +2,7 @@ apply from: '../gradle/booster.gradle'
dependencies
{
dependencies
{
kapt
"com.google.auto.service:auto-service:1.0-rc4"
kapt
"com.google.auto.service:auto-service:1.0-rc4"
implementation
project
(
':booster-
gradle-base
'
)
implementation
project
(
':booster-
android-gradle-api
'
)
implementation
project
(
':booster-transform-asm'
)
implementation
project
(
':booster-transform-asm'
)
implementation
project
(
':booster-transform-util'
)
implementation
project
(
':booster-transform-util'
)
}
}
booster-transform-shared-preferences/build.gradle
浏览文件 @
c7e2bd97
...
@@ -3,7 +3,6 @@ apply from: '../gradle/booster.gradle'
...
@@ -3,7 +3,6 @@ apply from: '../gradle/booster.gradle'
dependencies
{
dependencies
{
kapt
"com.google.auto.service:auto-service:1.0-rc4"
kapt
"com.google.auto.service:auto-service:1.0-rc4"
implementation
project
(
':booster-android-gradle-api'
)
implementation
project
(
':booster-android-gradle-api'
)
implementation
project
(
':booster-gradle-base'
)
implementation
project
(
':booster-task-spi'
)
implementation
project
(
':booster-task-spi'
)
implementation
project
(
':booster-transform-asm'
)
implementation
project
(
':booster-transform-asm'
)
compileOnly
'com.android.tools.build:gradle:3.0.0'
compileOnly
'com.android.tools.build:gradle:3.0.0'
...
...
booster-transform-shrink/README.md
浏览文件 @
c7e2bd97
...
@@ -2,3 +2,23 @@
...
@@ -2,3 +2,23 @@
This module is used for constants shrinking, such as fields in
`BuildConfig`
,
`R$id`
,
`R$layout`
,
`R$string`
, etc.
This module is used for constants shrinking, such as fields in
`BuildConfig`
,
`R$id`
,
`R$layout`
,
`R$string`
, etc.
## Properties
The following table shows the properties that transformer supports:
| Property | Description | Example |
| -------------------------------- | ------------------------------------------------------------ | ---------------------------------- |
|
`booster.transform.shrink.ignores`
| comma separated wildcard patterns to ignore | android/
\*
,androidx/
\*
|
The properties can be passthrough the command line as following:
```
bash
./gradlew assembleDebug
-Pbooster
.transform.shrink.ignores
=
android/
*
,androidx/
*
```
or configured in the
`gradle.properties`
:
```
properties
booster.transform.shrink.ignores
=
android/*,androidx/*
```
booster-transform-shrink/build.gradle
浏览文件 @
c7e2bd97
...
@@ -3,6 +3,6 @@ apply from: '../gradle/booster.gradle'
...
@@ -3,6 +3,6 @@ apply from: '../gradle/booster.gradle'
dependencies
{
dependencies
{
kapt
"com.google.auto.service:auto-service:1.0-rc4"
kapt
"com.google.auto.service:auto-service:1.0-rc4"
implementation
project
(
':booster-aapt2'
)
implementation
project
(
':booster-aapt2'
)
implementation
project
(
':booster-
gradle-base
'
)
implementation
project
(
':booster-
android-gradle-api
'
)
implementation
project
(
':booster-transform-asm'
)
implementation
project
(
':booster-transform-asm'
)
}
}
booster-transform-shrink/src/main/kotlin/com/didiglobal/booster/transform/shrink/RCollector.kt
已删除
100644 → 0
浏览文件 @
5d934449
package
com.didiglobal.booster.transform.shrink
import
java.io.File
import
java.util.concurrent.RecursiveTask
internal
class
RCollector
(
private
val
root
:
File
)
:
RecursiveTask
<
Collection
<
File
>>()
{
override
fun
compute
():
Collection
<
File
>
{
val
tasks
=
mutableListOf
<
RecursiveTask
<
Collection
<
File
>>>()
val
result
=
mutableListOf
<
File
>()
root
.
listFiles
()
?.
forEach
{
file
->
if
(
file
.
isDirectory
)
{
RCollector
(
file
).
also
{
task
->
tasks
.
add
(
task
)
}.
fork
()
}
else
if
(
"R.class"
==
file
.
name
||
(
file
.
name
.
startsWith
(
"R$"
)
&&
file
.
name
.
endsWith
(
".class"
)))
{
result
.
add
(
file
)
}
}
return
result
+
tasks
.
flatMap
{
it
.
join
()
}
}
}
booster-transform-shrink/src/main/kotlin/com/didiglobal/booster/transform/shrink/ShrinkTransformer.kt
浏览文件 @
c7e2bd97
package
com.didiglobal.booster.transform.shrink
package
com.didiglobal.booster.transform.shrink
import
com.didiglobal.booster.kotlinx.BLUE
import
com.didiglobal.booster.kotlinx.Wildcard
import
com.didiglobal.booster.kotlinx.RED
import
com.didiglobal.booster.kotlinx.RESET
import
com.didiglobal.booster.kotlinx.YELLOW
import
com.didiglobal.booster.kotlinx.asIterable
import
com.didiglobal.booster.kotlinx.asIterable
import
com.didiglobal.booster.kotlinx.head
import
com.didiglobal.booster.kotlinx.execute
import
com.didiglobal.booster.kotlinx.separatorsToSystem
import
com.didiglobal.booster.kotlinx.file
import
com.didiglobal.booster.kotlinx.ifNotEmpty
import
com.didiglobal.booster.kotlinx.touch
import
com.didiglobal.booster.transform.ArtifactManager.Companion.JAVAC
import
com.didiglobal.booster.transform.ArtifactManager.Companion.JAVAC
import
com.didiglobal.booster.transform.ArtifactManager.Companion.MERGED_RES
import
com.didiglobal.booster.transform.ArtifactManager.Companion.MERGED_RES
import
com.didiglobal.booster.transform.ArtifactManager.Companion.SYMBOL_LIST
import
com.didiglobal.booster.transform.ArtifactManager.Companion.SYMBOL_LIST
import
com.didiglobal.booster.transform.ArtifactManager.Companion.SYMBOL_LIST_WITH_PACKAGE_NAME
import
com.didiglobal.booster.transform.TransformContext
import
com.didiglobal.booster.transform.TransformContext
import
com.didiglobal.booster.transform.asm.ClassTransformer
import
com.didiglobal.booster.transform.asm.ClassTransformer
import
com.didiglobal.booster.transform.asm.simpleName
import
com.didiglobal.booster.transform.asm.simpleName
import
com.didiglobal.booster.util.FileFinder
import
com.google.auto.service.AutoService
import
com.google.auto.service.AutoService
import
org.gradle.api.logging.Logging
import
org.objectweb.asm.Opcodes.ACC_FINAL
import
org.objectweb.asm.Opcodes
import
org.objectweb.asm.Opcodes.ACC_STATIC
import
org.objectweb.asm.Opcodes.GETSTATIC
import
org.objectweb.asm.tree.ClassNode
import
org.objectweb.asm.tree.ClassNode
import
org.objectweb.asm.tree.FieldInsnNode
import
org.objectweb.asm.tree.FieldInsnNode
import
org.objectweb.asm.tree.FieldNode
import
org.objectweb.asm.tree.FieldNode
import
org.objectweb.asm.tree.LdcInsnNode
import
org.objectweb.asm.tree.LdcInsnNode
import
java.util.concurrent.ForkJoinPool
import
java.io.File
import
java.io.PrintWriter
internal
const
val
R_STYLEABLE
=
"R\$styleable"
internal
const
val
R_STYLEABLE
=
"R\$styleable"
internal
const
val
R_STYLEABLE_CLASS
=
"$R_STYLEABLE.class"
internal
const
val
R_STYLEABLE_CLASS
=
"$R_STYLEABLE.class"
internal
const
val
ANDROID_R
=
"android/R$"
internal
const
val
ANDROID_R
=
"android/R$"
internal
const
val
COM_ANDROID_INTERNAL_R
=
"com/android/internal/R$"
internal
const
val
COM_ANDROID_INTERNAL_R
=
"com/android/internal/R$"
internal
val
CONST_TYPE_SIGNATURES
=
setOf
(
"Z"
,
"B"
,
"C"
,
"S"
,
"I"
,
"J"
,
"F"
,
"D"
,
"L/java/lang/String;"
)
private
val
logger
=
Logging
.
getLogger
(
ShrinkTransformer
::
class
.
java
)
/**
/**
* Represents a class node transformer for constants shrinking
* Represents a class node transformer for constants shrinking
*
*
...
@@ -40,37 +37,75 @@ private val logger = Logging.getLogger(ShrinkTransformer::class.java)
...
@@ -40,37 +37,75 @@ private val logger = Logging.getLogger(ShrinkTransformer::class.java)
@AutoService
(
ClassTransformer
::
class
)
@AutoService
(
ClassTransformer
::
class
)
class
ShrinkTransformer
:
ClassTransformer
{
class
ShrinkTransformer
:
ClassTransformer
{
private
lateinit
var
applicationId
:
String
private
lateinit
var
appPackage
:
String
private
lateinit
var
pkgRStyleable
:
String
private
lateinit
var
appRStyleable
:
String
private
lateinit
var
appRStyleableClass
:
String
private
lateinit
var
symbols
:
SymbolList
private
lateinit
var
symbols
:
SymbolList
private
lateinit
var
retainedSymbols
:
Set
<
String
>
private
lateinit
var
retainedSymbols
:
Set
<
String
>
private
lateinit
var
ignores
:
Set
<
Wildcard
>
private
lateinit
var
logger
:
PrintWriter
override
fun
onPreTransform
(
context
:
TransformContext
)
{
override
fun
onPreTransform
(
context
:
TransformContext
)
{
this
.
applicationId
=
context
.
artifacts
.
get
(
SYMBOL_LIST_WITH_PACKAGE_NAME
).
single
().
head
()
!!
.
replace
(
'.'
,
'/'
)
this
.
appPackage
=
context
.
applicationId
.
replace
(
'.'
,
'/'
)
this
.
logger
=
context
.
reportsDir
.
file
(
Build
.
ARTIFACT
).
file
(
context
.
name
).
file
(
"report.txt"
).
touch
().
printWriter
()
this
.
symbols
=
SymbolList
.
from
(
context
.
artifacts
.
get
(
SYMBOL_LIST
).
single
())
this
.
symbols
=
SymbolList
.
from
(
context
.
artifacts
.
get
(
SYMBOL_LIST
).
single
())
this
.
pkgRStyleable
=
"$applicationId/$R_STYLEABLE"
this
.
appRStyleable
=
"$appPackage/$R_STYLEABLE"
this
.
appRStyleableClass
=
"$appPackage/$R_STYLEABLE_CLASS"
this
.
ignores
=
context
.
getProperty
(
PROPERTY_IGNORES
)
?.
split
(
','
)
?.
map
{
Wildcard
(
it
)
}
?.
toSet
()
?:
emptySet
()
val
redundant
=
context
.
findRedundantR
()
logger
.
println
(
"$PROPERTY_IGNORES=$ignores\n"
)
// Find symbols that should be retained
this
.
retainedSymbols
=
context
.
findRetainedSymbols
()
retainedSymbols
.
ifNotEmpty
{
symbols
->
logger
.
println
(
"Retained symbols:"
)
symbols
.
forEach
{
logger
.
println
(
" - R.id.$it"
)
}
logger
.
println
()
}
// Remove redundant R class files
redundant
.
ifNotEmpty
{
pairs
->
val
totalSize
=
redundant
.
map
{
it
.
first
.
length
()
}.
sum
()
val
maxWidth
=
redundant
.
map
{
it
.
second
.
length
}.
max
()
?.
plus
(
10
)
?:
10
ForkJoinPool
().
also
{
pool
->
logger
.
println
(
"Delete files:"
)
context
.
deleteLibraryRs
(
pool
,
applicationId
)
retainedSymbols
=
context
.
findRetainedSymbols
(
pool
)
}.
shutdown
()
logger
.
info
(
"Retained symbols: \n ${retainedSymbols.joinTo(StringBuilder(), "
\
n
-
")}"
)
pairs
.
forEach
{
if
(
it
.
first
.
delete
())
{
logger
.
println
(
" - `${it.second}`"
)
}
}
logger
.
println
(
"-"
.
repeat
(
maxWidth
))
logger
.
println
(
"Total: $totalSize bytes"
)
logger
.
println
()
}
}
}
override
fun
transform
(
context
:
TransformContext
,
klass
:
ClassNode
):
ClassNode
{
override
fun
transform
(
context
:
TransformContext
,
klass
:
ClassNode
):
ClassNode
{
when
{
when
{
klass
.
name
==
this
.
pkgRStyleable
->
klass
.
removeIntFields
()
this
.
ignores
.
any
{
it
.
matches
(
klass
.
name
)
}
->
{
logger
.
println
(
"Ignore `${klass.name}`"
)
return
klass
}
klass
.
name
==
this
.
appRStyleable
->
klass
.
removeIntFields
()
klass
.
simpleName
==
"BuildConfig"
->
klass
.
removeConstantFields
()
klass
.
simpleName
==
"BuildConfig"
->
klass
.
removeConstantFields
()
else
->
replaceSymbolReferenceWithConstant
(
klass
)
else
->
replaceSymbolReferenceWithConstant
(
klass
)
}
}
return
klass
return
klass
}
}
override
fun
onPostTransform
(
context
:
TransformContext
)
{
logger
.
close
()
}
private
fun
replaceSymbolReferenceWithConstant
(
klass
:
ClassNode
)
{
private
fun
replaceSymbolReferenceWithConstant
(
klass
:
ClassNode
)
{
klass
.
methods
.
forEach
{
method
->
klass
.
methods
.
forEach
{
method
->
val
insns
=
method
.
instructions
.
iterator
().
asIterable
().
filter
{
val
insns
=
method
.
instructions
.
iterator
().
asIterable
().
filter
{
it
.
opcode
==
Opcodes
.
GETSTATIC
it
.
opcode
==
GETSTATIC
}.
map
{
}.
map
{
it
as
FieldInsnNode
it
as
FieldInsnNode
}.
filter
{
}.
filter
{
...
@@ -89,17 +124,66 @@ class ShrinkTransformer : ClassTransformer {
...
@@ -89,17 +124,66 @@ class ShrinkTransformer : ClassTransformer {
method
.
instructions
.
insertBefore
(
field
,
LdcInsnNode
(
symbols
.
getInt
(
type
,
field
.
name
)))
method
.
instructions
.
insertBefore
(
field
,
LdcInsnNode
(
symbols
.
getInt
(
type
,
field
.
name
)))
method
.
instructions
.
remove
(
field
)
method
.
instructions
.
remove
(
field
)
}
catch
(
e
:
NullPointerException
)
{
}
catch
(
e
:
NullPointerException
)
{
println
(
"$RED ! Unresolvable symbol R.$type.${field.name} : ${klass.name}.${method.name}${method.desc} $RESET
"
)
logger
.
println
(
"Unresolvable symbol `R.$type.${field.name}` : ${klass.name}.${method.name}${method.desc}
"
)
}
}
}
}
// Replace library's R fields with application's R fields
// Replace library's R fields with application's R fields
intArrayFields
.
forEach
{
field
->
intArrayFields
.
forEach
{
field
->
field
.
owner
=
"$app
licationId
/${field.owner.substring(field.owner.lastIndexOf('/') + 1)}"
field
.
owner
=
"$app
Package
/${field.owner.substring(field.owner.lastIndexOf('/') + 1)}"
}
}
}
}
}
}
private
fun
TransformContext
.
findRedundantR
():
List
<
Pair
<
File
,
String
>>
{
return
artifacts
.
get
(
JAVAC
).
map
{
classes
->
val
base
=
classes
.
toURI
()
FileFinder
(
classes
)
{
r
->
r
.
name
.
startsWith
(
"R"
)
&&
r
.
name
.
endsWith
(
".class"
)
&&
(
r
.
name
[
1
]
==
'$'
||
r
.
name
.
length
==
7
)
}.
execute
().
map
{
r
->
Pair
(
r
,
base
.
relativize
(
r
.
toURI
()).
path
)
}
}.
flatten
().
filter
{
it
.
second
!=
appRStyleableClass
// keep application's R$styleable.class
}.
filter
{
pair
->
!
ignores
.
any
{
it
.
matches
(
pair
.
second
)
}
}
}
private
fun
ClassNode
.
removeIntFields
()
{
fields
.
map
{
it
as
FieldNode
}.
filter
{
field
->
val
signature
=
"$name.${field.name}${field.desc}"
!
ignores
.
any
{
it
.
matches
(
signature
)
}
}.
filter
{
it
.
desc
==
"I"
}.
forEach
{
fields
.
remove
(
it
)
logger
.
println
(
"Remove `$name.${it.name} : ${it.desc}` = 0x${(it.value as Int).toString(16)}"
)
}
}
private
fun
ClassNode
.
removeConstantFields
()
{
fields
.
map
{
it
as
FieldNode
}.
filter
{
field
->
val
signature
=
"$name.${field.name}${field.desc}"
!
ignores
.
any
{
it
.
matches
(
signature
)
}
}.
filter
{
0
!=
(
ACC_STATIC
and
it
.
access
)
&&
0
!=
(
ACC_FINAL
and
it
.
access
)
&&
it
.
value
!=
null
}.
forEach
{
fields
.
remove
(
it
)
logger
.
println
(
"Remove `$name.${it.name} : ${it.desc}` = ${it.valueAsString()}"
)
}
}
}
private
fun
FieldNode
.
valueAsString
()
=
when
{
this
.
value
is
String
->
"\"${this.value}\""
else
->
this
.
value
.
toString
()
}
}
/**
/**
...
@@ -107,51 +191,12 @@ class ShrinkTransformer : ClassTransformer {
...
@@ -107,51 +191,12 @@ class ShrinkTransformer : ClassTransformer {
*
*
* - attribute `constraint_referenced_ids` in `ConstraintLayout`
* - attribute `constraint_referenced_ids` in `ConstraintLayout`
*/
*/
private
fun
TransformContext
.
findRetainedSymbols
(
pool
:
ForkJoinPool
):
Set
<
String
>
{
private
fun
TransformContext
.
findRetainedSymbols
():
Set
<
String
>
{
return
artifacts
.
get
(
MERGED_RES
).
map
{
return
artifacts
.
get
(
MERGED_RES
).
map
{
pool
.
invoke
(
RetainedSymbolCollector
(
it
)
)
RetainedSymbolCollector
(
it
).
execute
(
)
}.
flatten
().
toSet
()
}.
flatten
().
toSet
()
}
}
/**
private
val
PROPERTY_PREFIX
=
Build
.
ARTIFACT
.
replace
(
'-'
,
'.'
)
* Delete R files of libraries
*/
private
fun
TransformContext
.
deleteLibraryRs
(
pool
:
ForkJoinPool
,
applicationId
:
String
)
{
return
artifacts
.
get
(
JAVAC
).
map
{
pool
.
invoke
(
RCollector
(
it
))
}.
flatten
().
filter
{
// only keep application's R$styleable.class
!
it
.
parent
.
endsWith
(
applicationId
.
separatorsToSystem
())
||
it
.
name
!=
R_STYLEABLE_CLASS
}.
forEach
{
// delete library's R
if
(
it
.
delete
())
{
println
(
"$YELLOW x ${it.absolutePath}$RESET"
)
}
else
{
println
(
"$BLUE ? ${it.absolutePath}$RESET"
)
}
}
}
private
fun
ClassNode
.
removeIntFields
()
{
fields
.
map
{
it
as
FieldNode
}.
filter
{
it
.
desc
==
"I"
}.
forEach
{
fields
.
remove
(
it
)
println
(
"$YELLOW x $name.${it.name} : ${it.desc}$RESET"
)
}
}
private
fun
ClassNode
.
removeConstantFields
()
{
private
val
PROPERTY_IGNORES
=
"$PROPERTY_PREFIX.ignores"
fields
.
map
{
it
as
FieldNode
}.
filter
{
0
!=
(
Opcodes
.
ACC_STATIC
and
it
.
access
)
&&
0
!=
(
Opcodes
.
ACC_FINAL
and
it
.
access
)
&&
CONST_TYPE_SIGNATURES
.
contains
(
it
.
desc
)
}.
forEach
{
fields
.
remove
(
it
)
println
(
"$YELLOW x $name.${it.name} : ${it.desc}$RESET"
)
}
}
booster-transform-spi/src/main/kotlin/com/didiglobal/booster/transform/TransformContext.kt
浏览文件 @
c7e2bd97
...
@@ -60,6 +60,11 @@ interface TransformContext {
...
@@ -60,6 +60,11 @@ interface TransformContext {
*/
*/
val
klassPool
:
KlassPool
val
klassPool
:
KlassPool
/**
* The application identifier
*/
val
applicationId
:
String
/**
/**
* Check if has the specified property. Generally, the property is equivalent to project property
* Check if has the specified property. Generally, the property is equivalent to project property
*
*
...
...
booster-transform-thread/build.gradle
浏览文件 @
c7e2bd97
...
@@ -3,7 +3,6 @@ apply from: '../gradle/booster.gradle'
...
@@ -3,7 +3,6 @@ apply from: '../gradle/booster.gradle'
dependencies
{
dependencies
{
kapt
"com.google.auto.service:auto-service:1.0-rc4"
kapt
"com.google.auto.service:auto-service:1.0-rc4"
implementation
project
(
':booster-android-gradle-api'
)
implementation
project
(
':booster-android-gradle-api'
)
implementation
project
(
':booster-gradle-base'
)
implementation
project
(
':booster-task-spi'
)
implementation
project
(
':booster-task-spi'
)
implementation
project
(
':booster-transform-asm'
)
implementation
project
(
':booster-transform-asm'
)
compileOnly
'com.android.tools.build:gradle:3.0.0'
compileOnly
'com.android.tools.build:gradle:3.0.0'
...
...
booster-transform-toast/build.gradle
浏览文件 @
c7e2bd97
...
@@ -3,7 +3,6 @@ apply from: '../gradle/booster.gradle'
...
@@ -3,7 +3,6 @@ apply from: '../gradle/booster.gradle'
dependencies
{
dependencies
{
kapt
"com.google.auto.service:auto-service:1.0-rc4"
kapt
"com.google.auto.service:auto-service:1.0-rc4"
implementation
project
(
':booster-android-gradle-api'
)
implementation
project
(
':booster-android-gradle-api'
)
implementation
project
(
':booster-gradle-base'
)
implementation
project
(
':booster-task-spi'
)
implementation
project
(
':booster-task-spi'
)
implementation
project
(
':booster-transform-asm'
)
implementation
project
(
':booster-transform-asm'
)
compileOnly
'com.android.tools.build:gradle:3.0.0'
compileOnly
'com.android.tools.build:gradle:3.0.0'
...
...
booster-transform-usage/build.gradle
浏览文件 @
c7e2bd97
...
@@ -2,6 +2,6 @@ apply from: '../gradle/booster.gradle'
...
@@ -2,6 +2,6 @@ apply from: '../gradle/booster.gradle'
dependencies
{
dependencies
{
kapt
"com.google.auto.service:auto-service:1.0-rc4"
kapt
"com.google.auto.service:auto-service:1.0-rc4"
compile
project
(
':booster-
gradle-base
'
)
compile
project
(
':booster-
android-gradle-api
'
)
compile
project
(
':booster-transform-asm'
)
compile
project
(
':booster-transform-asm'
)
}
}
settings.gradle
浏览文件 @
c7e2bd97
...
@@ -8,7 +8,6 @@ include ':booster-android-gradle-api'
...
@@ -8,7 +8,6 @@ include ':booster-android-gradle-api'
include
':booster-android-gradle-v3_0'
include
':booster-android-gradle-v3_0'
include
':booster-android-gradle-v3_2'
include
':booster-android-gradle-v3_2'
include
':booster-android-gradle-v3_3'
include
':booster-android-gradle-v3_3'
include
':booster-gradle-base'
include
':booster-gradle-plugin'
include
':booster-gradle-plugin'
include
':booster-task-all'
include
':booster-task-all'
include
':booster-task-artifact'
include
':booster-task-artifact'
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录