Merge pull request #2435 from sigmabeta/android-native-autodetect

Android: Have build.gradle figure out what ABI and Toolchain to use.
This commit is contained in:
Matthew Parlane 2015-05-25 10:11:52 +10:00
commit 34cd2b0299
2 changed files with 59 additions and 19 deletions

View File

@ -60,19 +60,14 @@ Android Studio will do this for you if you create `Source/Android/build.properti
following inside: following inside:
``` ```
toolchain=<toolchain>
abi=<abi>
makeArgs=<make-args> makeArgs=<make-args>
``` ```
Replace `<make-args>` with any arguments you want to pass to `make`, and the rest depending on which Replace `<make-args>` with any arguments you want to pass to `make`, and then execute the
platform the Android device you are targeting uses: `assembleDebug` or `installDebug` task corresponding to the hardware platform you are targeting.
For example, to deploy to a Nexus 9, which runs the AArch64 architecture, execute `installArm_64Debug`.
|Platform | `<abi>` | `<toolchain>` | A list of available tasks can be found in Android Studio in the Gradle tray, located at the top-right
|-------------------------|-------------|---------------------------| corner of the IDE by default.
|ARM 32-bit (most devices)| armeabi-v7a | arm-linux-androideabi-4.9 |
|ARM 64-bit (i.e. Nexus 9)| arm64-v8a | aarch64-linux-android-4.9 |
|Intel 64-bit | x86_64 | x86_64-4.9 |
The native libraries will be compiled, and copied into `./Source/Android/app/libs`. Android Studio The native libraries will be compiled, and copied into `./Source/Android/app/libs`. Android Studio
and Gradle will include any libraries in that folder into the APK at build time. and Gradle will include any libraries in that folder into the APK at build time.

View File

@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
android { android {
compileSdkVersion 21 compileSdkVersion 21
buildToolsVersion "20.0.0" buildToolsVersion "22.0.1"
lintOptions { lintOptions {
// This is important as it will run lint but not abort on error // This is important as it will run lint but not abort on error
@ -69,13 +69,12 @@ android {
} }
} }
// TODO Uncomment this when we successfully build for x86_64. x86_64 {
/*x86_64 {
flavorDimension "abi" flavorDimension "abi"
ndk { ndk {
abiFilter "x86_64" abiFilter "x86_64"
} }
}*/ }
} }
} }
@ -103,8 +102,10 @@ task setupCMake(type: Exec) {
def buildProperties = new Properties() def buildProperties = new Properties()
buildProperties.load(new FileInputStream(propsFile)) buildProperties.load(new FileInputStream(propsFile))
mkdir('build/' + buildProperties.abi) String abi = getAbi()
workingDir 'build/' + buildProperties.abi
mkdir('build/' + abi)
workingDir 'build/' + abi
executable 'cmake' executable 'cmake'
@ -114,8 +115,8 @@ task setupCMake(type: Exec) {
"../../../../..", "../../../../..",
"-DGIT_EXECUTABLE=" + getGitPath(), "-DGIT_EXECUTABLE=" + getGitPath(),
"-DANDROID_NDK=" + getNdkPath(), "-DANDROID_NDK=" + getNdkPath(),
"-DANDROID_TOOLCHAIN_NAME=" + buildProperties.toolchain, "-DANDROID_TOOLCHAIN_NAME=" + getToolchainName(),
"-DANDROID_ABI=" + buildProperties.abi "-DANDROID_ABI=" + abi
} else { } else {
executable 'echo' executable 'echo'
args 'No build.properties found; skipping CMake.' args 'No build.properties found; skipping CMake.'
@ -132,7 +133,9 @@ task compileNative(type: Exec, dependsOn: 'setupCMake') {
def buildProperties = new Properties() def buildProperties = new Properties()
buildProperties.load(new FileInputStream(propsFile)) buildProperties.load(new FileInputStream(propsFile))
workingDir 'build/' + buildProperties.abi String abi = getAbi()
workingDir 'build/' + abi
executable 'make' executable 'make'
@ -184,3 +187,45 @@ String getNdkPath() {
return null; return null;
} }
} }
String getAbi() {
String taskName = getGradle().startParameter.taskNames[0]
String abi;
if (taskName == null) {
return ""
}
project.logger.quiet("Gradle: Build = " + taskName)
if (taskName.contains("Arm_64")) {
abi = "arm64-v8a"
} else if (taskName.contains("Arm")) {
abi = "armeabi-v7a"
} else if (taskName.contains("X86_64")) {
abi = "x86_64"
}
project.logger.quiet("Gradle: ABI name: " + abi)
return abi;
}
String getToolchainName() {
String taskName = getGradle().startParameter.taskNames[0]
String toolchain;
if (taskName == null) {
return ""
}
if (taskName.contains("Arm_64")) {
toolchain = "aarch64-linux-android-4.9"
} else if (taskName.contains("Arm")) {
toolchain = "arm-linux-androideabi-4.9"
} else if (taskName.contains("X86_64")) {
toolchain = "x86_64-4.9"
}
project.logger.quiet("Gradle: ABI name: " + toolchain)
return toolchain;
}