如何防止 kotlin.Unit 对象被 Proguard 剥离
How to prevent kotlin.Unit object from being stripped by Proguard
Proguard 条纹 kotlin.Unit
独立对象(在 Kotlin 运行时库中),如果您使用此类型,则会导致编译错误。以下规则无助于保留此元素(可能是因为 Unit
不是 class,而是 object ):
-keep class kotlin.Unit.** { *; }
我们这样使用单位类型:
fun assert(func : Assert.() -> Unit) = Assert().apply(func)
是否有解决此问题的方法,或者我是否遗漏了什么?
在 Progaurd 规则中,class 名称后的通配符表示嵌套的 classes。所以去掉 Kotlin.Unit
之后的 .**
:
-keep class kotlin.Unit { *; }
不直接相关,但如果您使用 Kotlin 的集合,您还需要添加以下 Proguard 规则:
-keep class kotlin.collections.CollectionsKt { *; }
由于 Kotlin 的标准库有它自己的 Collections
文件并且它包含顶级函数,并且因为具有 top-level 函数的文件不是直接的 Java class,你应该使用 Kotlin 通常用来将此文件存储为 Java class 的名称。来自 Kotlin 的文档:
All the functions and properties declared in a file example.kt inside
a package org.foo.bar are put into a Java class named
org.foo.bar.ExampleKt.
The name of the generated Java class can be changed using the @JvmName
annotation.
同样不要在 class 名称后使用 .**
通配符。
Proguard 条纹 kotlin.Unit
独立对象(在 Kotlin 运行时库中),如果您使用此类型,则会导致编译错误。以下规则无助于保留此元素(可能是因为 Unit
不是 class,而是 object ):
-keep class kotlin.Unit.** { *; }
我们这样使用单位类型:
fun assert(func : Assert.() -> Unit) = Assert().apply(func)
是否有解决此问题的方法,或者我是否遗漏了什么?
在 Progaurd 规则中,class 名称后的通配符表示嵌套的 classes。所以去掉 Kotlin.Unit
之后的 .**
:
-keep class kotlin.Unit { *; }
不直接相关,但如果您使用 Kotlin 的集合,您还需要添加以下 Proguard 规则:
-keep class kotlin.collections.CollectionsKt { *; }
由于 Kotlin 的标准库有它自己的 Collections
文件并且它包含顶级函数,并且因为具有 top-level 函数的文件不是直接的 Java class,你应该使用 Kotlin 通常用来将此文件存储为 Java class 的名称。来自 Kotlin 的文档:
All the functions and properties declared in a file example.kt inside a package org.foo.bar are put into a Java class named org.foo.bar.ExampleKt.
The name of the generated Java class can be changed using the @JvmName annotation.
同样不要在 class 名称后使用 .**
通配符。