Skip to content

Commit d5af962

Browse files
ProGuard: require obfuscation be enabled explicitly (#2384)
1 parent 98b16c5 commit d5af962

File tree

6 files changed

+65
-17
lines changed

6 files changed

+65
-17
lines changed

gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/dsl/ProguardSettings.kt

+1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ abstract class ProguardSettings @Inject constructor(
2121
val maxHeapSize: Property<String?> = objects.nullableProperty()
2222
val configurationFiles: ConfigurableFileCollection = objects.fileCollection()
2323
val isEnabled: Property<Boolean> = objects.notNullProperty(false)
24+
val obfuscate: Property<Boolean> = objects.notNullProperty(false)
2425
}

gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/internal/configureJvmApplication.kt

+9
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,15 @@ private fun JvmApplicationContext.configureProguardTask(
242242
mainClass.set(app.mainClass)
243243
proguardVersion.set(settings.version)
244244
configurationFiles.from(settings.configurationFiles)
245+
// ProGuard uses -dontobfuscate option to turn off obfuscation, which is enabled by default
246+
// We want to disable obfuscation by default, because often
247+
// it is not needed, but makes troubleshooting much harder.
248+
// If obfuscation is turned off by default,
249+
// enabling (`isObfuscationEnabled.set(true)`) seems much better,
250+
// than disabling obfuscation disabling (`dontObfuscate.set(false)`).
251+
// That's why a task property is follows ProGuard design,
252+
// when our DSL does the opposite.
253+
dontobfuscate.set(settings.obfuscate.map { !it })
245254

246255
dependsOn(unpackDefaultResources)
247256
defaultComposeRulesFile.set(unpackDefaultResources.flatMap { it.resources.defaultComposeProguardRules })

gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/AbstractProguardTask.kt

+8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ abstract class AbstractProguardTask : AbstractComposeDesktopTask() {
4040
@get:InputFiles
4141
val configurationFiles: ConfigurableFileCollection = objects.fileCollection()
4242

43+
@get:Optional
44+
@get:Input
45+
val dontobfuscate: Property<Boolean?> = objects.nullableProperty()
46+
4347
// todo: DSL for excluding default rules
4448
// also consider pulling coroutines rules from coroutines artifact
4549
// https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/resources/META-INF/proguard/coroutines.pro
@@ -107,6 +111,10 @@ abstract class AbstractProguardTask : AbstractComposeDesktopTask() {
107111
}
108112

109113
rootConfigurationFile.ioFile.bufferedWriter().use { writer ->
114+
if (dontobfuscate.orNull == true) {
115+
writer.writeLn("-dontobfuscate")
116+
}
117+
110118
writer.writeLn("""
111119
-keep public class ${mainClass.get()} {
112120
public static void main(java.lang.String[]);

gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/DesktopApplicationTest.kt

+32-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import java.util.*
1616
import java.util.jar.JarFile
1717
import kotlin.collections.HashSet
1818
import org.junit.jupiter.api.Assertions.assertEquals
19+
import org.junit.jupiter.api.Assertions.assertFalse
1920
import org.junit.jupiter.api.Assumptions
2021
import org.junit.jupiter.api.Test
2122

@@ -77,17 +78,43 @@ class DesktopApplicationTest : GradlePluginTestBase() {
7778

7879
@Test
7980
fun proguard(): Unit = with(testProject(TestProjects.proguard)) {
80-
gradle(":runReleaseDistributable").build().checks { check ->
81-
check.taskOutcome(":proguardReleaseJars", TaskOutcome.SUCCESS)
81+
val enableObfuscation = """
82+
compose.desktop {
83+
application {
84+
buildTypes.release.proguard {
85+
obfuscate.set(true)
86+
}
87+
}
88+
}
89+
""".trimIndent()
8290

83-
assertEqualTextFiles(file("main-methods.actual.txt"), file("main-methods.expected.txt"))
91+
val actualMainImage = file("main-image.actual.png")
92+
val expectedMainImage = file("main-image.expected.png")
8493

85-
val actualMainImage = file("main-image.actual.png")
86-
val expectedMainImage = file("main-image.expected.png")
94+
fun checkImageBeforeBuild() {
95+
assertFalse(actualMainImage.exists(), "'$actualMainImage' exists")
96+
}
97+
fun checkImageAfterBuild() {
8798
assert(actualMainImage.readBytes().contentEquals(expectedMainImage.readBytes())) {
8899
"The actual image '$actualMainImage' does not match the expected image '$expectedMainImage'"
89100
}
90101
}
102+
103+
checkImageBeforeBuild()
104+
gradle(":runReleaseDistributable").build().checks { check ->
105+
check.taskOutcome(":proguardReleaseJars", TaskOutcome.SUCCESS)
106+
checkImageAfterBuild()
107+
assertEqualTextFiles(file("main-methods.actual.txt"), file("main-methods.expected.txt"))
108+
}
109+
110+
file("build.gradle").modify { "$it\n$enableObfuscation" }
111+
actualMainImage.delete()
112+
checkImageBeforeBuild()
113+
gradle(":runReleaseDistributable").build().checks { check ->
114+
check.taskOutcome(":proguardReleaseJars", TaskOutcome.SUCCESS)
115+
checkImageAfterBuild()
116+
assertNotEqualTextFiles(file("main-methods.actual.txt"), file("main-methods.expected.txt"))
117+
}
91118
}
92119

93120
@Test

gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/utils/assertUtils.kt

+15-8
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,20 @@ internal fun String.checkContains(substring: String) {
5151
}
5252

5353
internal fun assertEqualTextFiles(actual: File, expected: File) {
54-
fun File.normalizedText() = readLines().joinToString("\n") { it.trim() }
55-
56-
val actualText = actual.normalizedText()
57-
val expectedText = expected.normalizedText()
5854
Assertions.assertEquals(
59-
expectedText,
60-
actualText,
61-
"Expected file '$expected' differs from actual file '$actual'"
55+
expected.normalizedText(),
56+
actual.normalizedText(),
57+
"Content of '$expected' is not equal to content of '$actual'"
58+
)
59+
}
60+
61+
internal fun assertNotEqualTextFiles(actual: File, expected: File) {
62+
Assertions.assertNotEquals(
63+
expected.normalizedText(),
64+
actual.normalizedText(),
65+
"Content of '$expected' is equal to content of '$actual'"
6266
)
63-
}
67+
}
68+
69+
private fun File.normalizedText() =
70+
readLines().joinToString("\n") { it.trim() }
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
-keep public class Main {
22
public void keptByKeepRule(...);
3-
}
4-
5-
-keepclassmembernames public class Main {
6-
*;
73
}

0 commit comments

Comments
 (0)