diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/AndroidConfiguration.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/AndroidConfiguration.kt deleted file mode 100644 index 48fe2cccea4e12..00000000000000 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/AndroidConfiguration.kt +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -package com.facebook.react - -import com.android.build.gradle.BaseExtension -import org.gradle.api.Project - -fun Project.configureDevPorts(androidExt: BaseExtension) { - val devServerPort = - project.properties["reactNativeDevServerPort"]?.toString() ?: DEFAULT_DEV_SERVER_PORT - val inspectorProxyPort = - project.properties["reactNativeInspectorProxyPort"]?.toString() ?: devServerPort - - androidExt.buildTypes.all { - it.resValue("integer", "react_native_dev_server_port", devServerPort) - it.resValue("integer", "react_native_inspector_proxy_port", inspectorProxyPort) - } -} - -const val DEFAULT_DEV_SERVER_PORT = "8081" diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactPlugin.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactPlugin.kt index 079c6104d36e1e..81a708ff0456e5 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactPlugin.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactPlugin.kt @@ -9,13 +9,13 @@ package com.facebook.react import com.android.build.api.variant.AndroidComponentsExtension import com.android.build.gradle.AppExtension -import com.android.build.gradle.BaseExtension import com.android.build.gradle.LibraryExtension import com.android.build.gradle.internal.tasks.factory.dependsOn import com.facebook.react.tasks.BuildCodegenCLITask import com.facebook.react.tasks.GenerateCodegenArtifactsTask import com.facebook.react.tasks.GenerateCodegenSchemaTask import com.facebook.react.utils.AgpConfiguratorUtils.configureBuildConfigFields +import com.facebook.react.utils.AgpConfiguratorUtils.configureDevPorts import com.facebook.react.utils.JsonUtils import com.facebook.react.utils.NdkConfiguratorUtils.configureReactNativeNdk import com.facebook.react.utils.findPackageJsonFile @@ -57,11 +57,9 @@ class ReactPlugin : Plugin { private fun applyAppPlugin(project: Project, config: ReactExtension) { configureReactNativeNdk(project, config) configureBuildConfigFields(project) + configureDevPorts(project) project.afterEvaluate { if (config.applyAppPlugin.getOrElse(false)) { - val androidConfiguration = project.extensions.getByType(BaseExtension::class.java) - project.configureDevPorts(androidConfiguration) - val isAndroidLibrary = project.plugins.hasPlugin("com.android.library") val variants = if (isAndroidLibrary) { diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/AgpConfiguratorUtils.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/AgpConfiguratorUtils.kt index 541338c8472c7a..3bf008535f7e02 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/AgpConfiguratorUtils.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/AgpConfiguratorUtils.kt @@ -14,8 +14,9 @@ import org.gradle.api.Action import org.gradle.api.Project import org.gradle.api.plugins.AppliedPlugin +@Suppress("UnstableApiUsage") internal object AgpConfiguratorUtils { - @Suppress("UnstableApiUsage") + fun configureBuildConfigFields(project: Project) { val action = Action { @@ -29,4 +30,25 @@ internal object AgpConfiguratorUtils { project.pluginManager.withPlugin("com.android.application", action) project.pluginManager.withPlugin("com.android.library", action) } + + fun configureDevPorts(project: Project) { + val devServerPort = + project.properties["reactNativeDevServerPort"]?.toString() ?: DEFAULT_DEV_SERVER_PORT + val inspectorProxyPort = + project.properties["reactNativeInspectorProxyPort"]?.toString() ?: devServerPort + + val action = + Action { + project.extensions.getByType(AndroidComponentsExtension::class.java).finalizeDsl { ext -> + ext.defaultConfig.resValue("integer", "react_native_dev_server_port", devServerPort) + ext.defaultConfig.resValue( + "integer", "react_native_inspector_proxy_port", inspectorProxyPort) + } + } + + project.pluginManager.withPlugin("com.android.application", action) + project.pluginManager.withPlugin("com.android.library", action) + } } + +const val DEFAULT_DEV_SERVER_PORT = "8081" diff --git a/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/AndroidConfigurationTest.kt b/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/AndroidConfigurationTest.kt deleted file mode 100644 index 3c91f35e5a5aa6..00000000000000 --- a/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/AndroidConfigurationTest.kt +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -package com.facebook.react - -import com.android.build.gradle.BaseExtension -import org.gradle.testfixtures.ProjectBuilder -import org.junit.Assert.assertEquals -import org.junit.Test - -class AndroidConfigurationTest { - - @Test - fun configureDevPorts_withNoSpecifiedPort_usesDefault() { - val project = ProjectBuilder.builder().build() - project.pluginManager.apply("com.android.application") - val androidExtension = project.extensions.getByType(BaseExtension::class.java) - val debug = androidExtension.buildTypes.findByName("debug") - val release = androidExtension.buildTypes.findByName("release") - - project.configureDevPorts(androidExtension) - - assertEquals("8081", debug?.resValues?.get("integer/react_native_dev_server_port")?.value) - assertEquals("8081", debug?.resValues?.get("integer/react_native_inspector_proxy_port")?.value) - assertEquals("8081", release?.resValues?.get("integer/react_native_dev_server_port")?.value) - assertEquals( - "8081", release?.resValues?.get("integer/react_native_inspector_proxy_port")?.value) - } - - @Test - fun configureDevPorts_withSpecifiedReactNativeDevServerPort_usesIt() { - val project = ProjectBuilder.builder().build() - project.pluginManager.apply("com.android.application") - val androidExtension = project.extensions.getByType(BaseExtension::class.java) - val debug = androidExtension.buildTypes.findByName("debug") - val release = androidExtension.buildTypes.findByName("release") - - project.extensions.extraProperties["reactNativeDevServerPort"] = "42424" - - project.configureDevPorts(androidExtension) - - assertEquals("42424", debug?.resValues?.get("integer/react_native_dev_server_port")?.value) - assertEquals("42424", debug?.resValues?.get("integer/react_native_inspector_proxy_port")?.value) - assertEquals("42424", release?.resValues?.get("integer/react_native_dev_server_port")?.value) - assertEquals( - "42424", release?.resValues?.get("integer/react_native_inspector_proxy_port")?.value) - } - - @Test - fun configureDevPorts_withSpecifiedReactNativeInspectorProxyPort_usesIt() { - val project = ProjectBuilder.builder().build() - project.pluginManager.apply("com.android.application") - val androidExtension = project.extensions.getByType(BaseExtension::class.java) - val debug = androidExtension.buildTypes.findByName("debug") - val release = androidExtension.buildTypes.findByName("release") - - project.extensions.extraProperties["reactNativeInspectorProxyPort"] = "42424" - - project.configureDevPorts(androidExtension) - - assertEquals("8081", debug?.resValues?.get("integer/react_native_dev_server_port")?.value) - assertEquals("42424", debug?.resValues?.get("integer/react_native_inspector_proxy_port")?.value) - assertEquals("8081", release?.resValues?.get("integer/react_native_dev_server_port")?.value) - assertEquals( - "42424", release?.resValues?.get("integer/react_native_inspector_proxy_port")?.value) - } -}