From 3a321ae2bb623a8f5c7f064d82ca8ca9df3ebff4 Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Wed, 17 May 2023 09:13:38 -0700 Subject: [PATCH] Make sure the -DANDROID compilation flag is always included. (#37451) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/37451 It seems like starting from AGP 7.4, Android is not including the `-DANDROID` flag anymore from the NDK toolchain. As we do have several `#ifdef` logic that takes care of having this macro set, I'm going to make sure it's always set for both ReactAndroid, hermes-engine and the template. Changelog: [Android] [Fixed] - Make sure the -DANDROID compilation flag is always included Reviewed By: javache Differential Revision: D45908787 fbshipit-source-id: 07284712d7bcce73dc8ea0dffd4a9d00af4de1d2 --- .../com/facebook/react/utils/NdkConfiguratorUtils.kt | 12 +++++++++--- packages/react-native/ReactAndroid/build.gradle | 5 ++++- .../ReactAndroid/hermes-engine/build.gradle | 3 +++ .../ReactAndroid/src/main/jni/CMakeLists.txt | 1 - 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/NdkConfiguratorUtils.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/NdkConfiguratorUtils.kt index 20e28696f1312f..28a33b85f8fc3f 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/NdkConfiguratorUtils.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/NdkConfiguratorUtils.kt @@ -40,16 +40,22 @@ internal object NdkConfiguratorUtils { // Parameters should be provided in an additive manner (do not override what // the user provided, but allow for sensible defaults). val cmakeArgs = ext.defaultConfig.externalNativeBuild.cmake.arguments - if ("-DPROJECT_BUILD_DIR" !in cmakeArgs) { + if (cmakeArgs.none { it.startsWith("-DPROJECT_BUILD_DIR") }) { cmakeArgs.add("-DPROJECT_BUILD_DIR=${project.buildDir}") } - if ("-DREACT_ANDROID_DIR" !in cmakeArgs) { + if (cmakeArgs.none { it.startsWith("-DREACT_ANDROID_DIR") }) { cmakeArgs.add( "-DREACT_ANDROID_DIR=${extension.reactNativeDir.file("ReactAndroid").get().asFile}") } - if ("-DANDROID_STL" !in cmakeArgs) { + if (cmakeArgs.none { it.startsWith("-DANDROID_STL") }) { cmakeArgs.add("-DANDROID_STL=c++_shared") } + // Due to the new NDK toolchain file, the C++ flags gets overridden between compilation + // units. This is causing some libraries to don't be compiled with -DANDROID and other + // crucial flags. This can be revisited once we bump to NDK 25/26 + if (cmakeArgs.none { it.startsWith("-DANDROID_USE_LEGACY_TOOLCHAIN_FILE") }) { + cmakeArgs.add("-DANDROID_USE_LEGACY_TOOLCHAIN_FILE=ON") + } val architectures = project.getReactNativeArchitectures() // abiFilters are split ABI are not compatible each other, so we set the abiFilters diff --git a/packages/react-native/ReactAndroid/build.gradle b/packages/react-native/ReactAndroid/build.gradle index 084cfa62942683..d65e409316adf2 100644 --- a/packages/react-native/ReactAndroid/build.gradle +++ b/packages/react-native/ReactAndroid/build.gradle @@ -473,7 +473,10 @@ android { "-DREACT_BUILD_DIR=$buildDir", "-DANDROID_STL=c++_shared", "-DANDROID_TOOLCHAIN=clang", - "-DANDROID_PLATFORM=android-21" + "-DANDROID_PLATFORM=android-21", + // Due to https://github.com/android/ndk/issues/1693 we're losing Android + // specific compilation flags. This can be removed once we moved to NDK 25/26 + "-DANDROID_USE_LEGACY_TOOLCHAIN_FILE=ON" targets "jsijniprofiler", "reactnativeblob", diff --git a/packages/react-native/ReactAndroid/hermes-engine/build.gradle b/packages/react-native/ReactAndroid/hermes-engine/build.gradle index 2761a584427eb7..ce03961aefa3d8 100644 --- a/packages/react-native/ReactAndroid/hermes-engine/build.gradle +++ b/packages/react-native/ReactAndroid/hermes-engine/build.gradle @@ -154,6 +154,9 @@ android { // We intentionally build Hermes with Intl support only. This is to simplify // the build setup and to avoid overcomplicating the build-type matrix. arguments "-DHERMES_ENABLE_INTL=True" + // Due to https://github.com/android/ndk/issues/1693 we're losing Android + // specific compilation flags. This can be removed once we moved to NDK 25/26 + arguments "-DANDROID_USE_LEGACY_TOOLCHAIN_FILE=ON" targets "libhermes" } } diff --git a/packages/react-native/ReactAndroid/src/main/jni/CMakeLists.txt b/packages/react-native/ReactAndroid/src/main/jni/CMakeLists.txt index 824d4216c95724..78f2b949d92eee 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/CMakeLists.txt +++ b/packages/react-native/ReactAndroid/src/main/jni/CMakeLists.txt @@ -190,7 +190,6 @@ add_executable(reactnative_unittest -fexceptions -frtti -std=c++17 - -DANDROID -DHERMES_ENABLE_DEBUGGER) target_link_libraries(reactnative_unittest