summaryrefslogtreecommitdiff
path: root/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/tasks/BundleLibraryClasses.kt
blob: 301c40e600a5030761594e2eb86149fd8c7d2385 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.build.gradle.internal.tasks

import com.android.SdkConstants.FN_CLASSES_JAR
import com.android.build.api.artifact.ScopedArtifact
import com.android.build.api.variant.ScopedArtifacts
import com.android.build.gradle.internal.component.ComponentCreationConfig
import com.android.build.gradle.internal.databinding.DataBindingExcludeDelegate
import com.android.build.gradle.internal.databinding.configureFrom
import com.android.build.gradle.internal.dependency.getClassesDirFormat
import com.android.build.gradle.internal.packaging.JarCreatorFactory
import com.android.build.gradle.internal.packaging.JarCreatorType
import com.android.build.gradle.internal.profile.ProfileAwareWorkAction
import com.android.build.gradle.internal.publishing.AndroidArtifacts
import com.android.build.gradle.internal.publishing.AndroidArtifacts.ArtifactType.CLASSES_DIR
import com.android.build.gradle.internal.publishing.AndroidArtifacts.ClassesDirFormat.CONTAINS_CLASS_FILES_ONLY
import com.android.build.gradle.internal.publishing.AndroidArtifacts.ClassesDirFormat.CONTAINS_SINGLE_JAR
import com.android.build.gradle.internal.publishing.AndroidArtifacts.PublishedConfigType
import com.android.build.gradle.internal.scope.InternalArtifactType
import com.android.build.gradle.internal.tasks.factory.VariantTaskCreationAction
import com.android.build.gradle.internal.utils.setDisallowChanges
import com.android.build.gradle.options.BooleanOption
import com.android.build.gradle.tasks.toSerializable
import com.android.builder.dexing.isJarFile
import com.android.builder.files.SerializableFileChanges
import com.android.ide.common.attribution.TaskCategoryLabel
import com.android.utils.FileUtils
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.FileCollection
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Classpath
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskProvider
import org.gradle.work.DisableCachingByDefault
import org.gradle.work.Incremental
import org.gradle.work.InputChanges
import java.io.File
import java.lang.IllegalStateException
import java.util.function.Predicate
import java.util.regex.Pattern
import java.util.zip.Deflater

private val CLASS_PATTERN = Pattern.compile(".*\\.class$")
private val META_INF_PATTERN = Pattern.compile("^META-INF/.*$")

/** Common interface for bundle classes tasks to make configuring them easier. */
interface BundleLibraryClassesInputs {
    @get:Input
    val namespace: Property<String>

    @get:Classpath
    @get:Optional
    val classes: ConfigurableFileCollection

    @get:Input
    val packageRClass: Property<Boolean>

    @get:Nested
    @get:Optional
    val dataBindingExcludeDelegate: Property<DataBindingExcludeDelegate>

    @get:Input
    val jarCreatorType: Property<JarCreatorType>
}

private fun BundleLibraryClassesInputs.configure(
    creationConfig: ComponentCreationConfig,
    inputs: FileCollection,
    packageRClass: Boolean
) {
    namespace.setDisallowChanges(creationConfig.namespace)
    classes.from(inputs)
    if (packageRClass) {
        classes.from(
                creationConfig.artifacts.get(InternalArtifactType.COMPILE_R_CLASS_JAR)
        )
    }
    this.packageRClass.setDisallowChanges(packageRClass)
    jarCreatorType.setDisallowChanges(creationConfig.global.jarCreatorType)

    dataBindingExcludeDelegate.configureFrom(creationConfig)
}

private fun BundleLibraryClassesInputs.configureWorkerActionParams(
    params: BundleLibraryClassesWorkAction.Params,
    inputChanges: InputChanges?,
    output: Provider<File>
) {

    val incrementalChanges = if (inputChanges?.isIncremental == true) {
        inputChanges.getFileChanges(classes)
    } else {
        emptyList()
    }
    params.namespace.set(namespace)
    params.toIgnore.set(
        dataBindingExcludeDelegate.orNull?.getExcludedClassList(namespace.get()) ?: listOf()
    )
    params.output.set(output)
    // Ignore non-existent files (without this, ResourceNamespaceTest would fail).
    params.input.from(classes.files.filter { file -> file.exists() }.toSet())
    params.incremental.set(inputChanges?.isIncremental ?: false)
    params.inputChanges.set(incrementalChanges.toSerializable())
    params.packageRClass.set(packageRClass)
    params.jarCreatorType.set(jarCreatorType)
}

/**
 * Bundles all library classes to a directory.
 *
 * Caching disabled by default for this task because the task does very little work.
 * Input files are copied, unchanged, to an Output directory.
 * Calculating cache hit/miss and fetching results is likely more expensive than
 * simply executing the task.
 */
@DisableCachingByDefault
@BuildAnalyzer(taskCategoryLabels = [TaskCategoryLabel.ZIPPING])
abstract class BundleLibraryClassesDir: NewIncrementalTask(), BundleLibraryClassesInputs {

    @get:OutputDirectory
    abstract val output: DirectoryProperty

    @get:Classpath
    @get:Incremental
    abstract override val classes: ConfigurableFileCollection

    override fun doTaskAction(inputChanges: InputChanges) {
        workerExecutor.noIsolation().submit(
            BundleLibraryClassesWorkAction::class.java
        ) {
            it.initializeFromAndroidVariantTask(this)
            configureWorkerActionParams(it, inputChanges, output.asFile)
        }
    }

    class CreationAction(
        creationConfig: ComponentCreationConfig
    ) : VariantTaskCreationAction<BundleLibraryClassesDir, ComponentCreationConfig>(
        creationConfig
    ) {

        private val inputs: FileCollection

        init {
            // Because ordering matters for TransformAPI, we need to fetch classes from the
            // transform pipeline as soon as this creation action is instantiated.
            @Suppress("DEPRECATION") // Legacy support
            inputs =
                creationConfig.transformManager.getPipelineOutputAsFileCollection { types, scopes ->
                    types.contains(com.android.build.api.transform.QualifiedContent.DefaultContentType.CLASSES)
                            && scopes.size == 1 && scopes.contains(com.android.build.api.transform.QualifiedContent.Scope.PROJECT)
                }
        }

        override val name: String = creationConfig.computeTaskName("bundleLibRuntimeToDir")

        override val type: Class<BundleLibraryClassesDir> = BundleLibraryClassesDir::class.java

        override fun handleProvider(taskProvider: TaskProvider<BundleLibraryClassesDir>) {
            super.handleProvider(taskProvider)
            creationConfig.artifacts
                .setInitialProvider(taskProvider, BundleLibraryClassesDir::output)
                .on(InternalArtifactType.RUNTIME_LIBRARY_CLASSES_DIR)
        }

        override fun configure(task: BundleLibraryClassesDir) {
            super.configure(task)
            task.configure(creationConfig, inputs, false)
        }
    }
}

/**
 * Bundles all library classes to a jar.
 *
 * Caching disabled by default for this task because the task does very little work.
 * Input files are collected, unchanged, into a new Jar file.
 * Calculating cache hit/miss and fetching results is likely more expensive than
 * simply executing the task.
 */
@DisableCachingByDefault
@BuildAnalyzer(taskCategoryLabels = [TaskCategoryLabel.ZIPPING])
abstract class BundleLibraryClassesJar : NonIncrementalTask(), BundleLibraryClassesInputs {

    @get:OutputFile
    abstract val output: RegularFileProperty

    override fun doTaskAction() {
        workerExecutor.noIsolation().submit(
            BundleLibraryClassesWorkAction::class.java
        ) {
            it.initializeFromAndroidVariantTask(this)
            configureWorkerActionParams(it, null, output.asFile)
        }
    }

    class CreationAction(
        creationConfig: ComponentCreationConfig,
        private val publishedType: PublishedConfigType
    ) : VariantTaskCreationAction<BundleLibraryClassesJar, ComponentCreationConfig>(
        creationConfig
    ) {

        private val inputs: FileCollection

        init {
            check(
                publishedType == PublishedConfigType.API_ELEMENTS
                        || publishedType == PublishedConfigType.RUNTIME_ELEMENTS
            ) { "Library classes bundling is supported only for api and runtime." }
            // Because ordering matters for TransformAPI, we need to fetch classes from the
            // transform pipeline as soon as this creation action is instantiated.
            @Suppress("DEPRECATION") // Legacy support
            inputs = if (publishedType == PublishedConfigType.RUNTIME_ELEMENTS) {
                creationConfig.transformManager.getPipelineOutputAsFileCollection { types, scopes ->
                    types.contains(com.android.build.api.transform.QualifiedContent.DefaultContentType.CLASSES)
                            && scopes.size == 1 && scopes.contains(com.android.build.api.transform.QualifiedContent.Scope.PROJECT)
                }
            } else {
                creationConfig.artifacts
                    .forScope(ScopedArtifacts.Scope.PROJECT)
                    .getFinalArtifacts(ScopedArtifact.CLASSES)
            }
        }

        override val name: String = creationConfig.computeTaskName(
            if (publishedType == PublishedConfigType.API_ELEMENTS) {
                "bundleLibCompileToJar"
            } else {
                "bundleLibRuntimeToJar"
            }
        )

        override val type: Class<BundleLibraryClassesJar> = BundleLibraryClassesJar::class.java

        override fun handleProvider(
            taskProvider: TaskProvider<BundleLibraryClassesJar>
        ) {
            super.handleProvider(taskProvider)

            creationConfig.artifacts
                .setInitialProvider(
                    taskProvider,
                    BundleLibraryClassesJar::output
                ).withName(creationConfig.getArtifactName(FN_CLASSES_JAR)).let {
                    if (publishedType == PublishedConfigType.API_ELEMENTS) {
                        it.on(InternalArtifactType.COMPILE_LIBRARY_CLASSES_JAR)
                    } else {
                        it.on(InternalArtifactType.RUNTIME_LIBRARY_CLASSES_JAR)
                    }
                }
        }

        override fun configure(task: BundleLibraryClassesJar) {
            super.configure(task)
            val packageRClass =
                creationConfig.services.projectOptions[BooleanOption.COMPILE_CLASSPATH_LIBRARY_R_CLASSES] &&
                        publishedType == PublishedConfigType.API_ELEMENTS &&
                        creationConfig.buildFeatures.androidResources
            task.configure(creationConfig, inputs, packageRClass)
        }
    }
}

/** Packages files to jar using the provided filter. */
abstract class BundleLibraryClassesWorkAction : ProfileAwareWorkAction<BundleLibraryClassesWorkAction.Params>() {
    abstract class Params: ProfileAwareWorkAction.Parameters() {
        abstract val namespace: Property<String>
        abstract val toIgnore: ListProperty<String>
        abstract val output: Property<File>
        abstract val input: ConfigurableFileCollection
        abstract val incremental: Property<Boolean>
        abstract val inputChanges: Property<SerializableFileChanges>
        abstract val packageRClass: Property<Boolean>
        abstract val jarCreatorType: Property<JarCreatorType>
    }

    override fun run() {
        val ignorePatterns =
            (LibraryAarJarsTask.getDefaultExcludes(
                packageR = parameters.packageRClass.get()
            ) + parameters.toIgnore.get())
                .map { Pattern.compile(it) }

        val predicate = Predicate<String> { relativePath ->
            val normalizedPath = FileUtils.toSystemIndependentPath(relativePath)
            (CLASS_PATTERN.matcher(normalizedPath).matches()
                    || META_INF_PATTERN.matcher(normalizedPath).matches())
                    && !ignorePatterns.any { it.matcher(normalizedPath).matches() }
        }


        if (isJarFile(parameters.output.get())) {
            zipFilesNonIncrementally(parameters.input.files, parameters.output.get(), predicate, parameters.jarCreatorType.get())
        } else {
            when (getClassesDirFormat(parameters.input.files)) {
                CONTAINS_SINGLE_JAR -> {
                    FileUtils.deleteRecursivelyIfExists(parameters.output.get())
                    FileUtils.mkdirs(parameters.output.get())
                    zipFilesNonIncrementally(
                        parameters.input.files,
                        parameters.output.get().resolve(FN_CLASSES_JAR),
                        predicate,
                        parameters.jarCreatorType.get()
                    )
                }
                CONTAINS_CLASS_FILES_ONLY -> {
                    if (parameters.incremental.get()) {
                        if (getClassesDirFormat(parameters.output.get()) == CONTAINS_SINGLE_JAR) {
                            // It's not trivial to update the output directory incrementally from
                            // directory format CONTAINS_SINGLE_JAR to CONTAINS_CLASS_FILES_ONLY, so
                            // let's create it non-incrementally.
                            copyFilesNonIncrementally(parameters.input.files, parameters.output.get(), predicate)
                        } else {
                            copyFilesIncrementally(parameters.inputChanges.get(), parameters.output.get(), predicate)
                        }
                    } else {
                        copyFilesNonIncrementally(parameters.input.files, parameters.output.get(), predicate)
                    }
                }
            }
        }
    }

    /**
     * Returns the format of the directory with artifact type [CLASSES_DIR]. The format
     * depends on whether the input classes contain jars nor not. See
     * [AndroidArtifacts.ClassesDirFormat] for more details.
     */
    private fun getClassesDirFormat(inputClassRoots: Set<File>): AndroidArtifacts.ClassesDirFormat {
        return if (inputClassRoots.any { isJarFile(it) }) {
            CONTAINS_SINGLE_JAR
        } else {
            CONTAINS_CLASS_FILES_ONLY
        }
    }

    private fun zipFilesNonIncrementally(
        input: Set<File>,
        outputJar: File,
        filter: Predicate<String>,
        jarCreatorType: JarCreatorType
    ) {
        FileUtils.deleteIfExists(outputJar)
        FileUtils.mkdirs(outputJar.parentFile)
        JarCreatorFactory.make(outputJar.toPath(), filter, jarCreatorType).use { jarCreator ->
            // Don't compress because compressing takes extra time, and this jar doesn't go into any
            // APKs or AARs
            jarCreator.setCompressionLevel(Deflater.NO_COMPRESSION)
            input.forEach { base ->
                if (base.isDirectory) {
                    jarCreator.addDirectory(base.toPath())
                } else {
                    check(isJarFile(base)) { "Expected jar file but found ${base.path}." }
                    jarCreator.addJar(base.toPath())
                }
            }
        }
    }

    private fun copyFilesNonIncrementally(
        input: Set<File>,
        outputDir: File,
        filter: Predicate<String>
    ) {
        FileUtils.deleteRecursivelyIfExists(outputDir)
        FileUtils.mkdirs(outputDir)
        input.forEach { classRoot ->
            check(classRoot.isDirectory) { "Expected directory but found ${classRoot.path}." }
            for (it in classRoot.walk()) {
                val relativePath = it.relativeTo(classRoot).path
                if (relativePath.isEmpty() || !filter.test(relativePath)) continue

                val outputLocation = outputDir.resolve(relativePath)
                if (it.isFile) {
                    FileUtils.mkdirs(outputLocation.parentFile)
                    if (outputLocation.isFile) {
                        throw IllegalStateException("File $outputLocation already exists, it cannot be overwritten by $it.")
                    }
                    FileUtils.copyFile(it, outputLocation)
                } else if (it.isDirectory && !outputLocation.isDirectory) {
                    FileUtils.mkdirs(outputLocation)
                }
            }
        }
    }

    private fun copyFilesIncrementally(
        inputChanges: SerializableFileChanges,
        outputDir: File,
        filter: Predicate<String>
    ) {
        (inputChanges.removedFiles + inputChanges.modifiedFiles).forEach {
            val staleOutputFile = outputDir.resolve(it.normalizedPath)
            FileUtils.deleteRecursivelyIfExists(staleOutputFile)
        }
        (inputChanges.modifiedFiles + inputChanges.addedFiles).forEach {
            // If an added file is one of the roots of the FileCollection, normalizedPath will be
            // the file name, not an empty string, but we can probably ignore this edge case.
            val relativePath = it.normalizedPath
            if (relativePath.isEmpty() || !filter.test(relativePath)) return@forEach

            val outputLocation = outputDir.resolve(relativePath)
            if (it.file.isFile) {
                FileUtils.mkdirs(outputLocation.parentFile)
                if (outputLocation.isFile) {
                    throw IllegalStateException("File $outputLocation already exists, it cannot be overwritten by $it.")
                }
                FileUtils.copyFile(it.file, outputLocation)
            } else if (it.file.isDirectory && !outputLocation.isDirectory) {
                FileUtils.mkdirs(outputLocation)
            }
        }
    }
}