diff --git a/app/build.gradle.kts b/app/build.gradle.kts index ecf84f078..afa19e4ac 100755 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -219,21 +219,24 @@ tasks.create("generateInitScript") { doLast { out.bufferedWriter().use { + it.write( """ initscript { repositories { + + // Always specify the snapshots repository first maven { - mavenCentral() - google() - // Add snapshots repository for AndroidIDE CI builds - url "https://s01.oss.sonatype.org/content/repositories/snapshots/" + url "${VersionUtils.SNAPSHOTS_REPO}" } + + mavenCentral() + google() } dependencies { - classpath '${BuildConfig.packageName}:gradle-plugin:${downloadVersion}' + classpath '${BuildConfig.packageName}:gradle-plugin:$downloadVersion' } } diff --git a/build-info/build.gradle.kts b/build-info/build.gradle.kts index 64a43091f..142414045 100644 --- a/build-info/build.gradle.kts +++ b/build-info/build.gradle.kts @@ -54,7 +54,9 @@ tasks.create("generateBuildInfo") { "PROJECT_SITE" to ProjectConfig.PROJECT_SITE, "AGP_VERSION_MININUM" to AGP_VERSION_MINIMUM, - "AGP_VERSION_LATEST" to libs.versions.agp.tooling.get() + "AGP_VERSION_LATEST" to libs.versions.agp.tooling.get(), + + "SNAPSHOTS_REPOSITORY" to VersionUtils.SNAPSHOTS_REPO, ) ) } diff --git a/build-info/src/main/java/com/itsaky/androidide/buildinfo/BuildInfo.java.in b/build-info/src/main/java/com/itsaky/androidide/buildinfo/BuildInfo.java.in index cda396dfd..121ff0cd4 100644 --- a/build-info/src/main/java/com/itsaky/androidide/buildinfo/BuildInfo.java.in +++ b/build-info/src/main/java/com/itsaky/androidide/buildinfo/BuildInfo.java.in @@ -52,4 +52,5 @@ public class BuildInfo { // ------- Misc ------------------------ public static final String PROJECT_SITE = "@@PROJECT_SITE@@"; + public static final String SNAPSHOTS_REPOSITORY = "@@SNAPSHOTS_REPOSITORY@@"; } \ No newline at end of file diff --git a/build-logic/ide/src/main/java/ProjectConfig.kt b/build-logic/ide/src/main/java/ProjectConfig.kt index 41e0c1578..1bf23764d 100644 --- a/build-logic/ide/src/main/java/ProjectConfig.kt +++ b/build-logic/ide/src/main/java/ProjectConfig.kt @@ -101,6 +101,8 @@ val Project.downloadVersion: String return if (CI.isCiBuild) { publishingVersion } else { - "latest.integration" + // sometimes, when working locally, Gradle fails to download the latest snapshot version + // this may cause issues while initializing the project in AndroidIDE + VersionUtils.getLatestSnapshotVersion("gradle-plugin") } } diff --git a/build-logic/ide/src/main/java/VersionUtils.kt b/build-logic/ide/src/main/java/VersionUtils.kt new file mode 100644 index 000000000..936ac925b --- /dev/null +++ b/build-logic/ide/src/main/java/VersionUtils.kt @@ -0,0 +1,63 @@ +import org.gradle.api.GradleException +import java.io.BufferedInputStream +import java.net.URI +import javax.xml.parsers.DocumentBuilderFactory +import javax.xml.xpath.XPathFactory + +/* + * This file is part of AndroidIDE. + * + * AndroidIDE is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AndroidIDE is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with AndroidIDE. If not, see . + */ + +/** + * @author Akash Yadav + */ +object VersionUtils { + + /** + * The Sonatype snapshots repository. + */ + const val SNAPSHOTS_REPO = "https://s01.oss.sonatype.org/content/repositories/snapshots/" + + /** + * The latest integration version name. + */ + const val LATEST_INTEGRATION = "latest.integration" + + /** + * Gets the latest snapshot version of the given artifact from the Sonatype snapshots repository. + */ + @JvmStatic + fun getLatestSnapshotVersion(artifact: String): String { + val groupId = BuildConfig.packageName.replace('.', '/') + val moduleMetadata = "${SNAPSHOTS_REPO}/$groupId/${artifact}/maven-metadata.xml" + return try { + BufferedInputStream(URI.create(moduleMetadata).toURL().openStream()).use { inputStream -> + val builderFactory = DocumentBuilderFactory.newInstance() + val builder = builderFactory.newDocumentBuilder() + val document = builder.parse(inputStream) + + val xPathFactory = XPathFactory.newInstance() + val xPath = xPathFactory.newXPath() + + val latestVersion = xPath.evaluate("/metadata/versioning/latest", document) + println("Found latest version of artifact '$artifact' : '$latestVersion'") + return@use latestVersion + } + } catch (err: Throwable) { + throw GradleException("Failed to download $moduleMetadata", err) + } + } +} \ No newline at end of file diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEInitScriptPlugin.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEInitScriptPlugin.kt index 2e173acde..2c4c33f2e 100644 --- a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEInitScriptPlugin.kt +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEInitScriptPlugin.kt @@ -102,9 +102,7 @@ class AndroidIDEInitScriptPlugin : Plugin { // For AndroidIDE CI builds maven { repository -> - repository.url = URI.create( - "https://s01.oss.sonatype.org/content/repositories/snapshots/" - ) + repository.url = URI.create(BuildInfo.SNAPSHOTS_REPOSITORY) } } else { logger.info("Using local maven repository for classpath resolution...")