fixes for lint baseline process

- Tasks of type AndroidLintAnalysisTask also use lint baselines, so we delete the baseline before running them

- If a lint task will be UP-TO-DATE, then we shouldn't delete the baseline because lint may not regenerate it. We only want to delete the baseline if lint is going to regenerate it, rather than depending on a task that always deletes it

- Setting the lint.baselines.continue system property before task execution seems to be required

- Due to a problem (b/205315927) where lint baselines sometimes don't get generated, we temporarily don't delete baselines when all of their issues are cleaned up

There is still one remaining problem that we expect after this change:

  If a user manually creates a new lint-baseline.xml and runs `./gradlew lintDebug -PupdateLintBaseline`, Gradle may cache our choice of baseline (based on the nonexistence of the previous nonexistence of lint-baseline.xml), and not use the newly created baseline

  However, in normal usage when a user generates the lint baseline via `./gradlew lintDebug -PupdateLintBaseline` and then subsequently runs `./gradlew lintDebug`, the removal of `-PupdateLintBaseline` should cause Gradle to invalidate the configuration cache.

Bug: 204926314
Bug: 193556665
Test: do something that changes lint baselines, then
      run `./gradlew lintDebug -PupdateLintBaseline`, then
      run `./gradlew lintDebug` and see that it passes

Change-Id: I13808d9e1553c769bcf8077021bf55ec1d72af5d
diff --git a/buildSrc/private/src/main/kotlin/androidx/build/LintConfiguration.kt b/buildSrc/private/src/main/kotlin/androidx/build/LintConfiguration.kt
index b354c12..55617fe87 100644
--- a/buildSrc/private/src/main/kotlin/androidx/build/LintConfiguration.kt
+++ b/buildSrc/private/src/main/kotlin/androidx/build/LintConfiguration.kt
@@ -19,7 +19,6 @@
 import androidx.build.dependencyTracker.AffectedModuleDetector
 import com.android.build.api.dsl.Lint
 import com.android.build.gradle.internal.lint.AndroidLintAnalysisTask
-import com.android.build.gradle.internal.lint.AndroidLintTask
 import com.android.build.gradle.internal.lint.AndroidLintTextOutputTask
 import com.google.common.io.Files
 import org.gradle.api.DefaultTask
@@ -180,14 +179,6 @@
         checkReleaseBuilds = false
     }
 
-    // task to delete autogenerated baselines before running lint which might regenerate them
-    val removeBaselineTask = project.tasks.register(
-        "removeLintBaseline",
-        RemoveBaselineTask::class.java,
-    ) { deleteTask ->
-        deleteTask.baselineFile.set(generatedLintBaseline)
-    }
-
     // task to copy autogenerated baselines back into the source tree
     val updateBaselineTask = project.tasks.register(
         "updateLintBaseline",
@@ -197,33 +188,24 @@
         copyTask.dest.set(lintBaseline)
     }
 
+    val projectGeneratedLintBaseline = generatedLintBaseline
+
     tasks.withType(AndroidLintAnalysisTask::class.java).configureEach { task ->
         // don't run too many copies of lint at once due to memory limitations
         task.usesService(
             task.project.gradle.sharedServices.registrations.getByName(LINT_SERVICE_NAME).service
         )
-
         if (updateLintBaseline) {
+            // Remove the generated baseline before running lint, so that lint won't try to use it
             task.doFirst {
-                // if we are updating baselines,
-                // then we don't want the creation of a new baseline to be considered a failure
-                System.setProperty(LINT_BASELINE_CONTINUE, "true")
+                if (projectGeneratedLintBaseline.exists()) {
+                    projectGeneratedLintBaseline.delete()
+                }
             }
-
-            // delete the autogenerated baseline before running lint
-            task.dependsOn(removeBaselineTask)
         }
     }
-    if (updateLintBaseline) {
-        tasks.withType(AndroidLintTextOutputTask::class.java).configureEach { task ->
-            task.doFirst {
-                // if we are updating baselines,
-                // then we don't want the creation of a new baseline to be considered a failure
-                System.setProperty(LINT_BASELINE_CONTINUE, "true")
-            }
-        }
-        tasks.withType(AndroidLintTask::class.java).configureEach { task ->
-            // after we're done generating new baselines, we copy them back into the source tree
+    tasks.withType(AndroidLintTextOutputTask::class.java).configureEach { task ->
+        if (updateLintBaseline) {
             task.finalizedBy(updateBaselineTask)
         }
     }
@@ -410,25 +392,11 @@
                 Files.copy(source, dest)
                 println("Updated baseline file ${dest.path}")
             } else {
-                dest.delete()
-                println("Deleted baseline file ${dest.path}")
+                // Temporarily (b/205315927) skipping deleting baselines because lint doesn't
+                // always generate all of the required baselines
+                // dest.delete()
+                // println("Deleted baseline file ${dest.path}")
             }
         }
     }
 }
-
-/**
- * Task that removes the specified lint baseline file.
- */
-abstract class RemoveBaselineTask : DefaultTask() {
-    @get:InputFiles // allows missing files
-    abstract val baselineFile: RegularFileProperty
-
-    @TaskAction
-    fun removeBaseline() {
-        val lintBaseline = baselineFile.get().asFile
-        if (lintBaseline.exists()) {
-            lintBaseline.delete()
-        }
-    }
-}
diff --git a/gradlew b/gradlew
index bbc6b8f2..abff1b4 100755
--- a/gradlew
+++ b/gradlew
@@ -47,6 +47,12 @@
 
 JAVA_OPTS="$JAVA_OPTS -Dkotlin.incremental.compilation=true" # b/188565660
 
+if [[ " ${@} " =~ " -PupdateLintBaseline " ]]; then
+  # remove when b/188666845 is complete
+  # Inform lint to not fail even when creating a baseline file
+  JAVA_OPTS="$JAVA_OPTS -Dlint.baselines.continue=true"
+fi
+
 APP_NAME="Gradle"
 APP_BASE_NAME=`basename "$0"`