Skip to content

Commit

Permalink
Fix issue with workaround when no room library is configured
Browse files Browse the repository at this point in the history
  • Loading branch information
ghale committed Feb 5, 2021
1 parent c7111b2 commit 714abdf
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class RoomSchemaLocationWorkaround implements Workaround {

// Add a command line argument provider to the task-specific list of providers
task.options.compilerArgumentProviders.add(
new JavaCompilerRoomSchemaLocationArgumentProvider(taskSpecificSchemaDir)
new JavaCompilerRoomSchemaLocationArgumentProvider(roomExtension.schemaLocationDir, taskSpecificSchemaDir)
)

// Register the generated schemas to be merged back to the original specified schema directory
Expand All @@ -118,7 +118,7 @@ class RoomSchemaLocationWorkaround implements Workaround {
applyToAllAndroidVariants(project) { variant ->
def variantSpecificSchemaDir = project.objects.directoryProperty()
variantSpecificSchemaDir.set(getVariantSpecificSchemaDir(project, "kapt${variant.name.capitalize()}Kotlin"))
variant.javaCompileOptions.annotationProcessorOptions.compilerArgumentProviders.add(new KaptRoomSchemaLocationArgumentProvider(variantSpecificSchemaDir))
variant.javaCompileOptions.annotationProcessorOptions.compilerArgumentProviders.add(new KaptRoomSchemaLocationArgumentProvider(roomExtension.schemaLocationDir, variantSpecificSchemaDir))

// Register the variant-specific directory with the merge task
roomExtension.registerOutputDirectory(variantSpecificSchemaDir)
Expand Down Expand Up @@ -290,10 +290,14 @@ class RoomSchemaLocationWorkaround implements Workaround {
}

static abstract class RoomSchemaLocationArgumentProvider implements CommandLineArgumentProvider {
@Internal
final Provider<Directory> configuredSchemaLocationDir

@OutputDirectory
Provider<Directory> schemaLocationDir
final Provider<Directory> schemaLocationDir

RoomSchemaLocationArgumentProvider(Provider<Directory> schemaLocationDir) {
RoomSchemaLocationArgumentProvider(Provider<Directory> configuredSchemaLocationDir, Provider<Directory> schemaLocationDir) {
this.configuredSchemaLocationDir = configuredSchemaLocationDir
this.schemaLocationDir = schemaLocationDir
}

Expand All @@ -304,7 +308,7 @@ class RoomSchemaLocationWorkaround implements Workaround {

@Override
Iterable<String> asArguments() {
if (schemaLocationDir.isPresent()) {
if (configuredSchemaLocationDir.isPresent()) {
return ["-A${ROOM_SCHEMA_LOCATION}=${schemaLocationPath}" as String]
} else {
return []
Expand All @@ -313,16 +317,16 @@ class RoomSchemaLocationWorkaround implements Workaround {
}

static class JavaCompilerRoomSchemaLocationArgumentProvider extends RoomSchemaLocationArgumentProvider {
JavaCompilerRoomSchemaLocationArgumentProvider(Provider<Directory> schemaLocationDir) {
super(schemaLocationDir)
JavaCompilerRoomSchemaLocationArgumentProvider(Provider<Directory> configuredSchemaLocationDir, Provider<Directory> schemaLocationDir) {
super(configuredSchemaLocationDir, schemaLocationDir)
}
}

static class KaptRoomSchemaLocationArgumentProvider extends RoomSchemaLocationArgumentProvider {
private Provider<Directory> temporarySchemaLocationDir

KaptRoomSchemaLocationArgumentProvider(Provider<Directory> schemaLocationDir) {
super(schemaLocationDir)
KaptRoomSchemaLocationArgumentProvider(Provider<Directory> configuredSchemaLocationDir, Provider<Directory> schemaLocationDir) {
super(configuredSchemaLocationDir, schemaLocationDir)
this.temporarySchemaLocationDir = schemaLocationDir.map {it.dir("../${it.asFile.name}Temp") }
}

Expand All @@ -333,8 +337,8 @@ class RoomSchemaLocationWorkaround implements Workaround {
}

static class MergeAssociations {
ObjectFactory objectFactory
Map<Provider<Directory>, ConfigurableFileCollection> mergeAssociations = [:]
final ObjectFactory objectFactory
final Map<Provider<Directory>, ConfigurableFileCollection> mergeAssociations = [:]

@Inject
MergeAssociations(ObjectFactory objectFactory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,34 @@ class RoomSchemaLocationWorkaroundTest extends AbstractTest {
assertNotExecuted(buildResult, ':app:mergeRoomSchemaLocations')
}
def "builds with no errors when room library is not on the classpath"() {
def androidVersion = Versions.getLatestVersionForAndroid("4.1")
SimpleAndroidApp.builder(temporaryFolder.root, cacheDir)
.withAndroidVersion(androidVersion)
.withNoRoomLibrary()
.build()
.writeProject()
cacheDir.deleteDir()
cacheDir.mkdirs()
when:
BuildResult buildResult = withGradleVersion(Versions.latestSupportedGradleVersionFor(androidVersion).version)
.forwardOutput()
.withProjectDir(temporaryFolder.root)
.withArguments(CLEAN_BUILD)
.build()
then:
noExceptionThrown()
and:
assertNotExecuted(buildResult, ':app:mergeRoomSchemaLocations')
and:
!buildResult.output.contains('warning: The following options were not recognized by any processor: \'[room.schemaLocation')
}
void assertNotExecuted(buildResult, String taskPath) {
assert !buildResult.tasks.collect {it.path }.contains(taskPath)
}
Expand Down
Loading

0 comments on commit 714abdf

Please sign in to comment.