From 9c19d91e1815d71f74853434b5e8e34d8871b67a Mon Sep 17 00:00:00 2001 From: Eder Bastos Date: Mon, 18 May 2015 20:22:01 -0400 Subject: [PATCH 1/2] Android: Allow building of native libraries inside Android Studio / Gradle --- Readme.md | 22 ++++++++------ Source/Android/.gitignore | 3 +- Source/Android/app/build.gradle | 54 +++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 10 deletions(-) diff --git a/Readme.md b/Readme.md index 31cea22eff..f1f6e4d431 100644 --- a/Readme.md +++ b/Readme.md @@ -56,18 +56,22 @@ Gradle task `assembleDebug` to build, or `installDebug` to install the UI onto a In order to launch the app, you must build and include the native Dolphin libraries into the UI project. Building native code requires the [Android NDK](https://developer.android.com/tools/sdk/ndk/index.html). +Android Studio will do this for you if you create `Source/Android/build.properties`, and place the +following inside: -### Build Steps: -1. `mkdir Build-Android-` -2. `cd Build-Android-` -3. `cmake -DANDROID=True -DANDROID_NDK= -DANDROID_NATIVE_API_LEVEL=android-18 -DANDROID_TOOLCHAIN_NAME= -DANDROID_ABI= -DCMAKE_TOOLCHAIN_FILE=../Source/Android/android.toolchain.cmake -DGIT_EXECUTABLE= ..` -4. `make` +``` +gitPath= +ndkPath= +toolchain= +abi= +makeArgs= +``` -Replace `` with the absolute path to your machine's Git executable, with the absolute -path to where you installed your NDK, and the rest depending on which platform the Android device you are -targeting uses: +Replace `` with the absolute path to your machine's Git executable, `` with the absolute +path to where you installed your NDK, `` with any arguments you want to pass to `make`, and the +rest depending on which platform the Android device you are targeting uses: -|Platform | abi | toolchain | +|Platform | `` | `` | |-------------------------|-------------|---------------------------| |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 | diff --git a/Source/Android/.gitignore b/Source/Android/.gitignore index 78ddc4f68d..a053548512 100644 --- a/Source/Android/.gitignore +++ b/Source/Android/.gitignore @@ -39,4 +39,5 @@ tasks.xml gradle/ build/ *.so -*.iml \ No newline at end of file +*.iml +build.properties \ No newline at end of file diff --git a/Source/Android/app/build.gradle b/Source/Android/app/build.gradle index ae34d82377..50ec4b23e0 100644 --- a/Source/Android/app/build.gradle +++ b/Source/Android/app/build.gradle @@ -41,6 +41,10 @@ android { applicationIdSuffix ".debug" versionNameSuffix '-debug' jniDebuggable true + + tasks.withType(JavaCompile) { + compileTask -> compileTask.dependsOn(compileNative) + } } } } @@ -58,3 +62,53 @@ dependencies { // For loading huge screenshots from the disk. compile 'com.squareup.picasso:picasso:2.5.2' } + +task setupCMake(type: Exec) { + // Check if a build properties file exists. + def propsFile = rootProject.file("build.properties") + + // If it does, call CMake. + if (propsFile.canRead()) { + // Read the properties file's contents. + def buildProperties = new Properties() + buildProperties.load(new FileInputStream(propsFile)) + + mkdir('build/' + buildProperties.abi) + workingDir 'build/' + buildProperties.abi + + executable 'cmake' + + args "-DANDROID=true", + "-DANDROID_NATIVE_API_LEVEL=android-18", + "-DCMAKE_TOOLCHAIN_FILE=../../../android.toolchain.cmake", + "../../../../..", + "-DGIT_EXECUTABLE=" + buildProperties.gitPath, + "-DANDROID_NDK=" + buildProperties.ndkPath, + "-DANDROID_TOOLCHAIN_NAME=" + buildProperties.toolchain, + "-DANDROID_ABI=" + buildProperties.abi + } else { + executable 'echo' + args 'No build.properties found; skipping CMake.' + } +} + +task compileNative(type: Exec, dependsOn: 'setupCMake') { + // Check if a build properties file exists. + def propsFile = rootProject.file("build.properties") + + // If it does, call make. + if (propsFile.canRead()) { + // Read the properties file's contents. + def buildProperties = new Properties() + buildProperties.load(new FileInputStream(propsFile)) + + workingDir 'build/' + buildProperties.abi + + executable 'make' + + args buildProperties.makeArgs + } else { + executable 'echo' + args 'No build.properties found; skipping native build.' + } +} \ No newline at end of file From 4cded6532061385dab2e5d5d373ce5391a8274fe Mon Sep 17 00:00:00 2001 From: Eder Bastos Date: Tue, 19 May 2015 09:05:35 -0400 Subject: [PATCH 2/2] Android: No longer require specification of NDK or Git paths in build.gradle. --- Readme.md | 9 +++---- Source/Android/app/build.gradle | 46 +++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/Readme.md b/Readme.md index f1f6e4d431..040c50dbff 100644 --- a/Readme.md +++ b/Readme.md @@ -55,21 +55,18 @@ the Android UI. Import the Gradle project located in `./Source/Android`, and the Gradle task `assembleDebug` to build, or `installDebug` to install the UI onto a connected device. In order to launch the app, you must build and include the native Dolphin libraries into the UI project. -Building native code requires the [Android NDK](https://developer.android.com/tools/sdk/ndk/index.html). +(Building native code requires the [Android NDK](https://developer.android.com/tools/sdk/ndk/index.html).) Android Studio will do this for you if you create `Source/Android/build.properties`, and place the following inside: ``` -gitPath= -ndkPath= toolchain= abi= makeArgs= ``` -Replace `` with the absolute path to your machine's Git executable, `` with the absolute -path to where you installed your NDK, `` with any arguments you want to pass to `make`, and the -rest depending on which platform the Android device you are targeting uses: +Replace `` with any arguments you want to pass to `make`, and the rest depending on which +platform the Android device you are targeting uses: |Platform | `` | `` | |-------------------------|-------------|---------------------------| diff --git a/Source/Android/app/build.gradle b/Source/Android/app/build.gradle index 50ec4b23e0..8ccc80598f 100644 --- a/Source/Android/app/build.gradle +++ b/Source/Android/app/build.gradle @@ -82,8 +82,8 @@ task setupCMake(type: Exec) { "-DANDROID_NATIVE_API_LEVEL=android-18", "-DCMAKE_TOOLCHAIN_FILE=../../../android.toolchain.cmake", "../../../../..", - "-DGIT_EXECUTABLE=" + buildProperties.gitPath, - "-DANDROID_NDK=" + buildProperties.ndkPath, + "-DGIT_EXECUTABLE=" + getGitPath(), + "-DANDROID_NDK=" + getNdkPath(), "-DANDROID_TOOLCHAIN_NAME=" + buildProperties.toolchain, "-DANDROID_ABI=" + buildProperties.abi } else { @@ -111,4 +111,46 @@ task compileNative(type: Exec, dependsOn: 'setupCMake') { executable 'echo' args 'No build.properties found; skipping native build.' } +} + +String getGitPath() { + try { + def stdout = new ByteArrayOutputStream() + + exec { + commandLine 'which', 'git' + standardOutput = stdout + } + + def gitPath = stdout.toString().trim() + project.logger.quiet("Gradle: Found git executuable:" + gitPath) + + return gitPath + } catch (ignored) { + // Shouldn't happen. How did the user get this file without git? + project.logger.error("Gradle error: Couldn't find git executable.") + return null; + } +} + +String getNdkPath() { + try { + def stdout = new ByteArrayOutputStream() + + exec { + // ndk-build.cmd is a file unique to the root directory of android-ndk-r10d. + commandLine 'locate', 'ndk-build.cmd' + standardOutput = stdout + } + + def ndkCmdPath = stdout.toString() + def ndkPath = ndkCmdPath.substring(0, ndkCmdPath.lastIndexOf('/')) + + project.logger.quiet("Gradle: Found Android NDK:" + ndkPath) + + return ndkPath + } catch (ignored) { + project.logger.error("Gradle error: Couldn't find NDK.") + return null; + } } \ No newline at end of file