Move configureDevPorts inside AgpConfiguratorUtils and use finalizeDsl

Summary:
This moves configureDevPorts inside AgpConfiguratorUtils, and makes it use
finalizeDsl. This removes a deprecated API `BaseVariant` which is causing a
build warning for all the users + it's a step towards making RNGP fully variant aware.

Changelog:
[Internal] [Changed] - Move configureDevPorts inside AgpConfiguratorUtils and use finalizeDsl

Reviewed By: cipolleschi

Differential Revision: D40139558

fbshipit-source-id: 1e5b8661b8d223a603f8ab5d160edd2133304d92
This commit is contained in:
Nicola Corti
2022-10-12 04:34:28 -07:00
committed by Facebook GitHub Bot
parent 20a8dcdd93
commit 90faf0f254
4 changed files with 25 additions and 101 deletions
@@ -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"
@@ -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<Project> {
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) {
@@ -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<AppliedPlugin> {
@@ -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<AppliedPlugin> {
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"
@@ -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)
}
}