Skip to content

Commit

Permalink
Let RNGP set buildConfigFields for New Architecture and Hermes
Browse files Browse the repository at this point in the history
Summary:
This diff further simplifies the New App Template by removing the
buildConfigFields for both Hermes and New Architecture with the React Native Gradle Plugin.

Changelog:
[Android] [Changed] - Let RNGP set buildConfigFields for New Architecture and Hermes

Reviewed By: cipolleschi

Differential Revision: D40139559

fbshipit-source-id: 202b6ac9c4c036a026a2ff3e8279087ffd363eaa
  • Loading branch information
cortinico authored and facebook-github-bot committed Oct 6, 2022
1 parent 40567c2 commit 7d2f48c
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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.JsonUtils
import com.facebook.react.utils.NdkConfiguratorUtils.configureReactNativeNdk
import com.facebook.react.utils.findPackageJsonFile
Expand Down Expand Up @@ -55,6 +56,7 @@ class ReactPlugin : Plugin<Project> {

private fun applyAppPlugin(project: Project, config: ReactExtension) {
configureReactNativeNdk(project, config)
configureBuildConfigFields(project)
project.afterEvaluate {
if (config.applyAppPlugin.getOrElse(false)) {
val androidConfiguration = project.extensions.getByType(BaseExtension::class.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.utils

import com.android.build.api.variant.AndroidComponentsExtension
import com.facebook.react.utils.ProjectUtils.isHermesEnabled
import com.facebook.react.utils.ProjectUtils.isNewArchEnabled
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.plugins.AppliedPlugin

internal object AgpConfiguratorUtils {
@Suppress("UnstableApiUsage")
fun configureBuildConfigFields(project: Project) {
val action =
Action<AppliedPlugin> {
project.extensions.getByType(AndroidComponentsExtension::class.java).finalizeDsl { ext ->
ext.defaultConfig.buildConfigField(
"boolean", "IS_NEW_ARCHITECTURE_ENABLED", project.isNewArchEnabled.toString())
ext.defaultConfig.buildConfigField(
"boolean", "IS_HERMES_ENABLED", project.isHermesEnabled.toString())
}
}
project.pluginManager.withPlugin("com.android.application", action)
project.pluginManager.withPlugin("com.android.library", action)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,22 @@ internal object ProjectUtils {
get() =
project.hasProperty("newArchEnabled") &&
project.property("newArchEnabled").toString().toBoolean()

const val HERMES_FALLBACK = true

internal val Project.isHermesEnabled: Boolean
get() =
if (project.hasProperty("enableHermes")) {
project.property("enableHermes").toString().lowercase().toBooleanStrictOrNull() ?: true
} else if (project.extensions.extraProperties.has("react")) {
@Suppress("UNCHECKED_CAST")
val reactMap = project.extensions.extraProperties.get("react") as? Map<String, Any?>
when (val enableHermesKey = reactMap?.get("enableHermes")) {
is Boolean -> enableHermesKey
is String -> enableHermesKey.lowercase().toBooleanStrictOrNull() ?: true
else -> HERMES_FALLBACK
}
} else {
HERMES_FALLBACK
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package com.facebook.react.utils

import com.facebook.react.tests.createProject
import com.facebook.react.utils.ProjectUtils.isHermesEnabled
import com.facebook.react.utils.ProjectUtils.isNewArchEnabled
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
Expand Down Expand Up @@ -40,4 +41,62 @@ class ProjectUtilsTest {
project.extensions.extraProperties.set("newArchEnabled", "¯\\_(ツ)_/¯")
assertFalse(project.isNewArchEnabled)
}

@Test
fun isHermesEnabled_returnsTrueByDefault() {
assertTrue(createProject().isHermesEnabled)
}

@Test
fun isNewArchEnabled_withDisabledViaProperty_returnsFalse() {
val project = createProject()
project.extensions.extraProperties.set("enableHermes", "false")
assertFalse(project.isHermesEnabled)
}

@Test
fun isHermesEnabled_withEnabledViaProperty_returnsTrue() {
val project = createProject()
project.extensions.extraProperties.set("enableHermes", "true")
assertTrue(project.isHermesEnabled)
}

@Test
fun isHermesEnabled_withInvalidViaProperty_returnsTrue() {
val project = createProject()
project.extensions.extraProperties.set("enableHermes", "¯\\_(ツ)_/¯")
assertTrue(project.isHermesEnabled)
}

@Test
fun isHermesEnabled_withDisabledViaExt_returnsFalse() {
val project = createProject()
val extMap = mapOf("enableHermes" to false)
project.extensions.extraProperties.set("react", extMap)
assertFalse(project.isHermesEnabled)
}

@Test
fun isHermesEnabled_withEnabledViaExt_returnsTrue() {
val project = createProject()
val extMap = mapOf("enableHermes" to true)
project.extensions.extraProperties.set("react", extMap)
assertTrue(project.isHermesEnabled)
}

@Test
fun isHermesEnabled_withDisabledViaExtAsString_returnsFalse() {
val project = createProject()
val extMap = mapOf("enableHermes" to "false")
project.extensions.extraProperties.set("react", extMap)
assertFalse(project.isHermesEnabled)
}

@Test
fun isHermesEnabled_withInvalidViaExt_returnsTrue() {
val project = createProject()
val extMap = mapOf("enableHermes" to "¯\\_(ツ)_/¯")
project.extensions.extraProperties.set("react", extMap)
assertTrue(project.isHermesEnabled)
}
}

0 comments on commit 7d2f48c

Please sign in to comment.