mirror of
https://github.com/fankes/moshi.git
synced 2025-10-18 23:49:21 +08:00
Remove redundant preconditions from generated ProGuard rules (#1913)
This commit is contained in:
@@ -26,7 +26,8 @@ import com.squareup.kotlinpoet.ClassName
|
|||||||
* constructor as well as the DefaultConstructorMarker type Kotlin adds to it.
|
* constructor as well as the DefaultConstructorMarker type Kotlin adds to it.
|
||||||
*
|
*
|
||||||
* Each rule is intended to be as specific and targeted as possible to reduce footprint, and each is
|
* Each rule is intended to be as specific and targeted as possible to reduce footprint, and each is
|
||||||
* conditioned on usage of the original target type.
|
* conditioned on usage of the original target type (implicitly for keepnames and keepclassmembers,
|
||||||
|
* which have no effect if the target type was removed in the shrinking phase).
|
||||||
*
|
*
|
||||||
* To keep this processor as an ISOLATING incremental processor, we generate one file per target
|
* To keep this processor as an ISOLATING incremental processor, we generate one file per target
|
||||||
* class with a deterministic name (see [outputFilePathWithoutExtension]) with an appropriate
|
* class with a deterministic name (see [outputFilePathWithoutExtension]) with an appropriate
|
||||||
@@ -46,7 +47,6 @@ public data class ProguardConfig(
|
|||||||
|
|
||||||
public fun writeTo(out: Appendable): Unit = out.run {
|
public fun writeTo(out: Appendable): Unit = out.run {
|
||||||
//
|
//
|
||||||
// -if class {the target class}
|
|
||||||
// -keepnames class {the target class}
|
// -keepnames class {the target class}
|
||||||
// -if class {the target class}
|
// -if class {the target class}
|
||||||
// -keep class {the generated adapter} {
|
// -keep class {the generated adapter} {
|
||||||
@@ -57,7 +57,6 @@ public data class ProguardConfig(
|
|||||||
val targetName = targetClass.reflectionName()
|
val targetName = targetClass.reflectionName()
|
||||||
val adapterCanonicalName = ClassName(targetClass.packageName, adapterName).canonicalName
|
val adapterCanonicalName = ClassName(targetClass.packageName, adapterName).canonicalName
|
||||||
// Keep the class name for Moshi's reflective lookup based on it
|
// Keep the class name for Moshi's reflective lookup based on it
|
||||||
appendLine("-if class $targetName")
|
|
||||||
appendLine("-keepnames class $targetName")
|
appendLine("-keepnames class $targetName")
|
||||||
|
|
||||||
appendLine("-if class $targetName")
|
appendLine("-if class $targetName")
|
||||||
@@ -70,6 +69,7 @@ public data class ProguardConfig(
|
|||||||
if (targetConstructorHasDefaults) {
|
if (targetConstructorHasDefaults) {
|
||||||
// If the target class has default parameter values, keep its synthetic constructor
|
// If the target class has default parameter values, keep its synthetic constructor
|
||||||
//
|
//
|
||||||
|
// -if class {the target class}
|
||||||
// -keepnames class kotlin.jvm.internal.DefaultConstructorMarker
|
// -keepnames class kotlin.jvm.internal.DefaultConstructorMarker
|
||||||
// -keepclassmembers @com.squareup.moshi.JsonClass @kotlin.Metadata class * {
|
// -keepclassmembers @com.squareup.moshi.JsonClass @kotlin.Metadata class * {
|
||||||
// synthetic <init>(...);
|
// synthetic <init>(...);
|
||||||
@@ -77,7 +77,6 @@ public data class ProguardConfig(
|
|||||||
//
|
//
|
||||||
appendLine("-if class $targetName")
|
appendLine("-if class $targetName")
|
||||||
appendLine("-keepnames class kotlin.jvm.internal.DefaultConstructorMarker")
|
appendLine("-keepnames class kotlin.jvm.internal.DefaultConstructorMarker")
|
||||||
appendLine("-if class $targetName")
|
|
||||||
appendLine("-keepclassmembers class $targetName {")
|
appendLine("-keepclassmembers class $targetName {")
|
||||||
val allParams = targetConstructorParams.toMutableList()
|
val allParams = targetConstructorParams.toMutableList()
|
||||||
val maskCount = if (targetConstructorParams.isEmpty()) {
|
val maskCount = if (targetConstructorParams.isEmpty()) {
|
||||||
|
@@ -727,7 +727,6 @@ class JsonClassSymbolProcessorTest(private val useKSP2: Boolean) {
|
|||||||
when (generatedFile.nameWithoutExtension) {
|
when (generatedFile.nameWithoutExtension) {
|
||||||
"moshi-testPackage.Aliases" -> assertThat(generatedFile.readText()).contains(
|
"moshi-testPackage.Aliases" -> assertThat(generatedFile.readText()).contains(
|
||||||
"""
|
"""
|
||||||
-if class testPackage.Aliases
|
|
||||||
-keepnames class testPackage.Aliases
|
-keepnames class testPackage.Aliases
|
||||||
-if class testPackage.Aliases
|
-if class testPackage.Aliases
|
||||||
-keep class testPackage.AliasesJsonAdapter {
|
-keep class testPackage.AliasesJsonAdapter {
|
||||||
@@ -738,7 +737,6 @@ class JsonClassSymbolProcessorTest(private val useKSP2: Boolean) {
|
|||||||
|
|
||||||
"moshi-testPackage.Simple" -> assertThat(generatedFile.readText()).contains(
|
"moshi-testPackage.Simple" -> assertThat(generatedFile.readText()).contains(
|
||||||
"""
|
"""
|
||||||
-if class testPackage.Simple
|
|
||||||
-keepnames class testPackage.Simple
|
-keepnames class testPackage.Simple
|
||||||
-if class testPackage.Simple
|
-if class testPackage.Simple
|
||||||
-keep class testPackage.SimpleJsonAdapter {
|
-keep class testPackage.SimpleJsonAdapter {
|
||||||
@@ -749,7 +747,6 @@ class JsonClassSymbolProcessorTest(private val useKSP2: Boolean) {
|
|||||||
|
|
||||||
"moshi-testPackage.Generic" -> assertThat(generatedFile.readText()).contains(
|
"moshi-testPackage.Generic" -> assertThat(generatedFile.readText()).contains(
|
||||||
"""
|
"""
|
||||||
-if class testPackage.Generic
|
|
||||||
-keepnames class testPackage.Generic
|
-keepnames class testPackage.Generic
|
||||||
-if class testPackage.Generic
|
-if class testPackage.Generic
|
||||||
-keep class testPackage.GenericJsonAdapter {
|
-keep class testPackage.GenericJsonAdapter {
|
||||||
@@ -761,7 +758,6 @@ class JsonClassSymbolProcessorTest(private val useKSP2: Boolean) {
|
|||||||
"moshi-testPackage.UsingQualifiers" -> {
|
"moshi-testPackage.UsingQualifiers" -> {
|
||||||
assertThat(generatedFile.readText()).contains(
|
assertThat(generatedFile.readText()).contains(
|
||||||
"""
|
"""
|
||||||
-if class testPackage.UsingQualifiers
|
|
||||||
-keepnames class testPackage.UsingQualifiers
|
-keepnames class testPackage.UsingQualifiers
|
||||||
-if class testPackage.UsingQualifiers
|
-if class testPackage.UsingQualifiers
|
||||||
-keep class testPackage.UsingQualifiersJsonAdapter {
|
-keep class testPackage.UsingQualifiersJsonAdapter {
|
||||||
@@ -773,7 +769,6 @@ class JsonClassSymbolProcessorTest(private val useKSP2: Boolean) {
|
|||||||
|
|
||||||
"moshi-testPackage.MixedTypes" -> assertThat(generatedFile.readText()).contains(
|
"moshi-testPackage.MixedTypes" -> assertThat(generatedFile.readText()).contains(
|
||||||
"""
|
"""
|
||||||
-if class testPackage.MixedTypes
|
|
||||||
-keepnames class testPackage.MixedTypes
|
-keepnames class testPackage.MixedTypes
|
||||||
-if class testPackage.MixedTypes
|
-if class testPackage.MixedTypes
|
||||||
-keep class testPackage.MixedTypesJsonAdapter {
|
-keep class testPackage.MixedTypesJsonAdapter {
|
||||||
@@ -784,7 +779,6 @@ class JsonClassSymbolProcessorTest(private val useKSP2: Boolean) {
|
|||||||
|
|
||||||
"moshi-testPackage.DefaultParams" -> assertThat(generatedFile.readText()).contains(
|
"moshi-testPackage.DefaultParams" -> assertThat(generatedFile.readText()).contains(
|
||||||
"""
|
"""
|
||||||
-if class testPackage.DefaultParams
|
|
||||||
-keepnames class testPackage.DefaultParams
|
-keepnames class testPackage.DefaultParams
|
||||||
-if class testPackage.DefaultParams
|
-if class testPackage.DefaultParams
|
||||||
-keep class testPackage.DefaultParamsJsonAdapter {
|
-keep class testPackage.DefaultParamsJsonAdapter {
|
||||||
@@ -792,7 +786,6 @@ class JsonClassSymbolProcessorTest(private val useKSP2: Boolean) {
|
|||||||
}
|
}
|
||||||
-if class testPackage.DefaultParams
|
-if class testPackage.DefaultParams
|
||||||
-keepnames class kotlin.jvm.internal.DefaultConstructorMarker
|
-keepnames class kotlin.jvm.internal.DefaultConstructorMarker
|
||||||
-if class testPackage.DefaultParams
|
|
||||||
-keepclassmembers class testPackage.DefaultParams {
|
-keepclassmembers class testPackage.DefaultParams {
|
||||||
public synthetic <init>(java.lang.String,int,kotlin.jvm.internal.DefaultConstructorMarker);
|
public synthetic <init>(java.lang.String,int,kotlin.jvm.internal.DefaultConstructorMarker);
|
||||||
}
|
}
|
||||||
@@ -802,7 +795,6 @@ class JsonClassSymbolProcessorTest(private val useKSP2: Boolean) {
|
|||||||
"moshi-testPackage.Complex" -> {
|
"moshi-testPackage.Complex" -> {
|
||||||
assertThat(generatedFile.readText()).contains(
|
assertThat(generatedFile.readText()).contains(
|
||||||
"""
|
"""
|
||||||
-if class testPackage.Complex
|
|
||||||
-keepnames class testPackage.Complex
|
-keepnames class testPackage.Complex
|
||||||
-if class testPackage.Complex
|
-if class testPackage.Complex
|
||||||
-keep class testPackage.ComplexJsonAdapter {
|
-keep class testPackage.ComplexJsonAdapter {
|
||||||
@@ -810,7 +802,6 @@ class JsonClassSymbolProcessorTest(private val useKSP2: Boolean) {
|
|||||||
}
|
}
|
||||||
-if class testPackage.Complex
|
-if class testPackage.Complex
|
||||||
-keepnames class kotlin.jvm.internal.DefaultConstructorMarker
|
-keepnames class kotlin.jvm.internal.DefaultConstructorMarker
|
||||||
-if class testPackage.Complex
|
|
||||||
-keepclassmembers class testPackage.Complex {
|
-keepclassmembers class testPackage.Complex {
|
||||||
public synthetic <init>(java.lang.String,java.util.List,java.lang.Object,int,kotlin.jvm.internal.DefaultConstructorMarker);
|
public synthetic <init>(java.lang.String,java.util.List,java.lang.Object,int,kotlin.jvm.internal.DefaultConstructorMarker);
|
||||||
}
|
}
|
||||||
@@ -820,7 +811,6 @@ class JsonClassSymbolProcessorTest(private val useKSP2: Boolean) {
|
|||||||
|
|
||||||
"moshi-testPackage.MultipleMasks" -> assertThat(generatedFile.readText()).contains(
|
"moshi-testPackage.MultipleMasks" -> assertThat(generatedFile.readText()).contains(
|
||||||
"""
|
"""
|
||||||
-if class testPackage.MultipleMasks
|
|
||||||
-keepnames class testPackage.MultipleMasks
|
-keepnames class testPackage.MultipleMasks
|
||||||
-if class testPackage.MultipleMasks
|
-if class testPackage.MultipleMasks
|
||||||
-keep class testPackage.MultipleMasksJsonAdapter {
|
-keep class testPackage.MultipleMasksJsonAdapter {
|
||||||
@@ -828,7 +818,6 @@ class JsonClassSymbolProcessorTest(private val useKSP2: Boolean) {
|
|||||||
}
|
}
|
||||||
-if class testPackage.MultipleMasks
|
-if class testPackage.MultipleMasks
|
||||||
-keepnames class kotlin.jvm.internal.DefaultConstructorMarker
|
-keepnames class kotlin.jvm.internal.DefaultConstructorMarker
|
||||||
-if class testPackage.MultipleMasks
|
|
||||||
-keepclassmembers class testPackage.MultipleMasks {
|
-keepclassmembers class testPackage.MultipleMasks {
|
||||||
public synthetic <init>(long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,int,int,int,kotlin.jvm.internal.DefaultConstructorMarker);
|
public synthetic <init>(long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,long,int,int,int,kotlin.jvm.internal.DefaultConstructorMarker);
|
||||||
}
|
}
|
||||||
@@ -838,7 +827,6 @@ class JsonClassSymbolProcessorTest(private val useKSP2: Boolean) {
|
|||||||
"moshi-testPackage.NestedType.NestedSimple" -> {
|
"moshi-testPackage.NestedType.NestedSimple" -> {
|
||||||
assertThat(generatedFile.readText()).contains(
|
assertThat(generatedFile.readText()).contains(
|
||||||
"""
|
"""
|
||||||
-if class testPackage.NestedType${'$'}NestedSimple
|
|
||||||
-keepnames class testPackage.NestedType${'$'}NestedSimple
|
-keepnames class testPackage.NestedType${'$'}NestedSimple
|
||||||
-if class testPackage.NestedType${'$'}NestedSimple
|
-if class testPackage.NestedType${'$'}NestedSimple
|
||||||
-keep class testPackage.NestedType_NestedSimpleJsonAdapter {
|
-keep class testPackage.NestedType_NestedSimpleJsonAdapter {
|
||||||
|
Reference in New Issue
Block a user