From 5a8033df98296c941b0a57e49f2349e252339bf9 Mon Sep 17 00:00:00 2001 From: Anandraj Date: Wed, 6 Apr 2022 16:50:42 -0700 Subject: [PATCH 1/2] Fix for building new architecture sources on Windows On Windows there are limits on number of character in file paths and in command lines Ref: https://android.googlesource.com/platform/ndk/+/master/docs/BuildSystemMaintainers.md#Path-Length-Limits NDK allows circumventing command line limits using response(RSP) files as inputs using NDK_APP_SHORT_COMMANDS flag. Windows can support long file paths if configured through registry or by prefixing all file paths with a special character sequence The latter requires changes in NDK. And there are tools in NDK (AR) which is not able to handle long paths (>256) even after setting the registry key. The new architecutre source tree is too deep, and the object file naming conventions in NDK makes the matters worse, by producing incredibly long file paths. Other solutions such as symlinking source code etc. didn't work as expected, and makes the build scripts complicated and hard to manage. This change temporarily works around the issue by placing the temporary build outputs as short a path as possible within the project path. Changelog: [Android] [Fixed] - Fix for building new architecture sources on Windows --- ReactAndroid/build.gradle | 14 ++++++++++++++ template/android/app/build.gradle | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/ReactAndroid/build.gradle b/ReactAndroid/build.gradle index 5a384f23c12..9f9adaf4bac 100644 --- a/ReactAndroid/build.gradle +++ b/ReactAndroid/build.gradle @@ -318,6 +318,20 @@ android { // This flag will suppress "fcntl(): Bad file descriptor" warnings on local builds. arguments "--output-sync=none" } + + // Note: On Windows there are limits on number of character in file paths and in command lines + // Ref: https://android.googlesource.com/platform/ndk/+/master/docs/BuildSystemMaintainers.md#Path-Length-Limits + // NDK allows circumventing command line limits using response(RSP) files as inputs using NDK_APP_SHORT_COMMANDS flag. + // + // Windows can support long file paths if configured through registry or by prefixing all file paths with a special character sequence + // The latter requires changes in NDK. And there are tools in NDK (AR) which is not able to handle long paths (>256) even after setting the registry key. + // The new architecutre source tree is too deep, and the object file naming conventions in NDK makes the matters worse, by producing incredibly long file paths. + // Other solutions such as symlinking source code etc. didn't work as expected, and makes the build scripts complicated and hard to manage. + // This change temporarily works around the issue by placing the temporary build outputs as short a path as possible within the project path. + if (Os.isFamily(Os.FAMILY_WINDOWS)) { + arguments "NDK_OUT=${rootProject.projectDir.getParent()}\\.cxx", + "NDK_APP_SHORT_COMMANDS=true" + } } } ndk { diff --git a/template/android/app/build.gradle b/template/android/app/build.gradle index a173c56b4e9..16bd537462e 100644 --- a/template/android/app/build.gradle +++ b/template/android/app/build.gradle @@ -1,6 +1,7 @@ apply plugin: "com.android.application" import com.android.build.OutputFile +import org.apache.tools.ant.taskdefs.condition.Os /** * The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets @@ -157,6 +158,12 @@ android { // Make sure this target name is the same you specify inside the // src/main/jni/Android.mk file for the `LOCAL_MODULE` variable. targets "helloworld_appmodules" + + // Fix for windows limit on number of character in file paths and in command lines + if (Os.isFamily(Os.FAMILY_WINDOWS)) { + arguments "NDK_OUT=${rootProject.projectDir.getParent()}\\.cxx", + "NDK_APP_SHORT_COMMANDS=true" + } } } } From bea84f66b0be0cd4ab80d1f506a857758813d9dd Mon Sep 17 00:00:00 2001 From: Anandraj Date: Tue, 12 Apr 2022 23:26:28 -0700 Subject: [PATCH 2/2] NDK bump --- ReactAndroid/build.gradle | 14 ++++++-------- template/android/app/build.gradle | 2 +- template/android/build.gradle | 12 +++++++++++- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/ReactAndroid/build.gradle b/ReactAndroid/build.gradle index 9f9adaf4bac..36c4f83347a 100644 --- a/ReactAndroid/build.gradle +++ b/ReactAndroid/build.gradle @@ -287,6 +287,10 @@ android { ndkVersion System.getenv("LOCAL_ANDROID_NDK_VERSION") } + if (ndkVersion == null && ndkPath == null && rootProject.ext.ndkVersion != null) { + ndkVersion rootProject.ext.ndkVersion + } + defaultConfig { minSdkVersion(21) targetSdkVersion(31) @@ -321,15 +325,9 @@ android { // Note: On Windows there are limits on number of character in file paths and in command lines // Ref: https://android.googlesource.com/platform/ndk/+/master/docs/BuildSystemMaintainers.md#Path-Length-Limits - // NDK allows circumventing command line limits using response(RSP) files as inputs using NDK_APP_SHORT_COMMANDS flag. - // - // Windows can support long file paths if configured through registry or by prefixing all file paths with a special character sequence - // The latter requires changes in NDK. And there are tools in NDK (AR) which is not able to handle long paths (>256) even after setting the registry key. - // The new architecutre source tree is too deep, and the object file naming conventions in NDK makes the matters worse, by producing incredibly long file paths. - // Other solutions such as symlinking source code etc. didn't work as expected, and makes the build scripts complicated and hard to manage. - // This change temporarily works around the issue by placing the temporary build outputs as short a path as possible within the project path. + // NDK r23+ can handle long file paths. But we are still limited by the command line limit on Windows. if (Os.isFamily(Os.FAMILY_WINDOWS)) { - arguments "NDK_OUT=${rootProject.projectDir.getParent()}\\.cxx", + arguments "NDK_OUT=${rootProject.projectDir}\\.cxx", "NDK_APP_SHORT_COMMANDS=true" } } diff --git a/template/android/app/build.gradle b/template/android/app/build.gradle index 16bd537462e..fd4e5fae3b2 100644 --- a/template/android/app/build.gradle +++ b/template/android/app/build.gradle @@ -161,7 +161,7 @@ android { // Fix for windows limit on number of character in file paths and in command lines if (Os.isFamily(Os.FAMILY_WINDOWS)) { - arguments "NDK_OUT=${rootProject.projectDir.getParent()}\\.cxx", + arguments "NDK_OUT=${rootProject.projectDir}\\.cxx", "NDK_APP_SHORT_COMMANDS=true" } } diff --git a/template/android/build.gradle b/template/android/build.gradle index 8a1d68f9c5c..14d16ce93dc 100644 --- a/template/android/build.gradle +++ b/template/android/build.gradle @@ -1,3 +1,5 @@ +import org.apache.tools.ant.taskdefs.condition.Os + // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { @@ -6,7 +8,15 @@ buildscript { minSdkVersion = 21 compileSdkVersion = 31 targetSdkVersion = 31 - ndkVersion = "21.4.7075529" + + if (Os.isFamily(Os.FAMILY_WINDOWS)) { + // Note: On Windows there are limits on number of character in file paths and command lines + // Ref: https://android.googlesource.com/platform/ndk/+/master/docs/BuildSystemMaintainers.md#Path-Length-Limits + // NDK r23+ can handle long file paths. But we are still limited by the command line limit on Windows. + ndkVersion = "23.1.7779620" + } else { + ndkVersion = "21.4.7075529" + } } repositories { google()