diff --git a/packages/react-native-gradle-plugin/build.gradle.kts b/packages/react-native-gradle-plugin/build.gradle.kts index 5f1a955af52..d7ad553195f 100644 --- a/packages/react-native-gradle-plugin/build.gradle.kts +++ b/packages/react-native-gradle-plugin/build.gradle.kts @@ -5,6 +5,9 @@ * LICENSE file in the root directory of this source tree. */ +import org.gradle.api.internal.classpath.ModuleRegistry +import org.gradle.configurationcache.extensions.serviceOf + plugins { kotlin("jvm") version "1.4.21" id("java-gradle-plugin") @@ -27,5 +30,12 @@ gradlePlugin { dependencies { implementation(gradleApi()) implementation("com.android.tools.build:gradle:4.2.2") + testImplementation("junit:junit:4.13.2") + + testRuntimeOnly( + files( + serviceOf().getModule("gradle-tooling-api-builders").classpath.asFiles.first() + ) + ) } 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 index b7de384df6d..5babb8d50aa 100644 --- 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 @@ -11,7 +11,8 @@ import com.android.build.gradle.BaseExtension import org.gradle.api.Project fun Project.configureDevPorts(androidExt: BaseExtension) { - val devServerPort = project.properties["reactNativeDevServerPort"]?.toString() ?: "8081" + val devServerPort = + project.properties["reactNativeDevServerPort"]?.toString() ?: DEFAULT_DEV_SERVER_PORT val inspectorProxyPort = project.properties["reactNativeInspectorProxyPort"]?.toString() ?: devServerPort @@ -20,3 +21,5 @@ fun Project.configureDevPorts(androidExt: BaseExtension) { 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/test/kotlin/com/facebook/react/AndroidConfigurationTest.kt b/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/AndroidConfigurationTest.kt new file mode 100644 index 00000000000..34f799ed505 --- /dev/null +++ b/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/AndroidConfigurationTest.kt @@ -0,0 +1,68 @@ +/* + * Copyright (c) Facebook, Inc. and its 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("react_native_dev_server_port")?.value) + assertEquals("8081", debug?.resValues?.get("react_native_inspector_proxy_port")?.value) + assertEquals("8081", release?.resValues?.get("react_native_dev_server_port")?.value) + assertEquals("8081", release?.resValues?.get("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("react_native_dev_server_port")?.value) + assertEquals("42424", debug?.resValues?.get("react_native_inspector_proxy_port")?.value) + assertEquals("42424", release?.resValues?.get("react_native_dev_server_port")?.value) + assertEquals("42424", release?.resValues?.get("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("react_native_dev_server_port")?.value) + assertEquals("42424", debug?.resValues?.get("react_native_inspector_proxy_port")?.value) + assertEquals("8081", release?.resValues?.get("react_native_dev_server_port")?.value) + assertEquals("42424", release?.resValues?.get("react_native_inspector_proxy_port")?.value) + } +}