Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(android)(9_0_X): auto-download NDK r21c for module builds if needed #11756

Merged
merged 7 commits into from
Jun 9, 2020
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