diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.kt index ff017e8ca..bf69e3059 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.kt @@ -7,6 +7,7 @@ import com.github.jengelman.gradle.plugins.shadow.internal.runtimeConfiguration import com.github.jengelman.gradle.plugins.shadow.internal.sourceSets import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar.Companion.registerShadowJarCommon import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar.Companion.shadowJar +import java.io.File import javax.inject.Inject import org.gradle.api.NamedDomainObjectProvider import org.gradle.api.Plugin @@ -42,6 +43,11 @@ constructor(private val softwareComponentFactory: SoftwareComponentFactory) : Pl registerShadowJarCommon(tasks.named("jar", Jar::class.java)) { task -> task.from(sourceSets.named("main").map { it.output }) task.configurations.convention(provider { listOf(runtimeConfiguration) }) + // Include all source sets (including test) so that test code's transitive dependency + // references are also considered "used" during minimization. + task.sourceSetsClassesDirs.convention( + sourceSets.map { it.output.classesDirs.filter(File::isDirectory) } + ) } artifacts.add(configurations.shadow.name, taskProvider) } diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowKmpPlugin.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowKmpPlugin.kt index c8bb3a257..2fe756555 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowKmpPlugin.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowKmpPlugin.kt @@ -3,6 +3,7 @@ package com.github.jengelman.gradle.plugins.shadow import com.github.jengelman.gradle.plugins.shadow.internal.isAtLeastKgp import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar.Companion.SHADOW_JAR_TASK_NAME import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar.Companion.registerShadowJarCommon +import java.io.File import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.tasks.bundling.Jar @@ -41,6 +42,13 @@ public abstract class ShadowKmpPlugin : Plugin { .flatMap { configurations.named(it.runtimeDependencyConfigurationName) } .map { listOf(it) } ) + // Include all compilations (including test) so that test code's transitive dependency + // references are also considered "used" during minimization. + task.sourceSetsClassesDirs.convention( + target.compilations.map { compilation -> + compilation.output.classesDirs.filter(File::isDirectory) + } + ) if (!isAtLeastKgp("1.9.0")) return@registerShadowJarCommon diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt index 23020620d..cc020c98a 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt @@ -20,7 +20,6 @@ import com.github.jengelman.gradle.plugins.shadow.internal.minimizeWithR8 import com.github.jengelman.gradle.plugins.shadow.internal.multiReleaseAttributeKey import com.github.jengelman.gradle.plugins.shadow.internal.property import com.github.jengelman.gradle.plugins.shadow.internal.setProperty -import com.github.jengelman.gradle.plugins.shadow.internal.sourceSets import com.github.jengelman.gradle.plugins.shadow.internal.useZip import com.github.jengelman.gradle.plugins.shadow.relocation.CacheableRelocator import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator @@ -138,17 +137,7 @@ public abstract class ShadowJar : Jar() { @get:InputFiles @get:PathSensitive(PathSensitivity.RELATIVE) - public open val sourceSetsClassesDirs: ConfigurableFileCollection = objectFactory.fileCollection { - _minimizeJar.map { - if (it) { - project.sourceSets.map { sourceSet -> - sourceSet.output.classesDirs.filter(File::isDirectory) - } - } else { - emptySet() - } - } - } + public open val sourceSetsClassesDirs: ConfigurableFileCollection = objectFactory.fileCollection() @get:Classpath public open val r8Classpath: ConfigurableFileCollection = objectFactory.fileCollection {