Skip to content

Commit

Permalink
chore(android)(9_0_X): auto-download NDK r21c for module builds if ne…
Browse files Browse the repository at this point in the history
…eded (#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 <chris.a.williams@gmail.com>
Co-authored-by: Gary Mathews <contact@garymathews.com>
  • Loading branch information
3 people committed Jun 9, 2020
1 parent e9c58c3 commit 0b2fb28
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
36 changes: 27 additions & 9 deletions android/templates/module/generated/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions android/titanium/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 0b2fb28

Please sign in to comment.