From 0b2fb28f0cf7397312d8abd9d2e9ddb3313517b6 Mon Sep 17 00:00:00 2001 From: Joshua Quick Date: Tue, 9 Jun 2020 06:00:58 -0700 Subject: [PATCH] chore(android)(9_0_X): auto-download NDK r21c for module builds if needed (#11756) * chore(android): auto-download NDK r21c for module builds - This NDK version was notarized for macOS Catalina. Prevents security popups when doing module builds. * fix(android): bad file descriptor errors with NDK v21 builds - Compiling with Android NDK 21 and higher would cause several harmless "Bad file descriptor" errors to be logged. - Caused by NDK 21 setting "--output-sync" argument default. Setting it to "none" restores old behavior. * fix(android): only use --output-sync for NDK 21+ - Using NDK command line argument --output-sync will cause a build failure on NDK 19 and lower. And we don't need to pass it into NDK 20. * chore(android): removed TIMOB-27939 commented-out code Co-authored-by: Christopher Williams Co-authored-by: Gary Mathews --- .../templates/module/generated/build.gradle | 36 ++++++++++++++----- android/titanium/build.gradle | 25 +++++++++++++ 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/android/templates/module/generated/build.gradle b/android/templates/module/generated/build.gradle index df594208292..666066cebc0 100644 --- a/android/templates/module/generated/build.gradle +++ b/android/templates/module/generated/build.gradle @@ -14,6 +14,28 @@ repositories { // Path to proxy binding JSON file created by "kroll-apt" annotation processor and used to do C/C++ code generation. def tiModuleBindingsJsonPath = "${buildDir}/ti-generated/json/<%- moduleName %>.json".toString() +// Fetch Android NDK version assigned to the "local.properties" file. (Will be missing if NDK not installed.) +def ndkVersionString = null; +try { + def localProperties = new Properties() + localProperties.load(file("${rootDir}/local.properties").newDataInputStream()) + def ndkDir = localProperties.get('ndk.dir') + if (ndkDir != null) { + def ndkSourceProperties = new Properties() + ndkSourceProperties.load(file("${ndkDir}/source.properties").newDataInputStream()) + ndkVersionString = ndkSourceProperties.get('Pkg.Revision') + } +} catch (Throwable) { +} + +// If Titanium failed to set Android NDK path in "local.properties", then assume NDK is not installed. +// Have gradle auto-download NDK by setting the version we want below. (Will fail if a different version is installed.) +// Must be set to a stable release version listed here: https://developer.android.com/ndk/downloads +if (ndkVersionString == null) { + ndkVersionString = '21.2.6472646' + android.ndkVersion ndkVersionString +} + android { compileSdkVersion <%- compileSdkVersion %> defaultConfig { @@ -60,6 +82,11 @@ android { 'APP_STL:=c++_shared', "-j${Runtime.runtime.availableProcessors()}".toString() ]) + if (Integer.parseInt(ndkVersionString.split('\\.')[0]) >= 21) { + // Work-around issue where Google sets "--output-sync=target" by default for NDK v21+ + // which causes "bad file descriptor" errors when using "-j" parallel executions argument. + arguments.add('--output-sync=none') + } } } ndk { @@ -151,15 +178,6 @@ preBuild.doFirst { } } -// If Titanium failed to set Android NDK path in "local.properties", then assume NDK is not installed. -// Have gradle auto-download NDK by setting the version we want below. (Will fail if a different version is installed.) -// Must be set to a stable release version listed here: https://developer.android.com/ndk/downloads -def localProperties = new Properties() -localProperties.load(file("${rootDir}/local.properties").newDataInputStream()) -if (localProperties.get('ndk.dir') == null) { - android.ndkVersion '20.1.5948944' -} - // Set up project to compile Java side before compiling the C/C++ side. // We must do this because our "kroll-apt" Java annotation processor generates C++ source files. project.afterEvaluate { diff --git a/android/titanium/build.gradle b/android/titanium/build.gradle index 9d4eb4719db..86918b78eb0 100644 --- a/android/titanium/build.gradle +++ b/android/titanium/build.gradle @@ -37,6 +37,26 @@ for (nextString in tiBuildVersionString.split('\\.')) { } } +// Fetch Android NDK major version set via "local.properties" or "ANDROID_NDK_HOME" environment variable. +def ndkVersionMajor = 0 +try { + def localProperties = new Properties() + localProperties.load(file("${rootDir}/local.properties").newDataInputStream()) + def ndkDir = localProperties.get('ndk.dir') + if (ndkDir == null) { + ndkDir = System.env.ANDROID_NDK_HOME + } + if (ndkDir != null) { + def ndkSourceProperties = new Properties() + ndkSourceProperties.load(file("${ndkDir}/source.properties").newDataInputStream()) + def ndkVersionString = ndkSourceProperties.get('Pkg.Revision') + if (ndkVersionString != null) { + ndkVersionMajor = Integer.parseInt(ndkVersionString.split('\\.')[0]) + } + } +} catch (Throwable) { +} + android { compileSdkVersion 29 defaultConfig { @@ -67,6 +87,11 @@ android { "NDK_MODULE_PATH+=${projectDir}/../runtime/v8/src/ndk-modules".replace('\\', '\\\\'), "-j${Runtime.runtime.availableProcessors()}".toString() ]) + if (ndkVersionMajor >= 21) { + // Work-around issue where Google sets "--output-sync=target" by default for NDK v21+ + // which causes "bad file descriptor" errors when using "-j" parallel executions argument. + arguments.add('--output-sync=none') + } if (System.getProperty('os.name').toLowerCase().contains('windows')) { // Reduces length of NDK command line strings by writing source file list to text files. arguments.add('APP_SHORT_COMMANDS=true')