Fix Android autolink plugin for libraries that are platform specific (#45223)

Summary:
Fixes https://github.com/facebook/react-native/issues/45222

## Changelog:

[ANDROID] [FIXED] - Fix autolink plugin for libraries that are platform-specific

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

Test Plan: And a library that does not have Android native code such as react-native-segmented-control/segmented-control and sync gradle

Reviewed By: rshest

Differential Revision: D59221562

Pulled By: cortinico

fbshipit-source-id: 55739d63ded63e46897d0d770281f937668c1f50
This commit is contained in:
Gabriel Donadel
2024-07-01 05:05:23 -07:00
committed by Nicola Corti
parent 3252855e1d
commit f86bc9512f
2 changed files with 38 additions and 3 deletions
@@ -112,9 +112,15 @@ abstract class ReactSettingsExtension @Inject constructor(val settings: Settings
internal fun getLibrariesToAutolink(buildFile: File): Map<String, File> {
val model = JsonUtils.fromAutolinkingConfigJson(buildFile)
return model?.dependencies?.values?.associate { deps ->
":${deps.nameCleansed}" to File(deps.platforms?.android?.sourceDir)
} ?: emptyMap()
return model
?.dependencies
?.values
// We handle scenarios where there are deps that are
// iOS-only or missing the Android configs.
?.filter { it.platforms?.android?.sourceDir != null }
?.associate { deps ->
":${deps.nameCleansed}" to File(deps.platforms?.android?.sourceDir)
} ?: emptyMap()
}
internal fun computeSha256(lockFile: File) =
@@ -103,6 +103,35 @@ class ReactSettingsExtensionTest {
map[":react-native_oss-library-example"])
}
@Test
fun getLibrariesToAutolink_withiOSOnlyLibrary_returnsEmptyMap() {
val validJsonFile =
createJsonFile(
"""
{
"reactNativeVersion": "1000.0.0",
"dependencies": {
"@react-native/oss-library-example": {
"root": "./node_modules/@react-native/oss-library-example",
"name": "@react-native/oss-library-example",
"platforms": {
"ios": {
"podspecPath": "./node_modules/@react-native/oss-library-example/OSSLibraryExample.podspec",
"version": "0.0.1",
"configurations": [],
"scriptPhases": []
}
}
}
}
}
"""
.trimIndent())
val map = getLibrariesToAutolink(validJsonFile)
assertEquals(0, map.keys.size)
}
@Test
fun checkAndUpdateLockfiles_withNothingToCheck_returnsFalse() {
val project = ProjectBuilder.builder().build()