Add full offline Android Gradle Plugin dependency cache

This commit is contained in:
lendry
2026-06-26 20:55:41 +03:00
parent 8805ec327f
commit 07df6eacf1
266 changed files with 14499 additions and 26 deletions

View File

@@ -0,0 +1,106 @@
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<Triple<String, String, String>>()
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")
}