Skip to content

Commit

Permalink
Remove redundant preconditions from generated ProGuard rules (#1913)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilGlass authored and JakeWharton committed Dec 5, 2024
1 parent 932807e commit 8feae55
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import com.squareup.kotlinpoet.ClassName
* 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
* 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
* class with a deterministic name (see [outputFilePathWithoutExtension]) with an appropriate
Expand All @@ -46,7 +47,6 @@ public data class ProguardConfig(

public fun writeTo(out: Appendable): Unit = out.run {
//
// -if class {the target class}
// -keepnames class {the target class}
// -if class {the target class}
// -keep class {the generated adapter} {
Expand All @@ -57,7 +57,6 @@ public data class ProguardConfig(
val targetName = targetClass.reflectionName()
val adapterCanonicalName = ClassName(targetClass.packageName, adapterName).canonicalName
// Keep the class name for Moshi's reflective lookup based on it
appendLine("-if class $targetName")
appendLine("-keepnames class $targetName")

appendLine("-if class $targetName")
Expand All @@ -70,14 +69,14 @@ public data class ProguardConfig(
if (targetConstructorHasDefaults) {
// If the target class has default parameter values, keep its synthetic constructor
//
// -if class {the target class}
// -keepnames class kotlin.jvm.internal.DefaultConstructorMarker
// -keepclassmembers @com.squareup.moshi.JsonClass @kotlin.Metadata class * {
// synthetic <init>(...);
// }
//
appendLine("-if class $targetName")
appendLine("-keepnames class kotlin.jvm.internal.DefaultConstructorMarker")
appendLine("-if class $targetName")
appendLine("-keepclassmembers class $targetName {")
val allParams = targetConstructorParams.toMutableList()
val maskCount = if (targetConstructorParams.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,6 @@ class JsonClassCodegenProcessorTest {
when (generatedFile.nameWithoutExtension) {
"moshi-testPackage.Aliases" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.Aliases
-keepnames class testPackage.Aliases
-if class testPackage.Aliases
-keep class testPackage.AliasesJsonAdapter {
Expand All @@ -667,7 +666,6 @@ class JsonClassCodegenProcessorTest {
)
"moshi-testPackage.Simple" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.Simple
-keepnames class testPackage.Simple
-if class testPackage.Simple
-keep class testPackage.SimpleJsonAdapter {
Expand All @@ -677,7 +675,6 @@ class JsonClassCodegenProcessorTest {
)
"moshi-testPackage.Generic" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.Generic
-keepnames class testPackage.Generic
-if class testPackage.Generic
-keep class testPackage.GenericJsonAdapter {
Expand All @@ -688,7 +685,6 @@ class JsonClassCodegenProcessorTest {
"moshi-testPackage.UsingQualifiers" -> {
assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.UsingQualifiers
-keepnames class testPackage.UsingQualifiers
-if class testPackage.UsingQualifiers
-keep class testPackage.UsingQualifiersJsonAdapter {
Expand All @@ -699,7 +695,6 @@ class JsonClassCodegenProcessorTest {
}
"moshi-testPackage.MixedTypes" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.MixedTypes
-keepnames class testPackage.MixedTypes
-if class testPackage.MixedTypes
-keep class testPackage.MixedTypesJsonAdapter {
Expand All @@ -709,15 +704,13 @@ class JsonClassCodegenProcessorTest {
)
"moshi-testPackage.DefaultParams" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.DefaultParams
-keepnames class testPackage.DefaultParams
-if class testPackage.DefaultParams
-keep class testPackage.DefaultParamsJsonAdapter {
public <init>(com.squareup.moshi.Moshi);
}
-if class testPackage.DefaultParams
-keepnames class kotlin.jvm.internal.DefaultConstructorMarker
-if class testPackage.DefaultParams
-keepclassmembers class testPackage.DefaultParams {
public synthetic <init>(java.lang.String,int,kotlin.jvm.internal.DefaultConstructorMarker);
}
Expand All @@ -726,15 +719,13 @@ class JsonClassCodegenProcessorTest {
"moshi-testPackage.Complex" -> {
assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.Complex
-keepnames class testPackage.Complex
-if class testPackage.Complex
-keep class testPackage.ComplexJsonAdapter {
public <init>(com.squareup.moshi.Moshi,java.lang.reflect.Type[]);
}
-if class testPackage.Complex
-keepnames class kotlin.jvm.internal.DefaultConstructorMarker
-if class testPackage.Complex
-keepclassmembers class testPackage.Complex {
public synthetic <init>(java.lang.String,java.util.List,java.lang.Object,int,kotlin.jvm.internal.DefaultConstructorMarker);
}
Expand All @@ -743,15 +734,13 @@ class JsonClassCodegenProcessorTest {
}
"moshi-testPackage.MultipleMasks" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.MultipleMasks
-keepnames class testPackage.MultipleMasks
-if class testPackage.MultipleMasks
-keep class testPackage.MultipleMasksJsonAdapter {
public <init>(com.squareup.moshi.Moshi);
}
-if class testPackage.MultipleMasks
-keepnames class kotlin.jvm.internal.DefaultConstructorMarker
-if 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);
}
Expand All @@ -760,7 +749,6 @@ class JsonClassCodegenProcessorTest {
"moshi-testPackage.NestedType.NestedSimple" -> {
assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.NestedType${'$'}NestedSimple
-keepnames class testPackage.NestedType${'$'}NestedSimple
-if class testPackage.NestedType${'$'}NestedSimple
-keep class testPackage.NestedType_NestedSimpleJsonAdapter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,6 @@ class JsonClassSymbolProcessorTest {
when (generatedFile.nameWithoutExtension) {
"moshi-testPackage.Aliases" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.Aliases
-keepnames class testPackage.Aliases
-if class testPackage.Aliases
-keep class testPackage.AliasesJsonAdapter {
Expand All @@ -718,7 +717,6 @@ class JsonClassSymbolProcessorTest {
)
"moshi-testPackage.Simple" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.Simple
-keepnames class testPackage.Simple
-if class testPackage.Simple
-keep class testPackage.SimpleJsonAdapter {
Expand All @@ -728,7 +726,6 @@ class JsonClassSymbolProcessorTest {
)
"moshi-testPackage.Generic" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.Generic
-keepnames class testPackage.Generic
-if class testPackage.Generic
-keep class testPackage.GenericJsonAdapter {
Expand All @@ -739,7 +736,6 @@ class JsonClassSymbolProcessorTest {
"moshi-testPackage.UsingQualifiers" -> {
assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.UsingQualifiers
-keepnames class testPackage.UsingQualifiers
-if class testPackage.UsingQualifiers
-keep class testPackage.UsingQualifiersJsonAdapter {
Expand All @@ -750,7 +746,6 @@ class JsonClassSymbolProcessorTest {
}
"moshi-testPackage.MixedTypes" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.MixedTypes
-keepnames class testPackage.MixedTypes
-if class testPackage.MixedTypes
-keep class testPackage.MixedTypesJsonAdapter {
Expand All @@ -760,15 +755,13 @@ class JsonClassSymbolProcessorTest {
)
"moshi-testPackage.DefaultParams" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.DefaultParams
-keepnames class testPackage.DefaultParams
-if class testPackage.DefaultParams
-keep class testPackage.DefaultParamsJsonAdapter {
public <init>(com.squareup.moshi.Moshi);
}
-if class testPackage.DefaultParams
-keepnames class kotlin.jvm.internal.DefaultConstructorMarker
-if class testPackage.DefaultParams
-keepclassmembers class testPackage.DefaultParams {
public synthetic <init>(java.lang.String,int,kotlin.jvm.internal.DefaultConstructorMarker);
}
Expand All @@ -777,15 +770,13 @@ class JsonClassSymbolProcessorTest {
"moshi-testPackage.Complex" -> {
assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.Complex
-keepnames class testPackage.Complex
-if class testPackage.Complex
-keep class testPackage.ComplexJsonAdapter {
public <init>(com.squareup.moshi.Moshi,java.lang.reflect.Type[]);
}
-if class testPackage.Complex
-keepnames class kotlin.jvm.internal.DefaultConstructorMarker
-if class testPackage.Complex
-keepclassmembers class testPackage.Complex {
public synthetic <init>(java.lang.String,java.util.List,java.lang.Object,int,kotlin.jvm.internal.DefaultConstructorMarker);
}
Expand All @@ -794,15 +785,13 @@ class JsonClassSymbolProcessorTest {
}
"moshi-testPackage.MultipleMasks" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.MultipleMasks
-keepnames class testPackage.MultipleMasks
-if class testPackage.MultipleMasks
-keep class testPackage.MultipleMasksJsonAdapter {
public <init>(com.squareup.moshi.Moshi);
}
-if class testPackage.MultipleMasks
-keepnames class kotlin.jvm.internal.DefaultConstructorMarker
-if 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);
}
Expand All @@ -811,7 +800,6 @@ class JsonClassSymbolProcessorTest {
"moshi-testPackage.NestedType.NestedSimple" -> {
assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.NestedType${'$'}NestedSimple
-keepnames class testPackage.NestedType${'$'}NestedSimple
-if class testPackage.NestedType${'$'}NestedSimple
-keep class testPackage.NestedType_NestedSimpleJsonAdapter {
Expand Down

0 comments on commit 8feae55

Please sign in to comment.