Validating that the autodetected groupId matches the explicitly set groupId for non-published projects too

so that when we set the groupId automatically in a followup change, none of the groupIds will change.

This requires setting a groupId in non-published projects.
This also requires setting LibraryType and Publish in some cases so that we can detect that these projects aren't published

Bug: 146802533

Test: run `./development/validateRefactor.sh HEAD^` and see that there aren't any differences relating to groupIds or to files being added or removed (although there are some differences in docs, which is a known issue: b/189228842 )

Test: run `./gradlew tasks --all` with and without this change and see that the list of tasks is the same

Test: Treehugger runs busytown/*.sh

Change-Id: Ie4c5993872543745f6eada632a80de339bd08638
diff --git a/ads/ads-identifier-benchmark/build.gradle b/ads/ads-identifier-benchmark/build.gradle
index e07b659..66d7707 100644
--- a/ads/ads-identifier-benchmark/build.gradle
+++ b/ads/ads-identifier-benchmark/build.gradle
@@ -39,3 +39,7 @@
 android {
     namespace "androidx.ads.identifier.benchmark"
 }
+
+androidx {
+    mavenGroup = LibraryGroups.ADS
+}
diff --git a/ads/ads-identifier-testing/build.gradle b/ads/ads-identifier-testing/build.gradle
index efffcf2..7c747bc 100644
--- a/ads/ads-identifier-testing/build.gradle
+++ b/ads/ads-identifier-testing/build.gradle
@@ -36,4 +36,5 @@
 
 androidx {
     type = LibraryType.INTERNAL_TEST_LIBRARY
-}
\ No newline at end of file
+    mavenGroup = LibraryGroups.ADS
+}
diff --git a/annotation/annotation-experimental-lint/integration-tests/build.gradle b/annotation/annotation-experimental-lint/integration-tests/build.gradle
index 308e7cd..9922f11 100644
--- a/annotation/annotation-experimental-lint/integration-tests/build.gradle
+++ b/annotation/annotation-experimental-lint/integration-tests/build.gradle
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+import androidx.build.LibraryType
 import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
 
 def isIdeBuild() {
@@ -44,3 +45,8 @@
         }
     }
 }
+
+androidx {
+    type = LibraryType.INTERNAL_TEST_LIBRARY
+    mavenGroup = LibraryGroups.ANNOTATION
+}
diff --git a/annotation/annotation-sampled/build.gradle b/annotation/annotation-sampled/build.gradle
index d450e7e..267caf7 100644
--- a/annotation/annotation-sampled/build.gradle
+++ b/annotation/annotation-sampled/build.gradle
@@ -22,3 +22,7 @@
 dependencies {
     implementation(libs.kotlinStdlib)
 }
+
+androidx {
+    mavenGroup = LibraryGroups.ANNOTATION
+}
diff --git a/appcompat/appcompat-benchmark/build.gradle b/appcompat/appcompat-benchmark/build.gradle
index 8eb2d87..b9310e0 100644
--- a/appcompat/appcompat-benchmark/build.gradle
+++ b/appcompat/appcompat-benchmark/build.gradle
@@ -34,3 +34,7 @@
 android {
     namespace "androidx.appcompat.benchmark"
 }
+
+androidx {
+    mavenGroup = LibraryGroups.APPCOMPAT
+}
diff --git a/benchmark/benchmark/build.gradle b/benchmark/benchmark/build.gradle
index eb6c7f2..3676f98 100644
--- a/benchmark/benchmark/build.gradle
+++ b/benchmark/benchmark/build.gradle
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+import androidx.build.LibraryType
 import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
 
 plugins {
@@ -38,3 +39,8 @@
 android {
     namespace "androidx.benchmark.benchmark"
 }
+
+androidx {
+    type = LibraryType.INTERNAL_TEST_LIBRARY
+    mavenGroup = LibraryGroups.BENCHMARK
+}
diff --git a/buildSrc-tests/project-subsets/build.gradle b/buildSrc-tests/project-subsets/build.gradle
index 3199270..65ba658 100644
--- a/buildSrc-tests/project-subsets/build.gradle
+++ b/buildSrc-tests/project-subsets/build.gradle
@@ -30,3 +30,7 @@
     // run this task and leave details around incrementality up to the Gradle builds that we spawn
     t.outputs.upToDateWhen { false }
 }
+
+androidx {
+    mavenGroup = LibraryGroups.BUILDSRC_TESTS
+}
diff --git a/buildSrc/private/src/main/kotlin/androidx/build/AndroidXExtension.kt b/buildSrc/private/src/main/kotlin/androidx/build/AndroidXExtension.kt
index 1673982..a40314a 100644
--- a/buildSrc/private/src/main/kotlin/androidx/build/AndroidXExtension.kt
+++ b/buildSrc/private/src/main/kotlin/androidx/build/AndroidXExtension.kt
@@ -39,9 +39,6 @@
 
     var mavenGroup: LibraryGroup? = null
         set(value) {
-            val expectedValue = getNewLibraryGroup()
-            validateLibraryGroup(value, expectedValue)
-
             field = value
             chooseProjectVersion()
         }
@@ -67,6 +64,9 @@
         LibraryVersions = service.libraryVersions
         libraryGroupsByGroupId = service.libraryGroupsByGroupId
         overrideLibraryGroupsByProjectPath = service.overrideLibraryGroupsByProjectPath
+        project.afterEvaluate {
+            validateLibraryGroup()
+        }
     }
 
     var name: Property<String?> = project.objects.property(String::class.java)
@@ -78,10 +78,13 @@
         }
 
     // a temporary method while we migrate from LibraryGroups.${key} to libraryGroup.get()
-    private fun validateLibraryGroup(oldGroup: LibraryGroup?, newGroup: LibraryGroup?) {
-        check(newGroup == oldGroup) {
-            "Error in $project: oldGroup = ${oldGroup?.group} but newGroup = ${newGroup?.group}"
-        }
+    private fun validateLibraryGroup() {
+            val oldGroup = mavenGroup
+            val newGroup = getNewLibraryGroup()
+            check(newGroup == oldGroup) {
+                "Error in ${project.projectDir}/build.gradle: oldGroup = ${oldGroup?.group} " +
+                    "but newGroup = ${newGroup?.group}"
+            }
     }
 
     private fun getNewLibraryGroup(): LibraryGroup? {
diff --git a/buildSrc/private/src/main/kotlin/androidx/build/AndroidXImplPlugin.kt b/buildSrc/private/src/main/kotlin/androidx/build/AndroidXImplPlugin.kt
index 2de486e..378080f 100644
--- a/buildSrc/private/src/main/kotlin/androidx/build/AndroidXImplPlugin.kt
+++ b/buildSrc/private/src/main/kotlin/androidx/build/AndroidXImplPlugin.kt
@@ -537,7 +537,7 @@
             val mavenGroup = extension.mavenGroup
             val isProbablyPublished = extension.type == LibraryType.PUBLISHED_LIBRARY ||
                 extension.type == LibraryType.UNSET
-            if (mavenGroup != null && isProbablyPublished) {
+            if (mavenGroup != null && isProbablyPublished && extension.shouldPublish()) {
                 validateProjectStructure(mavenGroup.group)
             }
         }
diff --git a/car/app/app-samples/navigation/automotive/build.gradle b/car/app/app-samples/navigation/automotive/build.gradle
index 84a1c7e..69414f8 100644
--- a/car/app/app-samples/navigation/automotive/build.gradle
+++ b/car/app/app-samples/navigation/automotive/build.gradle
@@ -14,6 +14,9 @@
  * limitations under the License.
  */
 
+import androidx.build.LibraryType
+import androidx.build.Publish
+
 plugins {
     id("AndroidXPlugin")
     id("com.android.application")
@@ -42,3 +45,9 @@
     implementation(project(":car:app:app-samples:navigation-common"))
     implementation(project(":car:app:app-automotive"))
 }
+
+androidx {
+    type = LibraryType.SAMPLES
+    publish = Publish.NONE
+    mavenGroup = LibraryGroups.CAR_APP
+}
diff --git a/car/app/app-samples/navigation/common/build.gradle b/car/app/app-samples/navigation/common/build.gradle
index b29df05..24be554 100644
--- a/car/app/app-samples/navigation/common/build.gradle
+++ b/car/app/app-samples/navigation/common/build.gradle
@@ -14,6 +14,9 @@
  * limitations under the License.
  */
 
+import androidx.build.LibraryType
+import androidx.build.Publish
+
 plugins {
     id("AndroidXPlugin")
     id("com.android.library")
@@ -37,3 +40,9 @@
     implementation 'androidx.lifecycle:lifecycle-livedata:2.3.1'
     implementation 'androidx.activity:activity:1.2.3'
 }
+
+androidx {
+    type = LibraryType.SAMPLES
+    publish = Publish.NONE
+    mavenGroup = LibraryGroups.CAR_APP
+}
diff --git a/car/app/app-samples/navigation/mobile/build.gradle b/car/app/app-samples/navigation/mobile/build.gradle
index a33b0f9..e7b4e50 100644
--- a/car/app/app-samples/navigation/mobile/build.gradle
+++ b/car/app/app-samples/navigation/mobile/build.gradle
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+import androidx.build.LibraryType
+
 plugins {
     id("AndroidXPlugin")
     id("com.android.application")
@@ -42,3 +44,7 @@
     implementation(project(":car:app:app-samples:navigation-common"))
     implementation(project(":car:app:app-projected"))
 }
+
+androidx {
+    mavenGroup = LibraryGroups.CAR_APP
+}
diff --git a/car/app/app-samples/showcase/automotive/build.gradle b/car/app/app-samples/showcase/automotive/build.gradle
index db7f93c..9310044 100644
--- a/car/app/app-samples/showcase/automotive/build.gradle
+++ b/car/app/app-samples/showcase/automotive/build.gradle
@@ -48,3 +48,7 @@
     implementation(project(":car:app:app-samples:showcase-common"))
     implementation(project(":car:app:app-automotive"))
 }
+
+androidx {
+    mavenGroup = LibraryGroups.CAR_APP
+}
diff --git a/car/app/app-samples/showcase/common/build.gradle b/car/app/app-samples/showcase/common/build.gradle
index ef1f13d..7a9988f 100644
--- a/car/app/app-samples/showcase/common/build.gradle
+++ b/car/app/app-samples/showcase/common/build.gradle
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+import androidx.build.LibraryType
+
 plugins {
     id("AndroidXPlugin")
     id("com.android.library")
@@ -37,3 +39,7 @@
     compileOnly libs.kotlinStdlib
 }
 
+
+androidx {
+    mavenGroup = LibraryGroups.CAR_APP
+}
diff --git a/car/app/app-samples/showcase/mobile/build.gradle b/car/app/app-samples/showcase/mobile/build.gradle
index 061a506..b3ab257 100644
--- a/car/app/app-samples/showcase/mobile/build.gradle
+++ b/car/app/app-samples/showcase/mobile/build.gradle
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+import androidx.build.LibraryType
+
 plugins {
     id("AndroidXPlugin")
     id("com.android.application")
@@ -48,3 +50,7 @@
     implementation(project(":car:app:app-samples:showcase-common"))
     implementation(project(":car:app:app-projected"))
 }
+
+androidx {
+    mavenGroup = LibraryGroups.CAR_APP
+}
diff --git a/collection/collection-benchmark-kmp/build.gradle b/collection/collection-benchmark-kmp/build.gradle
index 22aa497..158941a 100644
--- a/collection/collection-benchmark-kmp/build.gradle
+++ b/collection/collection-benchmark-kmp/build.gradle
@@ -69,3 +69,7 @@
 androidx {
     publish = Publish.NONE
 }
+
+androidx {
+    mavenGroup = LibraryGroups.COLLECTION
+}
diff --git a/compose/desktop/desktop/samples/build.gradle b/compose/desktop/desktop/samples/build.gradle
index 91089c4..760fdc3 100644
--- a/compose/desktop/desktop/samples/build.gradle
+++ b/compose/desktop/desktop/samples/build.gradle
@@ -104,4 +104,8 @@
     dependsOn("run1")
 }
 
+androidx {
+    mavenGroup = LibraryGroups.COMPOSE_DESKTOP
+}
+
 BuildOnServerKt.addToBuildOnServer(project, "${project.path}:jvmJar")
diff --git a/compose/foundation/foundation/benchmark/build.gradle b/compose/foundation/foundation/benchmark/build.gradle
index 7da93b7..539cd8d 100644
--- a/compose/foundation/foundation/benchmark/build.gradle
+++ b/compose/foundation/foundation/benchmark/build.gradle
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+import androidx.build.LibraryType
+
 plugins {
     id("AndroidXPlugin")
     id("com.android.library")
@@ -42,3 +44,8 @@
 android {
     namespace "androidx.compose.foundation.benchmark"
 }
+
+androidx {
+    type = LibraryType.INTERNAL_TEST_LIBRARY
+    mavenGroup = LibraryGroups.COMPOSE_FOUNDATION
+}
diff --git a/compose/material/material-icons-extended-filled/build.gradle b/compose/material/material-icons-extended-filled/build.gradle
index 8a29bef..b03fc9f 100644
--- a/compose/material/material-icons-extended-filled/build.gradle
+++ b/compose/material/material-icons-extended-filled/build.gradle
@@ -18,4 +18,7 @@
 
 android {
     namespace "androidx.compose.material.icons.extended"
-}
\ No newline at end of file
+}
+androidx {
+    mavenGroup = LibraryGroups.COMPOSE_MATERIAL
+}
diff --git a/compose/material/material-icons-extended-outlined/build.gradle b/compose/material/material-icons-extended-outlined/build.gradle
index 8a29bef..b03fc9f 100644
--- a/compose/material/material-icons-extended-outlined/build.gradle
+++ b/compose/material/material-icons-extended-outlined/build.gradle
@@ -18,4 +18,7 @@
 
 android {
     namespace "androidx.compose.material.icons.extended"
-}
\ No newline at end of file
+}
+androidx {
+    mavenGroup = LibraryGroups.COMPOSE_MATERIAL
+}
diff --git a/compose/material/material-icons-extended-rounded/build.gradle b/compose/material/material-icons-extended-rounded/build.gradle
index 8a29bef..b03fc9f 100644
--- a/compose/material/material-icons-extended-rounded/build.gradle
+++ b/compose/material/material-icons-extended-rounded/build.gradle
@@ -18,4 +18,7 @@
 
 android {
     namespace "androidx.compose.material.icons.extended"
-}
\ No newline at end of file
+}
+androidx {
+    mavenGroup = LibraryGroups.COMPOSE_MATERIAL
+}
diff --git a/compose/material/material-icons-extended-sharp/build.gradle b/compose/material/material-icons-extended-sharp/build.gradle
index 8a29bef..b03fc9f 100644
--- a/compose/material/material-icons-extended-sharp/build.gradle
+++ b/compose/material/material-icons-extended-sharp/build.gradle
@@ -18,4 +18,7 @@
 
 android {
     namespace "androidx.compose.material.icons.extended"
-}
\ No newline at end of file
+}
+androidx {
+    mavenGroup = LibraryGroups.COMPOSE_MATERIAL
+}
diff --git a/compose/material/material-icons-extended-twotone/build.gradle b/compose/material/material-icons-extended-twotone/build.gradle
index 8a29bef..b03fc9f 100644
--- a/compose/material/material-icons-extended-twotone/build.gradle
+++ b/compose/material/material-icons-extended-twotone/build.gradle
@@ -18,4 +18,7 @@
 
 android {
     namespace "androidx.compose.material.icons.extended"
-}
\ No newline at end of file
+}
+androidx {
+    mavenGroup = LibraryGroups.COMPOSE_MATERIAL
+}
diff --git a/compose/material/material/benchmark/build.gradle b/compose/material/material/benchmark/build.gradle
index 4ffcb6a..072cb27 100644
--- a/compose/material/material/benchmark/build.gradle
+++ b/compose/material/material/benchmark/build.gradle
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+import androidx.build.LibraryType
+
 plugins {
     id("AndroidXPlugin")
     id("com.android.library")
@@ -42,3 +44,8 @@
 android {
     namespace "androidx.compose.material.benchmark"
 }
+
+androidx {
+    type = LibraryType.INTERNAL_TEST_LIBRARY
+    mavenGroup = LibraryGroups.COMPOSE_MATERIAL
+}
diff --git a/compose/ui/ui/benchmark/build.gradle b/compose/ui/ui/benchmark/build.gradle
index 5cc4f13..4478fa7 100644
--- a/compose/ui/ui/benchmark/build.gradle
+++ b/compose/ui/ui/benchmark/build.gradle
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+import androidx.build.LibraryType
+
 plugins {
     id("AndroidXPlugin")
     id("com.android.library")
@@ -42,3 +44,8 @@
 android {
     namespace "androidx.compose.ui.benchmark"
 }
+
+androidx {
+    type = LibraryType.INTERNAL_TEST_LIBRARY
+    mavenGroup = LibraryGroups.COMPOSE_UI
+}
diff --git a/datastore/datastore-compose-samples/build.gradle b/datastore/datastore-compose-samples/build.gradle
index 81ec2ec..48d74b4 100644
--- a/datastore/datastore-compose-samples/build.gradle
+++ b/datastore/datastore-compose-samples/build.gradle
@@ -70,3 +70,7 @@
         }
     }
 }
+
+androidx {
+    mavenGroup = LibraryGroups.DATASTORE
+}
diff --git a/datastore/datastore-sampleapp/build.gradle b/datastore/datastore-sampleapp/build.gradle
index 68d23fa..d6b355b 100644
--- a/datastore/datastore-sampleapp/build.gradle
+++ b/datastore/datastore-sampleapp/build.gradle
@@ -72,3 +72,7 @@
         multiDexEnabled true
     }
 }
+
+androidx {
+    mavenGroup = LibraryGroups.DATASTORE
+}
diff --git a/metrics/metrics-benchmark/build.gradle b/metrics/metrics-benchmark/build.gradle
index 83a3253..82b966b 100644
--- a/metrics/metrics-benchmark/build.gradle
+++ b/metrics/metrics-benchmark/build.gradle
@@ -42,3 +42,7 @@
                 "UNLOCKED,EMULATOR,LOW-BATTERY,ENG-BUILD"
     }
 }
+
+androidx {
+    mavenGroup = LibraryGroups.METRICS
+}
diff --git a/recyclerview/recyclerview-benchmark/build.gradle b/recyclerview/recyclerview-benchmark/build.gradle
index 092475c..8083a5c7 100644
--- a/recyclerview/recyclerview-benchmark/build.gradle
+++ b/recyclerview/recyclerview-benchmark/build.gradle
@@ -36,3 +36,7 @@
 android {
     namespace "androidx.recyclerview.benchmark"
 }
+
+androidx {
+    mavenGroup = LibraryGroups.RECYCLERVIEW
+}
diff --git a/room/benchmark/build.gradle b/room/benchmark/build.gradle
index ffbc949..5a6ddbe 100644
--- a/room/benchmark/build.gradle
+++ b/room/benchmark/build.gradle
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+import androidx.build.LibraryType
+
 plugins {
     id("AndroidXPlugin")
     id("com.android.library")
@@ -43,3 +45,8 @@
 android {
     namespace "androidx.room.benchmark"
 }
+
+androidx {
+    type = LibraryType.INTERNAL_TEST_LIBRARY
+    mavenGroup = LibraryGroups.ROOM
+}
diff --git a/startup/startup-runtime-lint/build.gradle b/startup/startup-runtime-lint/build.gradle
index 100820f..28c3370 100644
--- a/startup/startup-runtime-lint/build.gradle
+++ b/startup/startup-runtime-lint/build.gradle
@@ -34,7 +34,10 @@
 androidx {
     name = "Android App Startup Runtime Lint Checks"
     type = LibraryType.LINT
-    mavenVersion = LibraryVersions.STARTUP
     inceptionYear = "2020"
     description = "Android App Startup Runtime"
 }
+
+androidx {
+    mavenGroup = LibraryGroups.STARTUP
+}
diff --git a/wear/compose/compose-material/benchmark/build.gradle b/wear/compose/compose-material/benchmark/build.gradle
index 3a552ea..8fd202a 100644
--- a/wear/compose/compose-material/benchmark/build.gradle
+++ b/wear/compose/compose-material/benchmark/build.gradle
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+import androidx.build.LibraryType
+
 plugins {
     id("AndroidXPlugin")
     id("com.android.library")
@@ -49,4 +51,8 @@
     androidTestImplementation(libs.kotlinReflect)
     androidTestImplementation(libs.kotlinTestCommon)
     androidTestImplementation(libs.truth)
-}
\ No newline at end of file
+}
+androidx {
+    type = LibraryType.INTERNAL_TEST_LIBRARY
+    mavenGroup = LibraryGroups.WEAR_COMPOSE
+}
diff --git a/wear/compose/compose-material3/benchmark/build.gradle b/wear/compose/compose-material3/benchmark/build.gradle
index 9bd32e84..20201b8 100644
--- a/wear/compose/compose-material3/benchmark/build.gradle
+++ b/wear/compose/compose-material3/benchmark/build.gradle
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+import androidx.build.LibraryType
+
 plugins {
     id("AndroidXPlugin")
     id("com.android.library")
@@ -48,4 +50,8 @@
     androidTestImplementation(libs.kotlinReflect)
     androidTestImplementation(libs.kotlinTestCommon)
     androidTestImplementation(libs.truth)
-}
\ No newline at end of file
+}
+androidx {
+    type = LibraryType.INTERNAL_TEST_LIBRARY
+    mavenGroup = LibraryGroups.WEAR_COMPOSE
+}
diff --git a/wear/watchface/watchface-complications-data-source-samples/build.gradle b/wear/watchface/watchface-complications-data-source-samples/build.gradle
index 64181e2..1cdd9b1 100644
--- a/wear/watchface/watchface-complications-data-source-samples/build.gradle
+++ b/wear/watchface/watchface-complications-data-source-samples/build.gradle
@@ -33,3 +33,7 @@
     }
     namespace "androidx.wear.watchface.complications.datasource.samples"
 }
+
+androidx {
+    mavenGroup = LibraryGroups.WEAR_WATCHFACE
+}