Skip to content

Enable dead code elimination for vendored deps. #4368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,31 @@ import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.logging.Logger
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.create

abstract class VendorExtension {
/** Controls dead code elimination, enabled if true. */
abstract val optimize: Property<Boolean>

init {
optimize.convention(true)
}
}

class VendorPlugin : Plugin<Project> {
override fun apply(project: Project) {
val vendorConfig = project.extensions.create<VendorExtension>("vendor")
project.plugins.all {
when (this) {
is LibraryPlugin -> configureAndroid(project)
is LibraryPlugin -> configureAndroid(project, vendorConfig)
}
}
}

fun configureAndroid(project: Project) {
fun configureAndroid(project: Project, vendorConfig: VendorExtension) {
project.apply(plugin = "LicenseResolverPlugin")

val vendor = project.configurations.create("vendor")
Expand All @@ -71,7 +84,8 @@ class VendorPlugin : Plugin<Project> {
},
jarJarProvider = { jarJar.resolve() },
project = project,
logger = project.logger
logger = project.logger,
optimize = vendorConfig.optimize
),
logger = project.logger
)
Expand All @@ -80,19 +94,35 @@ class VendorPlugin : Plugin<Project> {
}

interface JarTransformer {
fun transform(inputJar: File, outputJar: File, packagesToVendor: Set<String>)
fun transform(
inputJar: File,
outputJar: File,
ownPackages: Set<String>,
packagesToVendor: Set<String>
)
}

class JarJarTransformer(
private val parentPackageProvider: () -> String,
private val jarJarProvider: () -> Collection<File>,
private val project: Project,
private val logger: Logger
private val logger: Logger,
private val optimize: Provider<Boolean>
) : JarTransformer {
override fun transform(inputJar: File, outputJar: File, packagesToVendor: Set<String>) {
override fun transform(
inputJar: File,
outputJar: File,
ownPackages: Set<String>,
packagesToVendor: Set<String>
) {
val parentPackage = parentPackageProvider()
val rulesFile = File.createTempFile(parentPackage, ".jarjar")
rulesFile.printWriter().use {
if (optimize.get()) {
for (packageName in ownPackages) {
it.println("keep $packageName.**")
}
}
for (externalPackageName in packagesToVendor) {
it.println("rule $externalPackageName.** $parentPackage.@0")
}
Expand Down Expand Up @@ -227,7 +257,7 @@ class VendorTransform(
zipAll(unzippedDir, jar)
val outputJar = File(workDir, "output.jar")

jarTransformer.transform(jar, outputJar, externalPackageNames)
jarTransformer.transform(jar, outputJar, ownPackageNames, externalPackageNames)
return outputJar
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ class VendorPluginTests {
)

// ImmutableList is not used, so it should be stripped out.
assertThat(classes).doesNotContain("com/google/common/collect/ImmutableList.class")
assertThat(classes).doesNotContain("com/example/com/google/common/collect/ImmutableList.class")
}

@Test
fun `vendor dagger excluding javax transitive deps and not using it should include dagger`() {
fun `vendor dagger excluding javax transitive deps and not using it should not include dagger`() {
val classes =
buildWith(
"""
Expand All @@ -113,12 +113,7 @@ class VendorPluginTests {
)
)
// expected classes
assertThat(classes)
.containsAtLeast(
"com/example/Hello.class",
"com/example/BuildConfig.class",
"com/example/dagger/Lazy.class"
)
assertThat(classes).containsExactly("com/example/Hello.class", "com/example/BuildConfig.class")
}

@Test
Expand Down
6 changes: 6 additions & 0 deletions transport/transport-runtime/transport-runtime.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ firebaseLibrary {
}
}

vendor {
// Integration tests use dagger classes that are not used in the main SDK,
// so we disable dead code elimination to ensure those classes are preserved.
optimize = false
}

android {
compileSdkVersion project.targetSdkVersion
defaultConfig {
Expand Down