@@ -105,6 +105,11 @@ class RoomSchemaLocationWorkaround implements Workaround {
105
105
106
106
// Register the generated schemas to be merged back to the original specified schema directory
107
107
task. project. roomSchemaMergeLocations. registerMerge(schemaDestinationDir, taskSpecificSchemaDir)
108
+
109
+ // Seed the task-specific generated schema dir with the existing schemas
110
+ task. doFirst {
111
+ copyExistingSchemasToTaskSpecificTmpDir(task, schemaDestinationDir, taskSpecificSchemaDir)
112
+ }
108
113
}
109
114
)
110
115
@@ -126,7 +131,12 @@ class RoomSchemaLocationWorkaround implements Workaround {
126
131
}
127
132
128
133
doFirst {
129
- setRoomSchemaLocationToTaskSpecificDirForKaptWithoutKotlinc(task)
134
+ // Setup a task-specific tmp dir and populate it with the existing schemas
135
+ setRoomSchemaLocationToTaskSpecificTmpDirForKaptWithoutKotlinc(task)
136
+ }
137
+ doLast {
138
+ // Copy the generated schemas from the tmp dir to the tracked output dir
139
+ copyGeneratedSchemasToOutputDir(task, getTaskSpecificSchemaTmpDir(task), getTaskSpecificSchemaDir(task))
130
140
}
131
141
}
132
142
@@ -143,7 +153,12 @@ class RoomSchemaLocationWorkaround implements Workaround {
143
153
}
144
154
145
155
doFirst {
146
- setRoomSchemaLocationToTaskSpecificDirForKaptWithKotlinc(task)
156
+ // Setup a task-specific tmp dir and populate it with the existing schemas
157
+ setRoomSchemaLocationToTaskSpecificTmpDirForKaptWithKotlinc(task)
158
+ }
159
+ doLast {
160
+ // Copy the generated schemas from the tmp dir to the tracked output dir
161
+ copyGeneratedSchemasToOutputDir(task, getTaskSpecificSchemaTmpDir(task), getTaskSpecificSchemaDir(task))
147
162
}
148
163
}
149
164
}
@@ -154,6 +169,11 @@ class RoomSchemaLocationWorkaround implements Workaround {
154
169
return new File (schemaBaseDir, task. name)
155
170
}
156
171
172
+ static File getTaskSpecificSchemaTmpDir (Task task ) {
173
+ def schemaBaseDir = task. project. layout. buildDirectory. dir(" roomSchemas/tmp" ). get(). asFile
174
+ return new File (schemaBaseDir, task. name)
175
+ }
176
+
157
177
static def getCompilerPluginOptions (Task task ) {
158
178
def processorOptionsField = getAccessibleField(task. class, " processorOptions" )
159
179
def compilerPluginOptions = processorOptionsField. get(task)
@@ -213,20 +233,44 @@ class RoomSchemaLocationWorkaround implements Workaround {
213
233
task. project. roomSchemaMergeLocations. registerMerge(schemaDestinationDir, taskSpecificSchemaDir)
214
234
}
215
235
216
- private static void setRoomSchemaLocationToTaskSpecificDirForKaptWithoutKotlinc (Task task ) {
236
+ private static void copyExistingSchemasToTaskSpecificTmpDir (Task task , File existingSchemaDir , File taskSpecificTmpDir ) {
237
+ // populate the task-specific tmp dir with any existing (non-generated) schemas
238
+ // this allows other annotation processors that might operate on these schemas
239
+ // to find them via the schema location argument
240
+ task. project. sync {
241
+ from existingSchemaDir
242
+ into taskSpecificTmpDir
243
+ }
244
+ }
245
+
246
+ private static void copyGeneratedSchemasToOutputDir (Task task , File taskSpecificTmpDir , File outputDir ) {
247
+ // Copy the generated generated schemas from the task-specific tmp dir to the
248
+ // task-specific output dir. This dance prevents the kapt task from clearing out
249
+ // the existing schemas before the annotation processors run
250
+ task. project. sync {
251
+ from taskSpecificTmpDir
252
+ into outputDir
253
+ }
254
+ }
255
+
256
+ private static void setRoomSchemaLocationToTaskSpecificTmpDirForKaptWithoutKotlinc (Task task ) {
217
257
def processorOptions = getCompilerPluginOptions(task)
218
258
processorOptions. each { option ->
219
259
if (option. key == ROOM_SCHEMA_LOCATION ) {
220
- setOptionValue(option, getTaskSpecificSchemaDir(task). absolutePath)
260
+ def taskSpecificTmpDir = getTaskSpecificSchemaTmpDir(task)
261
+ copyExistingSchemasToTaskSpecificTmpDir(task, task. project. file(option. value), taskSpecificTmpDir)
262
+ setOptionValue(option, taskSpecificTmpDir. absolutePath)
221
263
}
222
264
}
223
265
}
224
266
225
- private static void setRoomSchemaLocationToTaskSpecificDirForKaptWithKotlinc (Task task ) {
267
+ private static void setRoomSchemaLocationToTaskSpecificTmpDirForKaptWithKotlinc (Task task ) {
226
268
def encodedOptions = getEncodedCompilerPluginOptions(task)
227
269
def apOptions = decode(encodedOptions. value)
228
270
if (apOptions. containsKey(ROOM_SCHEMA_LOCATION )) {
229
- apOptions[ROOM_SCHEMA_LOCATION ] = getTaskSpecificSchemaDir(task). absolutePath
271
+ def taskSpecificTmpDir = getTaskSpecificSchemaTmpDir(task)
272
+ copyExistingSchemasToTaskSpecificTmpDir(task, task. project. file(apOptions[ROOM_SCHEMA_LOCATION ]), taskSpecificTmpDir)
273
+ apOptions[ROOM_SCHEMA_LOCATION ] = taskSpecificTmpDir. absolutePath
230
274
setOptionValue(encodedOptions, encode(apOptions))
231
275
}
232
276
}
0 commit comments