Skip to content

Commit 9f50e32

Browse files
authored
Merge pull request #136 from gradle/no/test-broken
Only apply Room configuration to main compile tasks
2 parents d2e2047 + f77ccc3 commit 9f50e32

File tree

3 files changed

+143
-77
lines changed

3 files changed

+143
-77
lines changed

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

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ class RoomSchemaLocationWorkaround implements Workaround {
7575
roomSchemaMergeLocations = roomExtension.roomSchemaMergeLocations
7676
}
7777

78-
// Make sure that the annotation processor argument has not been explicitly configured in the Android
79-
// configuration (i.e. we only want this configured through the room extension
8078
def configureVariant = { variant ->
79+
// Make sure that the annotation processor argument has not been explicitly configured in the Android
80+
// configuration (i.e. we only want this configured through the room extension
8181
Map<String, String> arguments = variant.javaCompileOptions.annotationProcessorOptions.arguments
8282
if (arguments.containsKey(ROOM_SCHEMA_LOCATION)) {
8383
throw new IllegalStateException("""${this.class.name} cannot be used with an explicit '${ROOM_SCHEMA_LOCATION}' annotation processor argument. Please change this to configure the schema location directory via the 'room' project extension:
@@ -86,30 +86,31 @@ class RoomSchemaLocationWorkaround implements Workaround {
8686
}
8787
""")
8888
}
89-
}
9089

91-
applyToAllAndroidVariants(project, configureVariant)
90+
// Configure the annotation processor argument provider on the Java compile task
91+
javaCompileProvider.configure { JavaCompile task ->
92+
def taskSpecificSchemaDir = project.objects.directoryProperty()
93+
taskSpecificSchemaDir.set(getTaskSpecificSchemaDir(task))
9294

93-
project.tasks.withType(JavaCompile).configureEach { JavaCompile task ->
94-
def taskSpecificSchemaDir = project.objects.directoryProperty()
95-
taskSpecificSchemaDir.set(getTaskSpecificSchemaDir(task))
95+
// Add a command line argument provider to the task-specific list of providers
96+
task.options.compilerArgumentProviders.add(
97+
new JavaCompilerRoomSchemaLocationArgumentProvider(taskSpecificSchemaDir)
98+
)
9699

97-
// Add a command line argument provider to the task-specific list of providers
98-
task.options.compilerArgumentProviders.add(
99-
new JavaCompilerRoomSchemaLocationArgumentProvider(taskSpecificSchemaDir)
100-
)
100+
// Register the generated schemas to be merged back to the original specified schema directory
101+
roomExtension.registerOutputDirectory(taskSpecificSchemaDir)
101102

102-
// Register the generated schemas to be merged back to the original specified schema directory
103-
roomExtension.registerOutputDirectory(taskSpecificSchemaDir)
103+
// Seed the task-specific generated schema dir with the existing schemas
104+
task.doFirst {
105+
copyExistingSchemasToTaskSpecificTmpDir(task, roomExtension.schemaLocationDir, taskSpecificSchemaDir)
106+
}
104107

105-
// Seed the task-specific generated schema dir with the existing schemas
106-
task.doFirst {
107-
copyExistingSchemasToTaskSpecificTmpDir(task, roomExtension.schemaLocationDir, taskSpecificSchemaDir)
108+
task.finalizedBy { roomExtension.schemaLocationDir.isPresent() ? mergeTask : null }
108109
}
109-
110-
task.finalizedBy { roomExtension.schemaLocationDir.isPresent() ? mergeTask : null }
111110
}
112111

112+
applyToAllAndroidVariants(project, configureVariant)
113+
113114
project.plugins.withId("kotlin-kapt") {
114115
// The kapt task has a list of annotation processor providers which _is_ the list of providers
115116
// in the Android variant, so we can't just add a task-specific provider. To handle kapt tasks,
@@ -130,17 +131,17 @@ class RoomSchemaLocationWorkaround implements Workaround {
130131
// pre-seeding the directory with existing schemas should be a capability of the room annotation processor
131132
// somehow?
132133
def configureKaptTask = { Task task ->
133-
task.doFirst {
134+
task.doFirst onlyIfAnnotationProcessorConfiguredForKapt(task) { KaptRoomSchemaLocationArgumentProvider provider ->
134135
// Populate the variant-specific schemas dir with the existing schemas
135-
copyExistingSchemasToTaskSpecificTmpDirForKapt(task, roomExtension.schemaLocationDir)
136+
copyExistingSchemasToTaskSpecificTmpDirForKapt(task, roomExtension.schemaLocationDir, provider)
136137
}
137138

138-
task.doLast {
139+
task.doLast onlyIfAnnotationProcessorConfiguredForKapt(task) { KaptRoomSchemaLocationArgumentProvider provider ->
139140
// Copy the generated schemas into the registered output directory
140-
copyGeneratedSchemasToOutputDirForKapt(task)
141+
copyGeneratedSchemasToOutputDirForKapt(task, provider)
141142
}
142143

143-
task.finalizedBy { roomExtension.schemaLocationDir.isPresent() ? mergeTask : null }
144+
task.finalizedBy onlyIfAnnotationProcessorConfiguredForKapt(task) { roomExtension.schemaLocationDir.isPresent() ? mergeTask : null }
144145
}
145146

146147
project.tasks.withType(kaptWithoutKotlincTaskClass).configureEach(configureKaptTask)
@@ -165,11 +166,20 @@ class RoomSchemaLocationWorkaround implements Workaround {
165166
}
166167
}
167168

168-
private static def getKaptRoomSchemaLocationArgumentProvider(Task task) {
169+
private static KaptRoomSchemaLocationArgumentProvider getKaptRoomSchemaLocationArgumentProvider(Task task) {
169170
def annotationProcessorOptionProviders = getAccessibleField(task.class, "annotationProcessorOptionProviders").get(task)
170171
return annotationProcessorOptionProviders.flatten().find { it instanceof KaptRoomSchemaLocationArgumentProvider }
171172
}
172173

174+
private static Closure onlyIfAnnotationProcessorConfiguredForKapt(Task task, Closure<?> action) {
175+
return {
176+
def provider = getKaptRoomSchemaLocationArgumentProvider(task)
177+
if (provider != null) {
178+
action.call(provider)
179+
}
180+
}
181+
}
182+
173183
private static void applyToAllAndroidVariants(Project project, Closure<?> configureVariant) {
174184
project.plugins.withId("com.android.application") {
175185
def android = project.extensions.findByName("android")
@@ -206,21 +216,19 @@ class RoomSchemaLocationWorkaround implements Workaround {
206216
}
207217
}
208218

209-
private static void copyExistingSchemasToTaskSpecificTmpDirForKapt(Task task, Provider<Directory> existingSchemaDir) {
219+
private static void copyExistingSchemasToTaskSpecificTmpDirForKapt(Task task, Provider<Directory> existingSchemaDir, KaptRoomSchemaLocationArgumentProvider provider) {
210220
// Derive the variant directory from the command line provider it is configured with
211-
def provider = getKaptRoomSchemaLocationArgumentProvider(task)
212221
def temporaryVariantSpecificSchemaDir = provider.temporarySchemaLocationDir
213222

214223
// Populate the variant-specific temporary schema dir with the existing schemas
215224
copyExistingSchemasToTaskSpecificTmpDir(task, existingSchemaDir, temporaryVariantSpecificSchemaDir)
216225
}
217226

218-
private static void copyGeneratedSchemasToOutputDirForKapt(Task task) {
227+
private static void copyGeneratedSchemasToOutputDirForKapt(Task task, KaptRoomSchemaLocationArgumentProvider provider) {
219228
// Copy the generated generated schemas from the task-specific tmp dir to the
220229
// task-specific output dir. This dance prevents the kapt task from clearing out
221230
// the existing schemas before the annotation processors run
222231
// Derive the variant directory from the command line provider it is configured with
223-
def provider = getKaptRoomSchemaLocationArgumentProvider(task)
224232
def variantSpecificSchemaDir = provider.schemaLocationDir
225233
def temporaryVariantSpecificSchemaDir = provider.temporarySchemaLocationDir
226234

0 commit comments

Comments
 (0)