@@ -105,6 +105,11 @@ class RoomSchemaLocationWorkaround implements Workaround {
105105
106106 // Register the generated schemas to be merged back to the original specified schema directory
107107 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+ }
108113 }
109114 )
110115
@@ -126,7 +131,12 @@ class RoomSchemaLocationWorkaround implements Workaround {
126131 }
127132
128133 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))
130140 }
131141 }
132142
@@ -143,7 +153,12 @@ class RoomSchemaLocationWorkaround implements Workaround {
143153 }
144154
145155 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))
147162 }
148163 }
149164 }
@@ -154,6 +169,11 @@ class RoomSchemaLocationWorkaround implements Workaround {
154169 return new File (schemaBaseDir, task. name)
155170 }
156171
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+
157177 static def getCompilerPluginOptions (Task task ) {
158178 def processorOptionsField = getAccessibleField(task. class, " processorOptions" )
159179 def compilerPluginOptions = processorOptionsField. get(task)
@@ -213,20 +233,44 @@ class RoomSchemaLocationWorkaround implements Workaround {
213233 task. project. roomSchemaMergeLocations. registerMerge(schemaDestinationDir, taskSpecificSchemaDir)
214234 }
215235
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 ) {
217257 def processorOptions = getCompilerPluginOptions(task)
218258 processorOptions. each { option ->
219259 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)
221263 }
222264 }
223265 }
224266
225- private static void setRoomSchemaLocationToTaskSpecificDirForKaptWithKotlinc (Task task ) {
267+ private static void setRoomSchemaLocationToTaskSpecificTmpDirForKaptWithKotlinc (Task task ) {
226268 def encodedOptions = getEncodedCompilerPluginOptions(task)
227269 def apOptions = decode(encodedOptions. value)
228270 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
230274 setOptionValue(encodedOptions, encode(apOptions))
231275 }
232276 }
0 commit comments