Skip to content

Commit 14571ea

Browse files
authored
Merge pull request #143 from gradle/dd/config-cache
Make plugin compatible with configuration-cache
2 parents 0e84f8e + 19aa615 commit 14571ea

File tree

4 files changed

+64
-36
lines changed

4 files changed

+64
-36
lines changed

src/main/groovy/org/gradle/android/workarounds/CompileLibraryResourcesWorkaround_4_2.groovy

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ class CompileLibraryResourcesWorkaround_4_2 implements Workaround {
2222
return Class.forName('com.android.build.gradle.tasks.CompileLibraryResourcesTask')
2323
}
2424

25-
String propertyName = "inputDirectories"
25+
static final String PROPERTY_NAME = "inputDirectories"
2626

27-
public void setPropertyValue(Task task, ConfigurableFileCollection directoryProperty) {
28-
Field field = task.class.getDeclaredFields().find { it.name == "__${propertyName}__" }
27+
public static void setPropertyValue(Task task, ConfigurableFileCollection directoryProperty) {
28+
Field field = task.class.getDeclaredFields().find { it.name == "__${PROPERTY_NAME}__" }
2929
field.setAccessible(true)
3030
field.set(task, directoryProperty)
3131
}
@@ -38,20 +38,20 @@ class CompileLibraryResourcesWorkaround_4_2 implements Workaround {
3838
// Create a synthetic input with the original property value and RELATIVE path sensitivity
3939
task.inputs.files(originalPropertyValue)
4040
.withPathSensitivity(PathSensitivity.RELATIVE)
41-
.withPropertyName("${propertyName}.workaround")
41+
.withPropertyName("${PROPERTY_NAME}.workaround")
4242
.optional()
43-
project.gradle.taskGraph.beforeTask {
44-
if (it == task) {
45-
originalPropertyValue.from(task.getProperty(propertyName))
46-
def dummyProperty = project.objects.fileCollection()
47-
// Non-existent file to give the ConfigurableFileCollection a value.
48-
dummyProperty.setFrom(project.file('/doesnt-exist'))
49-
setPropertyValue(task, dummyProperty)
43+
project.gradle.taskGraph.whenReady {
44+
originalPropertyValue.from(task.getProperty(PROPERTY_NAME))
45+
def dummyProperty = project.objects.fileCollection()
46+
// Non-existent file to give the ConfigurableFileCollection a value.
47+
dummyProperty.setFrom(project.file('/doesnt-exist'))
48+
setPropertyValue(task, dummyProperty)
49+
50+
// Set the task property back to its original value
51+
task.doFirst {
52+
setPropertyValue(it, originalPropertyValue)
5053
}
51-
}
52-
// Set the task property back to its original value
53-
task.doFirst {
54-
setPropertyValue(task, originalPropertyValue)
54+
5555
}
5656
}
5757
}

src/main/groovy/org/gradle/android/workarounds/MergeResourcesWorkaround.groovy

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.gradle.android.workarounds
33
import org.gradle.android.AndroidIssue
44
import org.gradle.api.Project
55
import org.gradle.api.Task
6+
import org.gradle.api.execution.TaskExecutionGraph
67
import org.gradle.api.file.FileCollection
78
import org.gradle.api.provider.MapProperty
89
import org.gradle.api.tasks.PathSensitivity
@@ -27,16 +28,14 @@ class MergeResourcesWorkaround implements Workaround {
2728
task.inputs.files(originalResources.map {it.values() })
2829
.withPathSensitivity(PathSensitivity.RELATIVE)
2930
.withPropertyName("rawLocalResources.workaround")
30-
project.gradle.taskGraph.beforeTask {
31-
if (it == task) {
32-
task.resourcesComputer.resources.each { key, value -> originalResources.put(key, value) }
33-
task.resourcesComputer.resources.clear()
31+
project.gradle.taskGraph.whenReady { TaskExecutionGraph executionGraph ->
32+
task.resourcesComputer.resources.each { key, value -> originalResources.put(key, value) }
33+
task.resourcesComputer.resources.clear()
34+
// Set the source back to its original value before we execute the main task action
35+
task.doFirst {
36+
it.resourcesComputer.resources.putAll(originalResources.get())
3437
}
3538
}
36-
// Set the source back to its original value before we execute the main task action
37-
task.doFirst {
38-
task.resourcesComputer.resources.putAll(originalResources.get())
39-
}
4039
}
4140
}
4241

src/main/groovy/org/gradle/android/workarounds/RoomSchemaLocationWorkaround.groovy

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import org.gradle.api.file.ConfigurableFileCollection
99
import org.gradle.api.file.Directory
1010
import org.gradle.api.file.DirectoryProperty
1111
import org.gradle.api.file.DuplicatesStrategy
12+
import org.gradle.api.internal.file.FileOperations
1213
import org.gradle.api.model.ObjectFactory
1314
import org.gradle.api.provider.Provider
1415
import org.gradle.api.tasks.Internal
@@ -92,6 +93,8 @@ class RoomSchemaLocationWorkaround implements Workaround {
9293
def taskSpecificSchemaDir = project.objects.directoryProperty()
9394
taskSpecificSchemaDir.set(getTaskSpecificSchemaDir(task))
9495

96+
def fileOperations = project.fileOperations
97+
9598
// Add a command line argument provider to the task-specific list of providers
9699
task.options.compilerArgumentProviders.add(
97100
new JavaCompilerRoomSchemaLocationArgumentProvider(roomExtension.schemaLocationDir, taskSpecificSchemaDir)
@@ -102,7 +105,7 @@ class RoomSchemaLocationWorkaround implements Workaround {
102105

103106
// Seed the task-specific generated schema dir with the existing schemas
104107
task.doFirst {
105-
copyExistingSchemasToTaskSpecificTmpDir(task, roomExtension.schemaLocationDir, taskSpecificSchemaDir)
108+
copyExistingSchemasToTaskSpecificTmpDir(fileOperations, roomExtension.schemaLocationDir, taskSpecificSchemaDir)
106109
}
107110

108111
task.finalizedBy { roomExtension.schemaLocationDir.isPresent() ? mergeTask : null }
@@ -133,7 +136,7 @@ class RoomSchemaLocationWorkaround implements Workaround {
133136
def configureKaptTask = { Task task ->
134137
task.doFirst onlyIfAnnotationProcessorConfiguredForKapt(task) { KaptRoomSchemaLocationArgumentProvider provider ->
135138
// Populate the variant-specific schemas dir with the existing schemas
136-
copyExistingSchemasToTaskSpecificTmpDirForKapt(task, roomExtension.schemaLocationDir, provider)
139+
copyExistingSchemasToTaskSpecificTmpDirForKapt(project.fileOperations, roomExtension.schemaLocationDir, provider)
137140
}
138141

139142
task.doLast onlyIfAnnotationProcessorConfiguredForKapt(task) { KaptRoomSchemaLocationArgumentProvider provider ->
@@ -204,24 +207,25 @@ class RoomSchemaLocationWorkaround implements Workaround {
204207
return new File(schemaBaseDir, variantName)
205208
}
206209

207-
private static void copyExistingSchemasToTaskSpecificTmpDir(Task task, Provider<Directory> existingSchemaDir, Provider<Directory> taskSpecificTmpDir) {
210+
private static void copyExistingSchemasToTaskSpecificTmpDir(FileOperations fileOperations, Provider<Directory> existingSchemaDir, Provider<Directory> taskSpecificTmpDir) {
208211
// populate the task-specific tmp dir with any existing (non-generated) schemas
209212
// this allows other annotation processors that might operate on these schemas
210213
// to find them via the schema location argument
211214
if (existingSchemaDir.isPresent()) {
212-
task.project.sync {
213-
from existingSchemaDir
214-
into taskSpecificTmpDir
215+
216+
fileOperations.sync {
217+
it.from(existingSchemaDir)
218+
it.into(taskSpecificTmpDir)
215219
}
216220
}
217221
}
218222

219-
private static void copyExistingSchemasToTaskSpecificTmpDirForKapt(Task task, Provider<Directory> existingSchemaDir, KaptRoomSchemaLocationArgumentProvider provider) {
223+
private static void copyExistingSchemasToTaskSpecificTmpDirForKapt(FileOperations fileOperations, Provider<Directory> existingSchemaDir, KaptRoomSchemaLocationArgumentProvider provider) {
220224
// Derive the variant directory from the command line provider it is configured with
221225
def temporaryVariantSpecificSchemaDir = provider.temporarySchemaLocationDir
222226

223227
// Populate the variant-specific temporary schema dir with the existing schemas
224-
copyExistingSchemasToTaskSpecificTmpDir(task, existingSchemaDir, temporaryVariantSpecificSchemaDir)
228+
copyExistingSchemasToTaskSpecificTmpDir(fileOperations, existingSchemaDir, temporaryVariantSpecificSchemaDir)
225229
}
226230

227231
private static void copyGeneratedSchemasToOutputDirForKapt(Task task, KaptRoomSchemaLocationArgumentProvider provider) {
@@ -362,18 +366,22 @@ class RoomSchemaLocationWorkaround implements Workaround {
362366
* We don't want to create task dependencies on the compile/kapt tasks because we don't want to force execution
363367
* of those tasks if only a single variant is being assembled.
364368
*/
365-
static class RoomSchemaLocationMergeTask extends DefaultTask {
369+
static abstract class RoomSchemaLocationMergeTask extends DefaultTask {
370+
371+
// Using older internal API to maintain compatibility with Gradle 5.x
372+
@Inject abstract FileOperations getFileOperations()
373+
366374
@Internal
367375
MergeAssociations roomSchemaMergeLocations
368376

369377
@TaskAction
370378
void mergeSourcesToDestinations() {
371379
roomSchemaMergeLocations.mergeAssociations.each { destination, source ->
372380
println "Merging schemas to ${destination.get().asFile}"
373-
project.copy {
374-
duplicatesStrategy(DuplicatesStrategy.INCLUDE)
375-
into(destination)
376-
from(source)
381+
fileOperations.copy {
382+
it.duplicatesStrategy = DuplicatesStrategy.INCLUDE
383+
it.into(destination)
384+
it.from(source)
377385
}
378386
}
379387
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.gradle.android
2+
3+
class ConfigurationCachingTest extends AbstractTest {
4+
def "Can run with configuration cache"() {
5+
given:
6+
SimpleAndroidApp.builder(temporaryFolder.root, cacheDir)
7+
.withAndroidVersion(Versions.latestAndroidVersion())
8+
.withKotlinDisabled()
9+
.build()
10+
.writeProject()
11+
12+
when:
13+
def result = withGradleVersion(Versions.latestGradleVersion().version)
14+
.withProjectDir(temporaryFolder.root)
15+
.withArguments('--configuration-cache', 'assembleDebug')
16+
.build()
17+
18+
then:
19+
!result.output.contains("problems were found storing the configuration cache")
20+
}
21+
}

0 commit comments

Comments
 (0)