import org.gradle.api.GradleException plugins { java } repositories { google() mavenCentral() } val agpVersion: String = providers.gradleProperty("agpVersion").orElse("8.11.0").get() configurations { create("agp") } dependencies { add("agp", "com.android.tools.build:gradle:$agpVersion") } tasks.register("exportOfflineMaven") { dependsOn(configurations.named("agp")) doLast { val offlineRoot = file("${rootProject.projectDir}/../offline-maven").apply { mkdirs() } val configuration = configurations.getByName("agp") val resolved = configuration.resolvedConfiguration val modules = linkedSetOf>() resolved.resolvedArtifacts.forEach { artifact -> val id = artifact.moduleVersion.id modules.add(Triple(id.group, id.name, id.version)) installArtifact( offlineRoot, id.group, id.name, id.version, artifact.extension ?: "jar", artifact.file, ) } modules.forEach { (group, name, version) -> ensurePom(offlineRoot, group, name, version) } file("${offlineRoot.absolutePath}/.agp-offline-complete").writeText( "agpVersion=$agpVersion\nmodules=${modules.size}\n", ) println("Exported ${modules.size} modules to ${offlineRoot.absolutePath}") } } fun installArtifact( root: File, group: String, name: String, version: String, ext: String, src: File, ) { val dir = root.resolve("${group.replace('.', '/')}/$name/$version") dir.mkdirs() src.copyTo(dir.resolve("$name-$version.$ext"), overwrite = true) } fun ensurePom(root: File, group: String, name: String, version: String) { val dir = root.resolve("${group.replace('.', '/')}/$name/$version") dir.mkdirs() val pom = dir.resolve("$name-$version.pom") if (pom.exists() && pom.length() > 0L) { return } val groupPath = group.replace('.', '/') val bases = when { group.startsWith("com.android") || group.startsWith("androidx") || group.startsWith("com.google.android") -> listOf( "https://dl.google.com/dl/android/maven2", "https://dl.google.com/android/maven2", "https://maven.google.com", ) else -> listOf( "https://repo.maven.apache.org/maven2", "https://dl.google.com/dl/android/maven2", "https://maven.google.com", ) } for (base in bases) { val url = "$base/$groupPath/$name/$version/$name-$version.pom" try { exec { commandLine("curl", "-fsSL", url, "-o", pom.absolutePath) } if (pom.exists() && pom.length() > 0L) { return } } catch (_: Exception) { pom.delete() } } throw GradleException("POM not found: $group:$name:$version") }