Add Sonatype Central repository for users of nightly builds only (#55305)

Summary:
Even though Sonatype Central repository isn't used by an app, still the React Native Gradle plugin adds the Sonatype Central to repositories.
In some cases it leads to a considerable amount of failed requests on Gradle downloading dependencies.

<img width="1076" height="109" alt="858 failed requests out of 858 requests" src="https://github.com/user-attachments/assets/40d6aeb6-dcc4-4ebb-9f0b-742d26ba6e65" />

I'm introducing a change where Sonatype Central repository is included for users of nightly builds only.

## Changelog:

[ANDROID] [CHANGED] - Add Sonatype Central repository for users of nightly builds only

Pull Request resolved: https://github.com/facebook/react-native/pull/55305

Test Plan:
All of the tests in gradle plugin are green
```
$ ./gradlew test

BUILD SUCCESSFUL in 10s
20 actionable tasks: 20 executed
```

Reviewed By: cipolleschi

Differential Revision: D91887976

Pulled By: cortinico

fbshipit-source-id: 1e61d3b8d97a444e17a02bfe9ed0334e9b0ab0bc
This commit is contained in:
Sergei Glotov authored and meta-codesync[bot] committed 2026-02-03 09:11:00 -08:00
1 parent 51c965883f
commit 47c3ebe23f
3 files changed
+114 -43

No files matched your search

@@ -74,10 +74,10 @@ class ReactPlugin : Plugin<Project> {
val hermesVersionPropertiesFile =
File(reactNativeDir, "sdks/hermes-engine/version.properties")
val versionAndGroupStrings =
readVersionAndGroupStrings(propertiesFile, hermesVersionPropertiesFile)
readVersionAndGroupStrings(project, propertiesFile, hermesVersionPropertiesFile)
val hermesV1Enabled = rootExtension.hermesV1Enabled.get()
configureDependencies(project, versionAndGroupStrings, hermesV1Enabled)
configureRepositories(project)
configureRepositories(project, versionAndGroupStrings.isNightly)
}
configureReactNativeNdk(project, extension)
@@ -35,13 +35,17 @@ internal object DependencyUtils {
val hermesV1VersionString: String,
val reactGroupString: String = DEFAULT_INTERNAL_REACT_PUBLISHING_GROUP,
val hermesGroupString: String = DEFAULT_INTERNAL_HERMES_PUBLISHING_GROUP,
)
private val isHermesNightly: Boolean = false,
) {
val isNightly: Boolean
get() = versionString.isNightly() || isHermesNightly
}
/**
* This method takes care of configuring the repositories{} block for both the app and all the 3rd
* party libraries which are auto-linked.
*/
fun configureRepositories(project: Project) {
fun configureRepositories(project: Project, isNightly: Boolean) {
val exclusiveEnterpriseRepository = project.rootProject.exclusiveEnterpriseRepository()
if (exclusiveEnterpriseRepository != null) {
project.logger.lifecycle(
@@ -67,9 +71,11 @@ internal object DependencyUtils {
return@allprojects
}
// We add the snapshot for users on nightlies.
mavenRepoFromUrl("https://central.sonatype.com/repository/maven-snapshots/") { repo ->
repo.content { it.excludeGroup("org.webkit") }
if (isNightly) {
// We add the snapshot for users on nightlies.
mavenRepoFromUrl("https://central.sonatype.com/repository/maven-snapshots/") { repo ->
repo.content { it.excludeGroup("org.webkit") }
}
}
repositories.mavenCentral { repo ->
// We don't want to fetch JSC from Maven Central as there are older versions there.
@@ -204,13 +210,17 @@ internal object DependencyUtils {
return dependencySubstitution
}
fun readVersionAndGroupStrings(propertiesFile: File, hermesVersionFile: File): Coordinates {
fun readVersionAndGroupStrings(
project: Project,
propertiesFile: File,
hermesVersionFile: File,
): Coordinates {
val reactAndroidProperties = Properties()
propertiesFile.inputStream().use { reactAndroidProperties.load(it) }
val versionStringFromFile = (reactAndroidProperties[INTERNAL_VERSION_NAME] as? String).orEmpty()
// If on a nightly, we need to fetch the -SNAPSHOT artifact from Sonatype.
val versionString =
if (versionStringFromFile.startsWith("0.0.0") || "-nightly-" in versionStringFromFile) {
if (versionStringFromFile.isNightly()) {
"$versionStringFromFile-SNAPSHOT"
} else {
versionStringFromFile
@@ -237,6 +247,7 @@ internal object DependencyUtils {
val hermesV1Version =
(hermesVersionProperties[INTERNAL_HERMES_V1_VERSION_NAME] as? String).orEmpty()
val isHermesNightly = (project.findProperty(INTERNAL_USE_HERMES_NIGHTLY) as? String).toBoolean()
return Coordinates(
versionString,
@@ -244,6 +255,7 @@ internal object DependencyUtils {
hermesV1Version,
reactGroupString,
hermesGroupString,
isHermesNightly,
)
}
@@ -274,6 +286,8 @@ internal object DependencyUtils {
else -> INCLUDE_JITPACK_REPOSITORY_DEFAULT
}
internal fun String.isNightly(): Boolean = this.startsWith("0.0.0") || "-nightly-" in this
internal fun Project.exclusiveEnterpriseRepository() =
when {
hasProperty(SCOPED_EXCLUSIVE_ENTEPRISE_REPOSITORY) ->
@@ -12,6 +12,7 @@ import com.facebook.react.utils.DependencyUtils.configureDependencies
import com.facebook.react.utils.DependencyUtils.configureRepositories
import com.facebook.react.utils.DependencyUtils.exclusiveEnterpriseRepository
import com.facebook.react.utils.DependencyUtils.getDependencySubstitutions
import com.facebook.react.utils.DependencyUtils.isNightly
import com.facebook.react.utils.DependencyUtils.mavenRepoFromURI
import com.facebook.react.utils.DependencyUtils.mavenRepoFromUrl
import com.facebook.react.utils.DependencyUtils.readVersionAndGroupStrings
@@ -35,7 +36,7 @@ class DependencyUtilsTest {
val project = createProject()
project.extensions.extraProperties.set("react.internal.mavenLocalRepo", localMaven.absolutePath)
configureRepositories(project)
configureRepositories(project, false)
assertThat(
project.repositories.firstOrNull {
@@ -45,27 +46,12 @@ class DependencyUtilsTest {
.isNotNull()
}
@Test
fun configureRepositories_containsSnapshotRepo() {
val repositoryURI = URI.create("https://central.sonatype.com/repository/maven-snapshots/")
val project = createProject()
configureRepositories(project)
assertThat(
project.repositories.firstOrNull {
it is MavenArtifactRepository && it.url == repositoryURI
}
)
.isNotNull()
}
@Test
fun configureRepositories_containsMavenCentral() {
val repositoryURI = URI.create("https://repo.maven.apache.org/maven2/")
val project = createProject()
configureRepositories(project)
configureRepositories(project, false)
assertThat(
project.repositories.firstOrNull {
@@ -80,7 +66,7 @@ class DependencyUtilsTest {
val repositoryURI = URI.create("https://dl.google.com/dl/android/maven2/")
val project = createProject()
configureRepositories(project)
configureRepositories(project, false)
assertThat(
project.repositories.firstOrNull {
@@ -95,7 +81,7 @@ class DependencyUtilsTest {
val repositoryURI = URI.create("https://www.jitpack.io")
val project = createProject()
configureRepositories(project)
configureRepositories(project, false)
assertThat(
project.repositories.firstOrNull {
@@ -115,7 +101,7 @@ class DependencyUtilsTest {
repositoryURI.toString(),
)
configureRepositories(project)
configureRepositories(project, false)
assertThat(project.repositories).hasSize(1)
assertThat(
@@ -132,7 +118,7 @@ class DependencyUtilsTest {
var project = createProject()
project.extensions.extraProperties.set("includeJitpackRepository", "false")
configureRepositories(project)
configureRepositories(project, false)
assertThat(
project.repositories.firstOrNull {
@@ -145,7 +131,7 @@ class DependencyUtilsTest {
project = createProject()
project.extensions.extraProperties.set("react.includeJitpackRepository", "false")
configureRepositories(project)
configureRepositories(project, false)
assertThat(
project.repositories.firstOrNull {
@@ -161,7 +147,7 @@ class DependencyUtilsTest {
var project = createProject()
project.extensions.extraProperties.set("includeJitpackRepository", "true")
configureRepositories(project)
configureRepositories(project, false)
assertThat(
project.repositories.firstOrNull {
@@ -174,7 +160,37 @@ class DependencyUtilsTest {
project = createProject()
project.extensions.extraProperties.set("react.includeJitpackRepository", "true")
configureRepositories(project)
configureRepositories(project, false)
assertThat(
project.repositories.firstOrNull {
it is MavenArtifactRepository && it.url == repositoryURI
}
)
.isNotNull()
}
@Test
fun configureRepositories_notNightly_doesNotContainSonatype() {
val repositoryURI = URI.create("https://central.sonatype.com/repository/maven-snapshots/")
var project = createProject()
configureRepositories(project, false)
assertThat(
project.repositories.firstOrNull {
it is MavenArtifactRepository && it.url == repositoryURI
}
)
.isNull()
}
@Test
fun configureRepositories_nightly_containSonatype() {
val repositoryURI = URI.create("https://central.sonatype.com/repository/maven-snapshots/")
var project = createProject()
configureRepositories(project, true)
assertThat(
project.repositories.firstOrNull {
@@ -192,7 +208,7 @@ class DependencyUtilsTest {
val project = createProject()
project.extensions.extraProperties.set("react.internal.mavenLocalRepo", localMaven.absolutePath)
configureRepositories(project)
configureRepositories(project, false)
val indexOfLocalRepo =
project.repositories.indexOfFirst {
@@ -211,7 +227,7 @@ class DependencyUtilsTest {
val mavenCentralURI = URI.create("https://repo.maven.apache.org/maven2/")
val project = createProject()
configureRepositories(project)
configureRepositories(project, false)
val indexOfSnapshotRepo =
project.repositories.indexOfFirst {
@@ -231,7 +247,7 @@ class DependencyUtilsTest {
val appProject = ProjectBuilder.builder().withName("app").withParent(rootProject).build()
val libProject = ProjectBuilder.builder().withName("lib").withParent(rootProject).build()
configureRepositories(appProject)
configureRepositories(appProject, false)
assertThat(
appProject.repositories.firstOrNull {
@@ -259,7 +275,7 @@ class DependencyUtilsTest {
repo.content { content -> content.excludeGroup("com.facebook.react") }
}
configureRepositories(appProject)
configureRepositories(appProject, false)
// We need to make sure we have Maven Central defined twice, one by the library,
// and another is the override by RNGP.
@@ -598,7 +614,8 @@ class DependencyUtilsTest {
)
}
val strings = readVersionAndGroupStrings(propertiesFile, hermesVersionFile)
val project = createProject()
val strings = readVersionAndGroupStrings(project, propertiesFile, hermesVersionFile)
val versionString = strings.versionString
val hermesVersionString = strings.hermesVersionString
val hermesV1VersionString = strings.hermesV1VersionString
@@ -635,7 +652,8 @@ class DependencyUtilsTest {
)
}
val strings = readVersionAndGroupStrings(propertiesFile, hermesVersionFile)
val project = createProject()
val strings = readVersionAndGroupStrings(project, propertiesFile, hermesVersionFile)
val versionString = strings.versionString
val hermesVersionString = strings.hermesVersionString
val hermesV1VersionString = strings.hermesV1VersionString
@@ -667,7 +685,8 @@ class DependencyUtilsTest {
)
}
val strings = readVersionAndGroupStrings(propertiesFile, hermesVersionFile)
val project = createProject()
val strings = readVersionAndGroupStrings(project, propertiesFile, hermesVersionFile)
val versionString = strings.versionString
val hermesVersionString = strings.hermesVersionString
val hermesV1VersionString = strings.hermesV1VersionString
@@ -701,7 +720,8 @@ class DependencyUtilsTest {
)
}
val strings = readVersionAndGroupStrings(propertiesFile, hermesVersionFile)
val project = createProject()
val strings = readVersionAndGroupStrings(project, propertiesFile, hermesVersionFile)
val versionString = strings.versionString
val hermesVersionString = strings.hermesVersionString
val hermesV1VersionString = strings.hermesV1VersionString
@@ -736,7 +756,8 @@ class DependencyUtilsTest {
)
}
val strings = readVersionAndGroupStrings(propertiesFile, hermesVersionFile)
val project = createProject()
val strings = readVersionAndGroupStrings(project, propertiesFile, hermesVersionFile)
val reactGroupString = strings.reactGroupString
val hermesGroupString = strings.hermesGroupString
@@ -768,7 +789,8 @@ class DependencyUtilsTest {
)
}
val strings = readVersionAndGroupStrings(propertiesFile, hermesVersionFile)
val project = createProject()
val strings = readVersionAndGroupStrings(project, propertiesFile, hermesVersionFile)
val reactGroupString = strings.reactGroupString
val hermesGroupString = strings.hermesGroupString
@@ -840,4 +862,39 @@ class DependencyUtilsTest {
val project = createProject(tempFolder.root)
assertThat(project.exclusiveEnterpriseRepository()).isNull()
}
@Test
fun isNightly_returnsTrue_forValidNightlyVersions() {
val trueCases =
listOf(
"0.85.0-nightly-20260128-36f07a1b2",
"0.82.0-nightly-date-commit",
"0.0.0-20230505-2109-9b69263a1",
"0.0.0-date-commit",
"0.0.0-nightly-",
)
trueCases.forEach { version ->
assert(version.isNightly()) { "Expected '$version' to be detected as nightly" }
}
}
@Test
fun isNightly_returnsFalse_forNonNightlyVersions() {
val falseCases =
listOf(
"0.83.0", // Standard version
"0.0.1",
"nightly", // Missing hyphens
"0.83.0-nightly", // Missing trailing hyphen
"any-nightly", // Missing trailing hyphen
"nightly-build", // Missing leading hyphen
"", // Empty string
" ", // Blank string
)
falseCases.forEach { version ->
assert(!version.isNightly()) { "Expected '$version' to NOT be detected as nightly" }
}
}
}