Snap for 8604520 from a6c750a7f91516d17774c86cbcfeac679c1179b5 to studio-chipmunk-release

Change-Id: Ia28bbd5d4129cc8a24fd65b0dccd5300a9333e1b
diff --git a/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/ide/dependencies/FullDependencyGraphBuilder.kt b/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/ide/dependencies/FullDependencyGraphBuilder.kt
index 18d8790..ef9a306 100644
--- a/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/ide/dependencies/FullDependencyGraphBuilder.kt
+++ b/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/ide/dependencies/FullDependencyGraphBuilder.kt
@@ -82,7 +82,7 @@
 
         for (artifact in unvisitedArtifacts) {
             val library = libraryService.getLibrary(artifact)
-            items.add(GraphItemImpl(library.key, null, listOf()))
+            items.add(GraphItemImpl(library.key, null))
         }
 
         return items.toList()
@@ -188,19 +188,22 @@
         }
 
         if (library != null) {
-            // create the GraphItem for the library, starting by recursively computing the children
-            val children =
-                    dependency.selected.getDependenciesForVariant(variant).mapNotNull {
-                        handleDependency(it, visited, artifactMap)
-                    }
-
-            return GraphItemImpl(
+            // Create GraphItem for the library first and add it to cache in order to avoid cycles.
+            // See http://b/232075280.
+            val libraryGraphItem = GraphItemImpl(
                 library.key,
-                null,
-                children
+                null
             ).also {
                 visited[variant] = it
             }
+
+            // Now visit children, and add them as dependencies
+            dependency.selected.getDependenciesForVariant(variant).forEach {
+                handleDependency(it, visited, artifactMap)?.let { childGraphItem ->
+                    libraryGraphItem.addDependency(childGraphItem)
+                }
+            }
+            return libraryGraphItem
         }
 
         return null
diff --git a/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/ide/v2/GraphItemImpl.kt b/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/ide/v2/GraphItemImpl.kt
index f4c5bf4..4318f57 100644
--- a/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/ide/v2/GraphItemImpl.kt
+++ b/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/ide/v2/GraphItemImpl.kt
@@ -25,11 +25,20 @@
 data class GraphItemImpl(
     override val key: String,
     override val requestedCoordinates: String?,
-    override val dependencies: List<GraphItem>
 ) : GraphItem, Serializable {
+
+    private val _dependencies = mutableListOf<GraphItem>()
+
+    override val dependencies: List<GraphItem>
+        get() = _dependencies
+
+    internal fun addDependency(dependency: GraphItem) {
+        _dependencies.add(dependency)
+    }
+
     companion object {
         @JvmStatic
-        private val serialVersionUID: Long = 1L
+        private val serialVersionUID: Long = 2L
     }
 
 }
diff --git a/build-system/integration-test/application/src/test/java/com/android/build/gradle/integration/model/CyclicPomDependencyTest.kt b/build-system/integration-test/application/src/test/java/com/android/build/gradle/integration/model/CyclicPomDependencyTest.kt
new file mode 100644
index 0000000..b26ee55
--- /dev/null
+++ b/build-system/integration-test/application/src/test/java/com/android/build/gradle/integration/model/CyclicPomDependencyTest.kt
@@ -0,0 +1,117 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.build.gradle.integration.model
+
+import com.android.build.gradle.integration.common.fixture.model.ModelComparator
+import com.android.build.gradle.integration.common.fixture.testprojects.PluginType
+import com.android.build.gradle.integration.common.fixture.testprojects.createGradleProject
+import com.android.build.gradle.integration.common.fixture.testprojects.prebuilts.setUpHelloWorld
+import com.android.builder.model.v2.ide.SyncIssue
+import org.junit.Rule
+import org.junit.Test
+
+/** Regression test for b/232075280. */
+class CyclicPomDependencyTest: ModelComparator() {
+
+    private val buildFileTemplate = { libName: String, dependencyName: String ->
+        """
+            group = "com.foo"
+            version = "1.0"
+
+            Configuration customPublishing  = configurations.create("customPublishing")
+            customPublishing.setCanBeConsumed(true)
+            customPublishing.setCanBeResolved(false)
+            customPublishing.attributes.attribute(
+                    TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE,
+                    objects.named(TargetJvmEnvironment.class, TargetJvmEnvironment.STANDARD_JVM)
+            )
+            dependencies.add("customPublishing", 'com.foo:$dependencyName:1.0')
+            customPublishing.outgoing.artifact(tasks.getByName("jar"))
+
+            abstract class FactoryAccessor {
+                @javax.inject.Inject
+                abstract SoftwareComponentFactory getFactory();
+            }
+            FactoryAccessor factoryAccessor = project.objects.newInstance(FactoryAccessor.class)
+            AdhocComponentWithVariants component = factoryAccessor.getFactory().adhoc("custom")
+            component.addVariantsFromConfiguration(customPublishing) {}
+            project.components.add(component)
+
+            tasks.withType(GenerateModuleMetadata) {
+                enabled = false
+            }
+
+            publishing {
+                repositories {
+                    maven { url = '../repo' }
+                }
+                publications {
+                    mavenJava(MavenPublication) {
+                        artifactId = "$libName"
+                        from components.custom
+                    }
+                }
+            }
+        """.trimIndent()
+    }
+
+    @get:Rule
+    val project = createGradleProject {
+        subProject(":app") {
+            plugins.add(PluginType.ANDROID_APP)
+            android {
+                setUpHelloWorld()
+            }
+            appendToBuildFile {
+                """
+                    repositories {
+                        maven {
+                            url { '../repo' }
+                        }
+                    }
+                    dependencies {
+                        implementation("com.foo:bar1:1.0")
+                    }
+                """.trimIndent()
+            }
+        }
+        subProject(":bar1") {
+            plugins.add(PluginType.JAVA_LIBRARY)
+            plugins.add(PluginType.MAVEN_PUBLISH)
+            appendToBuildFile {
+                buildFileTemplate("bar1", "bar2")
+            }
+        }
+        subProject(":bar2") {
+            plugins.add(PluginType.JAVA_LIBRARY)
+            plugins.add(PluginType.MAVEN_PUBLISH)
+            appendToBuildFile {
+                buildFileTemplate("bar2", "bar1")
+            }
+        }
+    }
+
+    @Test
+    fun `test models`() {
+        project.executor().run(":bar1:publish", ":bar2:publish")
+        val result = project.modelV2()
+            .ignoreSyncIssues(SyncIssue.SEVERITY_WARNING)
+            .fetchModels(variantName = "debug")
+
+        with(result).compareVariantDependencies(goldenFile = "VariantDependencies")
+    }
+}
diff --git a/build-system/integration-test/application/src/test/resources/com/android/build/gradle/integration/model/CyclicPomDependencyTest_VariantDependencies.txt b/build-system/integration-test/application/src/test/resources/com/android/build/gradle/integration/model/CyclicPomDependencyTest_VariantDependencies.txt
new file mode 100644
index 0000000..46a329a
--- /dev/null
+++ b/build-system/integration-test/application/src/test/resources/com/android/build/gradle/integration/model/CyclicPomDependencyTest_VariantDependencies.txt
@@ -0,0 +1,205 @@
+> VariantDependencies:
+   - name                 = "debug"
+   > mainArtifact:
+      - compileDependencies:
+         - com.foo|bar1|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-api|com.foo:bar1:1.0:
+            - requestedCoordinates = (null)
+            - dependencies:
+               - com.foo|bar2|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-api|com.foo:bar2:1.0:
+                  - requestedCoordinates = (null)
+                  - dependencies:
+                     - com.foo|bar1|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-api|com.foo:bar1:1.0:
+                        - dependencies = "*visited*"
+      - runtimeDependencies:
+         - com.foo|bar1|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-runtime|com.foo:bar1:1.0:
+            - requestedCoordinates = (null)
+            - dependencies:
+               - com.foo|bar2|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-runtime|com.foo:bar2:1.0:
+                  - requestedCoordinates = (null)
+                  - dependencies:
+                     - com.foo|bar1|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-runtime|com.foo:bar1:1.0:
+                        - dependencies = "*visited*"
+      - unresolvedDependencies = []
+   < mainArtifact
+   > androidTestArtifact:
+      > compileDependencies:
+         - :|:app|debug|com.android.build.api.attributes.AgpVersionAttr>{AGP_Version}, com.android.build.gradle.internal.attributes.VariantAttr>debug, org.gradle.libraryelements>jar, org.gradle.usage>java-api|project:app:unspecified:
+            - requestedCoordinates = (null)
+            - dependencies         = []
+         - com.foo|bar1|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-api|com.foo:bar1:1.0:
+            - requestedCoordinates = (null)
+            - dependencies:
+               - com.foo|bar2|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-api|com.foo:bar2:1.0:
+                  - requestedCoordinates = (null)
+                  - dependencies:
+                     - com.foo|bar1|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-api|com.foo:bar1:1.0:
+                        - dependencies = "*visited*"
+      < compileDependencies
+      - runtimeDependencies    = []
+      - unresolvedDependencies = []
+   < androidTestArtifact
+   > unitTestArtifact:
+      > compileDependencies:
+         - :|:app|debug|com.android.build.api.attributes.AgpVersionAttr>{AGP_Version}, com.android.build.gradle.internal.attributes.VariantAttr>debug, org.gradle.libraryelements>jar, org.gradle.usage>java-api|project:app:unspecified:
+            - requestedCoordinates = (null)
+            - dependencies         = []
+         - com.foo|bar1|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-api|com.foo:bar1:1.0:
+            - requestedCoordinates = (null)
+            - dependencies:
+               - com.foo|bar2|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-api|com.foo:bar2:1.0:
+                  - requestedCoordinates = (null)
+                  - dependencies:
+                     - com.foo|bar1|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-api|com.foo:bar1:1.0:
+                        - dependencies = "*visited*"
+      < compileDependencies
+      > runtimeDependencies:
+         > :|:app|debug|com.android.build.api.attributes.AgpVersionAttr>{AGP_Version}, com.android.build.gradle.internal.attributes.VariantAttr>debug, org.gradle.usage>java-runtime|project:app:unspecified:
+            - requestedCoordinates = (null)
+            - dependencies:
+               - com.foo|bar1|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-runtime|com.foo:bar1:1.0:
+                  - requestedCoordinates = (null)
+                  - dependencies:
+                     - com.foo|bar2|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-runtime|com.foo:bar2:1.0:
+                        - requestedCoordinates = (null)
+                        - dependencies:
+                           - com.foo|bar1|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-runtime|com.foo:bar1:1.0:
+                              - dependencies = "*visited*"
+         < :|:app|debug|com.android.build.api.attributes.AgpVersionAttr>{AGP_Version}, com.android.build.gradle.internal.attributes.VariantAttr>debug, org.gradle.usage>java-runtime|project:app:unspecified
+         - com.foo|bar1|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-runtime|com.foo:bar1:1.0:
+            - dependencies = "*visited*"
+      < runtimeDependencies
+      - unresolvedDependencies = []
+   < unitTestArtifact
+   - testFixturesArtifact = (null)
+   > libraries:
+      > :|:app|debug|com.android.build.api.attributes.AgpVersionAttr>{AGP_Version}, com.android.build.gradle.internal.attributes.VariantAttr>debug, org.gradle.libraryelements>jar, org.gradle.usage>java-api|project:app:unspecified:
+         - type               = PROJECT
+         - artifact           = {PROJECT}/app/build/intermediates/compile_app_classes_jar/debug/classes.jar{!}
+         > projectInfo:
+            - buildId        = ":"
+            - projectPath    = ":app"
+            - isTestFixtures = false
+            - buildType      = "debug"
+            - productFlavors = []
+            - attributes:
+               * "com.android.build.api.attributes.AgpVersionAttr -> {AGP_Version}"
+               * "com.android.build.gradle.internal.attributes.VariantAttr -> debug"
+               * "org.gradle.libraryelements -> jar"
+               * "org.gradle.usage -> java-api"
+            - capabilities:
+               * "project:app:unspecified"
+         < projectInfo
+         - libraryInfo        = (null)
+         - lintJar            = (null)
+         - androidLibraryData = (null)
+      < :|:app|debug|com.android.build.api.attributes.AgpVersionAttr>{AGP_Version}, com.android.build.gradle.internal.attributes.VariantAttr>debug, org.gradle.libraryelements>jar, org.gradle.usage>java-api|project:app:unspecified
+      > :|:app|debug|com.android.build.api.attributes.AgpVersionAttr>{AGP_Version}, com.android.build.gradle.internal.attributes.VariantAttr>debug, org.gradle.usage>java-runtime|project:app:unspecified:
+         - type               = PROJECT
+         - artifact           = (null)
+         > projectInfo:
+            - buildId        = ":"
+            - projectPath    = ":app"
+            - isTestFixtures = false
+            - buildType      = "debug"
+            - productFlavors = []
+            - attributes:
+               * "com.android.build.api.attributes.AgpVersionAttr -> {AGP_Version}"
+               * "com.android.build.gradle.internal.attributes.VariantAttr -> debug"
+               * "org.gradle.usage -> java-runtime"
+            - capabilities:
+               * "project:app:unspecified"
+         < projectInfo
+         - libraryInfo        = (null)
+         - lintJar            = (null)
+         - androidLibraryData = (null)
+      < :|:app|debug|com.android.build.api.attributes.AgpVersionAttr>{AGP_Version}, com.android.build.gradle.internal.attributes.VariantAttr>debug, org.gradle.usage>java-runtime|project:app:unspecified
+      > com.foo|bar1|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-api|com.foo:bar1:1.0:
+         - type               = JAVA_LIBRARY
+         - artifact           = {PROJECT}/repo/com/foo/bar1/1.0/bar1-1.0.jar{F}
+         - projectInfo        = (null)
+         > libraryInfo:
+            - group          = "com.foo"
+            - name           = "bar1"
+            - version        = "1.0"
+            - isTestFixtures = false
+            - buildType      = (null)
+            - productFlavors = []
+            - attributes:
+               * "org.gradle.category -> library"
+               * "org.gradle.libraryelements -> jar"
+               * "org.gradle.status -> release"
+               * "org.gradle.usage -> java-api"
+            - capabilities:
+               * "com.foo:bar1:1.0"
+         < libraryInfo
+         - lintJar            = (null)
+         - androidLibraryData = (null)
+      < com.foo|bar1|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-api|com.foo:bar1:1.0
+      > com.foo|bar1|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-runtime|com.foo:bar1:1.0:
+         - type               = JAVA_LIBRARY
+         - artifact           = {PROJECT}/repo/com/foo/bar1/1.0/bar1-1.0.jar{F}
+         - projectInfo        = (null)
+         > libraryInfo:
+            - group          = "com.foo"
+            - name           = "bar1"
+            - version        = "1.0"
+            - isTestFixtures = false
+            - buildType      = (null)
+            - productFlavors = []
+            - attributes:
+               * "org.gradle.category -> library"
+               * "org.gradle.libraryelements -> jar"
+               * "org.gradle.status -> release"
+               * "org.gradle.usage -> java-runtime"
+            - capabilities:
+               * "com.foo:bar1:1.0"
+         < libraryInfo
+         - lintJar            = (null)
+         - androidLibraryData = (null)
+      < com.foo|bar1|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-runtime|com.foo:bar1:1.0
+      > com.foo|bar2|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-api|com.foo:bar2:1.0:
+         - type               = JAVA_LIBRARY
+         - artifact           = {PROJECT}/repo/com/foo/bar2/1.0/bar2-1.0.jar{F}
+         - projectInfo        = (null)
+         > libraryInfo:
+            - group          = "com.foo"
+            - name           = "bar2"
+            - version        = "1.0"
+            - isTestFixtures = false
+            - buildType      = (null)
+            - productFlavors = []
+            - attributes:
+               * "org.gradle.category -> library"
+               * "org.gradle.libraryelements -> jar"
+               * "org.gradle.status -> release"
+               * "org.gradle.usage -> java-api"
+            - capabilities:
+               * "com.foo:bar2:1.0"
+         < libraryInfo
+         - lintJar            = (null)
+         - androidLibraryData = (null)
+      < com.foo|bar2|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-api|com.foo:bar2:1.0
+      > com.foo|bar2|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-runtime|com.foo:bar2:1.0:
+         - type               = JAVA_LIBRARY
+         - artifact           = {PROJECT}/repo/com/foo/bar2/1.0/bar2-1.0.jar{F}
+         - projectInfo        = (null)
+         > libraryInfo:
+            - group          = "com.foo"
+            - name           = "bar2"
+            - version        = "1.0"
+            - isTestFixtures = false
+            - buildType      = (null)
+            - productFlavors = []
+            - attributes:
+               * "org.gradle.category -> library"
+               * "org.gradle.libraryelements -> jar"
+               * "org.gradle.status -> release"
+               * "org.gradle.usage -> java-runtime"
+            - capabilities:
+               * "com.foo:bar2:1.0"
+         < libraryInfo
+         - lintJar            = (null)
+         - androidLibraryData = (null)
+      < com.foo|bar2|1.0|org.gradle.category>library, org.gradle.libraryelements>jar, org.gradle.status>release, org.gradle.usage>java-runtime|com.foo:bar2:1.0
+   < libraries
+< VariantDependencies
diff --git a/build-system/integration-test/framework/src/main/java/com/android/build/gradle/integration/common/fixture/testprojects/PluginType.kt b/build-system/integration-test/framework/src/main/java/com/android/build/gradle/integration/common/fixture/testprojects/PluginType.kt
index 57facfc..d9d4162 100644
--- a/build-system/integration-test/framework/src/main/java/com/android/build/gradle/integration/common/fixture/testprojects/PluginType.kt
+++ b/build-system/integration-test/framework/src/main/java/com/android/build/gradle/integration/common/fixture/testprojects/PluginType.kt
@@ -81,6 +81,9 @@
     JAVA_TEST_FIXTURES(
         id = "java-test-fixtures",
         useNewDsl = true
+    ),
+    MAVEN_PUBLISH(
+        id= "maven-publish",
     )
 }
 
diff --git a/common/version.bzl b/common/version.bzl
index bbe426e..0e8f031 100644
--- a/common/version.bzl
+++ b/common/version.bzl
@@ -1,2 +1,2 @@
-BASE_VERSION = "30.2.1"
-BUILD_VERSION = "7.2.1"
+BASE_VERSION = "30.2.0-dev"
+BUILD_VERSION = "7.2.0-dev"
diff --git a/common/version/com/android/version.properties b/common/version/com/android/version.properties
index 8709cc3..6612996 100644
--- a/common/version/com/android/version.properties
+++ b/common/version/com/android/version.properties
@@ -1,8 +1,8 @@
 # The source of truth for these values are in tools/buildSrc/base
 # Consistency is maintained via tests
 
-baseVersion = 30.2.1
-buildVersion = 7.2.1
-cmdlineToolsVersion = 7.0
+baseVersion = 30.2.0-dev
+buildVersion = 7.2.0-dev
+cmdlineToolsVersion = 3.0-dev
 apiVersion = 3
 nativeApiVersion = 0
diff --git a/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/ApiDetector.kt b/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/ApiDetector.kt
index 598a2f6..285c09c 100644
--- a/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/ApiDetector.kt
+++ b/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/ApiDetector.kt
@@ -157,6 +157,7 @@
 import org.jetbrains.uast.UTryExpression
 import org.jetbrains.uast.UTypeReferenceExpression
 import org.jetbrains.uast.UUnaryExpression
+import org.jetbrains.uast.UastBinaryExpressionWithTypeKind
 import org.jetbrains.uast.UastBinaryOperator
 import org.jetbrains.uast.UastCallKind
 import org.jetbrains.uast.expressions.UInjectionHost
@@ -188,6 +189,13 @@
  * requirement in the manifest).
  */
 class ApiDetector : ResourceXmlDetector(), SourceCodeScanner, ResourceFolderScanner {
+    init {
+      // Trigger <clinit> of UastBinaryExpressionWithTypeKind early
+      // to avoid a class initialization deadlock (b/232441126)
+      //noinspection NoOp
+      UastBinaryExpressionWithTypeKind.UNKNOWN
+    }
+
     private var apiDatabase: ApiLookup? = null
 
     override fun beforeCheckRootProject(context: Context) {