@@ -32,7 +32,6 @@ import org.gradle.api.GradleException
32
32
import org.gradle.api.Plugin
33
33
import org.gradle.api.Project
34
34
import org.gradle.api.artifacts.Configuration
35
- import org.gradle.api.attributes.Attribute
36
35
import org.gradle.api.logging.Logger
37
36
38
37
class VendorPlugin : Plugin <Project > {
@@ -59,6 +58,7 @@ class VendorPlugin : Plugin<Project> {
59
58
val android = project.extensions.getByType(LibraryExtension ::class .java)
60
59
61
60
android.registerTransform(VendorTransform (
61
+ android,
62
62
vendor,
63
63
JarJarTransformer (
64
64
parentPackageProvider = {
@@ -67,13 +67,12 @@ class VendorPlugin : Plugin<Project> {
67
67
jarJarProvider = { jarJar.resolve() },
68
68
project = project,
69
69
logger = project.logger),
70
- logger = project.logger,
71
- android = android))
70
+ logger = project.logger))
72
71
}
73
72
}
74
73
75
74
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 >)
77
76
}
78
77
79
78
class JarJarTransformer (
@@ -82,17 +81,11 @@ class JarJarTransformer(
82
81
private val project : Project ,
83
82
private val logger : Logger
84
83
) : 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 >) {
86
85
val parentPackage = parentPackageProvider()
87
- val rulesFile = File .createTempFile(" $ parentPackage- $variantName " , " .jarjar" )
86
+ val rulesFile = File .createTempFile(parentPackage, " .jarjar" )
88
87
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) {
96
89
it.println (" rule $externalPackageName .** $parentPackage .@0" )
97
90
}
98
91
}
@@ -102,17 +95,17 @@ class JarJarTransformer(
102
95
main = " org.pantsbuild.jarjar.Main"
103
96
classpath = project.files(jarJarProvider())
104
97
// 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)
106
99
systemProperties = mapOf (" verbose" to " true" , " misplacedClassStrategy" to " FATAL" )
107
100
}.assertNormalExitValue()
108
101
}
109
102
}
110
103
111
104
class VendorTransform (
105
+ private val android : LibraryExtension ,
112
106
private val configuration : Configuration ,
113
107
private val jarTransformer : JarTransformer ,
114
- private val logger : Logger ,
115
- private val android : LibraryExtension
108
+ private val logger : Logger
116
109
) :
117
110
Transform () {
118
111
override fun getName () = " firebaseVendorTransform"
@@ -132,7 +125,6 @@ class VendorTransform(
132
125
}
133
126
134
127
override fun transform (transformInvocation : TransformInvocation ) {
135
- println (transformInvocation.referencedInputs)
136
128
if (configuration.resolve().isEmpty()) {
137
129
logger.info(" Nothing to vendor. " +
138
130
" If you don't need vendor functionality please disable 'firebase-vendor' plugin. " +
@@ -167,10 +159,18 @@ class VendorTransform(
167
159
}
168
160
}
169
161
162
+ private fun isTest (transformInvocation : TransformInvocation ): Boolean {
163
+ return android.testVariants.find { it.name == transformInvocation.context.variantName } != null
164
+ }
165
+
170
166
private fun process (workDir : File , transformInvocation : TransformInvocation ): File {
171
167
transformInvocation.context.variantName
172
168
val unzippedDir = File (workDir, " unzipped" )
169
+ val unzippedExcludedDir = File (workDir, " unzipped-excluded" )
173
170
unzippedDir.mkdirs()
171
+ unzippedExcludedDir.mkdirs()
172
+
173
+ val externalCodeDir = if (isTest(transformInvocation)) unzippedExcludedDir else unzippedDir
174
174
175
175
for (input in transformInvocation.inputs) {
176
176
for (directoryInput in input.directoryInputs) {
@@ -181,11 +181,11 @@ class VendorTransform(
181
181
val ownPackageNames = inferPackages(unzippedDir)
182
182
183
183
for (jar in configuration.resolve()) {
184
- unzipJar(jar, unzippedDir )
184
+ unzipJar(jar, externalCodeDir )
185
185
}
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" )
189
189
if (java.exists() || javax.exists()) {
190
190
// JarJar unconditionally skips any classes whose package name starts with "java" or "javax".
191
191
throw GradleException (" Vendoring java or javax packages is not supported. " +
@@ -194,47 +194,15 @@ class VendorTransform(
194
194
}
195
195
val jar = File (workDir, " intermediate.jar" )
196
196
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" )
200
198
201
- jarTransformer.transform(transformInvocation.context.variantName, jar, jar2, ownPackageNames, externalPackageNames, classesToExclude )
202
- return jar2
199
+ jarTransformer.transform(jar, outputJar, externalPackageNames)
200
+ return outputJar
203
201
}
204
202
205
203
private fun inferPackages (dir : File ): Set <String > {
206
204
return dir.walk().filter { it.name.endsWith(" .class" ) }.map { it.parentFile.toRelativeString(dir).replace(' /' , ' .' ) }.toSet()
207
205
}
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
- }
238
206
}
239
207
240
208
fun unzipJar (jar : File , directory : File ) {
0 commit comments