From f86bc9512f8ce2318e1e49afb5c1938f4e6202c9 Mon Sep 17 00:00:00 2001 From: Gabriel Donadel Date: Mon, 1 Jul 2024 05:05:23 -0700 Subject: [PATCH] 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 --- .../facebook/react/ReactSettingsExtension.kt | 12 ++++++-- .../react/ReactSettingsExtensionTest.kt | 29 +++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/packages/react-native-gradle-plugin/settings-plugin/src/main/kotlin/com/facebook/react/ReactSettingsExtension.kt b/packages/react-native-gradle-plugin/settings-plugin/src/main/kotlin/com/facebook/react/ReactSettingsExtension.kt index b72f6701f9e..b3e22bd73ad 100644 --- a/packages/react-native-gradle-plugin/settings-plugin/src/main/kotlin/com/facebook/react/ReactSettingsExtension.kt +++ b/packages/react-native-gradle-plugin/settings-plugin/src/main/kotlin/com/facebook/react/ReactSettingsExtension.kt @@ -112,9 +112,15 @@ abstract class ReactSettingsExtension @Inject constructor(val settings: Settings internal fun getLibrariesToAutolink(buildFile: File): Map { 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) = diff --git a/packages/react-native-gradle-plugin/settings-plugin/src/test/kotlin/com/facebook/react/ReactSettingsExtensionTest.kt b/packages/react-native-gradle-plugin/settings-plugin/src/test/kotlin/com/facebook/react/ReactSettingsExtensionTest.kt index 21454324253..72d687b2ef5 100644 --- a/packages/react-native-gradle-plugin/settings-plugin/src/test/kotlin/com/facebook/react/ReactSettingsExtensionTest.kt +++ b/packages/react-native-gradle-plugin/settings-plugin/src/test/kotlin/com/facebook/react/ReactSettingsExtensionTest.kt @@ -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()