Skip to content

Commit 9765ba2

Browse files
committed
Remove dead code elimination.
1 parent 6f15a17 commit 9765ba2

File tree

2 files changed

+31
-62
lines changed

2 files changed

+31
-62
lines changed

buildSrc/src/main/java/com/google/firebase/gradle/plugins/VendorPlugin.kt

+24-56
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import org.gradle.api.GradleException
3232
import org.gradle.api.Plugin
3333
import org.gradle.api.Project
3434
import org.gradle.api.artifacts.Configuration
35-
import org.gradle.api.attributes.Attribute
3635
import org.gradle.api.logging.Logger
3736

3837
class VendorPlugin : Plugin<Project> {
@@ -59,6 +58,7 @@ class VendorPlugin : Plugin<Project> {
5958
val android = project.extensions.getByType(LibraryExtension::class.java)
6059

6160
android.registerTransform(VendorTransform(
61+
android,
6262
vendor,
6363
JarJarTransformer(
6464
parentPackageProvider = {
@@ -67,13 +67,12 @@ class VendorPlugin : Plugin<Project> {
6767
jarJarProvider = { jarJar.resolve() },
6868
project = project,
6969
logger = project.logger),
70-
logger = project.logger,
71-
android = android))
70+
logger = project.logger))
7271
}
7372
}
7473

7574
interface JarTransformer {
76-
fun transform(variantName: String, input: File, output: File, ownPackageNames: Set<String>, externalPackageNames: Set<String>, classesToExclude: Set<String>)
75+
fun transform(inputJar: File, outputJar: File, packagesToVendor: Set<String>)
7776
}
7877

7978
class JarJarTransformer(
@@ -82,17 +81,11 @@ class JarJarTransformer(
8281
private val project: Project,
8382
private val logger: Logger
8483
) : JarTransformer {
85-
override fun transform(variantName: String, input: File, output: File, ownPackageNames: Set<String>, externalPackageNames: Set<String>, classesToExclude: Set<String>) {
84+
override fun transform(inputJar: File, outputJar: File, packagesToVendor: Set<String>) {
8685
val parentPackage = parentPackageProvider()
87-
val rulesFile = File.createTempFile("$parentPackage-$variantName", ".jarjar")
86+
val rulesFile = File.createTempFile(parentPackage, ".jarjar")
8887
rulesFile.printWriter().use {
89-
for (classToExclude in classesToExclude) {
90-
it.println("zap $classToExclude")
91-
}
92-
for (packageName in ownPackageNames) {
93-
it.println("keep $packageName.**")
94-
}
95-
for (externalPackageName in externalPackageNames) {
88+
for (externalPackageName in packagesToVendor) {
9689
it.println("rule $externalPackageName.** $parentPackage.@0")
9790
}
9891
}
@@ -102,17 +95,17 @@ class JarJarTransformer(
10295
main = "org.pantsbuild.jarjar.Main"
10396
classpath = project.files(jarJarProvider())
10497
// jvmArgs = listOf("-Xdebug", "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005")
105-
args = listOf("process", rulesFile.absolutePath, input.absolutePath, output.absolutePath)
98+
args = listOf("process", rulesFile.absolutePath, inputJar.absolutePath, outputJar.absolutePath)
10699
systemProperties = mapOf("verbose" to "true", "misplacedClassStrategy" to "FATAL")
107100
}.assertNormalExitValue()
108101
}
109102
}
110103

111104
class VendorTransform(
105+
private val android: LibraryExtension,
112106
private val configuration: Configuration,
113107
private val jarTransformer: JarTransformer,
114-
private val logger: Logger,
115-
private val android: LibraryExtension
108+
private val logger: Logger
116109
) :
117110
Transform() {
118111
override fun getName() = "firebaseVendorTransform"
@@ -132,7 +125,6 @@ class VendorTransform(
132125
}
133126

134127
override fun transform(transformInvocation: TransformInvocation) {
135-
println(transformInvocation.referencedInputs)
136128
if (configuration.resolve().isEmpty()) {
137129
logger.info("Nothing to vendor. " +
138130
"If you don't need vendor functionality please disable 'firebase-vendor' plugin. " +
@@ -167,10 +159,18 @@ class VendorTransform(
167159
}
168160
}
169161

162+
private fun isTest(transformInvocation: TransformInvocation): Boolean {
163+
return android.testVariants.find { it.name == transformInvocation.context.variantName } != null
164+
}
165+
170166
private fun process(workDir: File, transformInvocation: TransformInvocation): File {
171167
transformInvocation.context.variantName
172168
val unzippedDir = File(workDir, "unzipped")
169+
val unzippedExcludedDir = File(workDir, "unzipped-excluded")
173170
unzippedDir.mkdirs()
171+
unzippedExcludedDir.mkdirs()
172+
173+
val externalCodeDir = if (isTest(transformInvocation)) unzippedExcludedDir else unzippedDir
174174

175175
for (input in transformInvocation.inputs) {
176176
for (directoryInput in input.directoryInputs) {
@@ -181,11 +181,11 @@ class VendorTransform(
181181
val ownPackageNames = inferPackages(unzippedDir)
182182

183183
for (jar in configuration.resolve()) {
184-
unzipJar(jar, unzippedDir)
184+
unzipJar(jar, externalCodeDir)
185185
}
186-
val externalPackageNames = inferPackages(unzippedDir) subtract ownPackageNames
187-
val java = File(unzippedDir, "java")
188-
val javax = File(unzippedDir, "javax")
186+
val externalPackageNames = inferPackages(externalCodeDir) subtract ownPackageNames
187+
val java = File(externalCodeDir, "java")
188+
val javax = File(externalCodeDir, "javax")
189189
if (java.exists() || javax.exists()) {
190190
// JarJar unconditionally skips any classes whose package name starts with "java" or "javax".
191191
throw GradleException("Vendoring java or javax packages is not supported. " +
@@ -194,47 +194,15 @@ class VendorTransform(
194194
}
195195
val jar = File(workDir, "intermediate.jar")
196196
zipAll(unzippedDir, jar)
197-
val jar2 = File(workDir, "output.jar")
198-
val classesToExclude = classesToExclude(transformInvocation.context.variantName)
199-
println(classesToExclude.joinToString("\n"))
197+
val outputJar = File(workDir, "output.jar")
200198

201-
jarTransformer.transform(transformInvocation.context.variantName, jar, jar2, ownPackageNames, externalPackageNames, classesToExclude)
202-
return jar2
199+
jarTransformer.transform(jar, outputJar, externalPackageNames)
200+
return outputJar
203201
}
204202

205203
private fun inferPackages(dir: File): Set<String> {
206204
return dir.walk().filter { it.name.endsWith(".class") }.map { it.parentFile.toRelativeString(dir).replace('/', '.') }.toSet()
207205
}
208-
209-
private fun classesToExclude(variantName: String): Set<String> {
210-
val testedVariant = android.testVariants.find { it.name == variantName }
211-
if (testedVariant == null) {
212-
return setOf()
213-
}
214-
val dependencyJars = testedVariant.runtimeConfiguration.incoming.artifactView {
215-
attributes {
216-
attribute(Attribute.of("artifactType", String::class.java), "jar")
217-
}
218-
}.artifacts.artifactFiles
219-
return dependencyJars.asSequence().map {
220-
println("Checking $it")
221-
if (!it.path.contains("intermediates/full_jar")) {
222-
println("Skipping $it")
223-
return@map it
224-
}
225-
println("Replacing $it")
226-
return@map File(it.path.replace(Regex("(.+)/intermediates/full_jar/(.+)/full.jar$")) { match ->
227-
"${match.groups[1]?.value}/intermediates/runtime_library_classes_jar/${match.groups[2]?.value}/classes.jar"
228-
})
229-
}.flatMap {
230-
val entries = ZipFile(it).entries()
231-
entries.asIterator().asSequence()
232-
}.filter {
233-
!it.isDirectory && it.name.endsWith(".class")
234-
}.map {
235-
it.name.replace('/', '.').removeSuffix(".class")
236-
}.toSet()
237-
}
238206
}
239207

240208
fun unzipJar(jar: File, directory: File) {

buildSrc/src/test/kotlin/com/google/firebase/gradle/plugins/VendorTests.kt

+7-6
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ class VendorPluginTests {
7575
"com/example/com/google/errorprone/annotations/CanIgnoreReturnValue.class")
7676

7777
// ImmutableList is not used, so it should be stripped out.
78-
assertThat(classes).doesNotContain("com/google/common/collect/ImmutableList")
78+
assertThat(classes).doesNotContain("com/google/common/collect/ImmutableList.class")
7979
}
8080

8181
@Test
82-
fun `vendor dagger excluding javax transitive deps and not using it should not include dagger`() {
82+
fun `vendor dagger excluding javax transitive deps and not using it should include dagger`() {
8383
val classes = buildWith("""
8484
vendor ('com.google.dagger:dagger:2.27') {
8585
exclude group: "javax.inject", module: "javax.inject"
@@ -93,13 +93,14 @@ class VendorPluginTests {
9393
}
9494
""".trimIndent()))
9595
// expected classes
96-
assertThat(classes).containsExactly(
96+
assertThat(classes).containsAtLeast(
9797
"com/example/Hello.class",
98-
"com/example/BuildConfig.class")
98+
"com/example/BuildConfig.class",
99+
"com/example/dagger/Lazy.class")
99100
}
100101

101102
@Test
102-
fun `vendor dagger excluding javax transitive deps should only include used symbols`() {
103+
fun `vendor dagger excluding javax transitive deps should include dagger`() {
103104
val classes = buildWith("""
104105
vendor ('com.google.dagger:dagger:2.27') {
105106
exclude group: "javax.inject", module: "javax.inject"
@@ -116,7 +117,7 @@ class VendorPluginTests {
116117
}
117118
""".trimIndent()))
118119
// expected classes
119-
assertThat(classes).containsExactly(
120+
assertThat(classes).containsAtLeast(
120121
"com/example/Hello.class",
121122
"com/example/BuildConfig.class",
122123
"com/example/dagger/Module.class")

0 commit comments

Comments
 (0)