JNI and Gradle in Android Studio - android

I'm trying to add native code to my app. I have everything in ../main/jni as it was in my Eclipse project. I have added ndk.dir=... to my local.properties. I haven't done anything else yet (I'm not sure what else is actually required, so if I've missed something let me know). When I try and build I get this error:
Execution failed for task ':app:compileDebugNdk'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
/Users/me/android-ndk-r8e/ndk-build NDK_PROJECT_PATH=null
APP_BUILD_SCRIPT=/Users/me/Project/app/build/ndk/debug/Android.mk APP_PLATFORM=android-19
NDK_OUT=/Users/me/Project/app/build/ndk/debug/obj
NDK_LIBS_OUT=/Users/me/Project/app/build/ndk/debug/lib APP_ABI=all
Error Code:
2
Output:
make: *** No rule to make target `/Users/me/Project/webapp/build/ndk/debug//Users/me/Project/app/src/main/jni/jni_part.cpp',
needed by `/Users/me/Project/app/build/ndk/debug/obj/local/armeabi-v7a/objs/webapp//Users/me/Project/app/src/main/jni/jni_part.o'.
Stop.
What do I need to do?
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# OpenCV
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
include .../OpenCV-2.4.5-android-sdk/sdk/native/jni/OpenCV.mk
LOCAL_MODULE := native_part
LOCAL_SRC_FILES := jni_part.cpp
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
Application.mk:
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi armeabi-v7a
APP_PLATFORM := android-8

Gradle Build Tools 2.2.0+ - The closest the NDK has ever come to being called 'magic'
In trying to avoid experimental and frankly fed up with the NDK and all its hackery I am happy that 2.2.x of the Gradle Build Tools came out and now it just works. The key is the externalNativeBuild and pointing ndkBuild path argument at an Android.mk or change ndkBuild to cmake and point the path argument at a CMakeLists.txt build script.
android {
compileSdkVersion 19
buildToolsVersion "25.0.2"
defaultConfig {
minSdkVersion 19
targetSdkVersion 19
ndk {
abiFilters 'armeabi', 'armeabi-v7a', 'x86'
}
externalNativeBuild {
cmake {
cppFlags '-std=c++11'
arguments '-DANDROID_TOOLCHAIN=clang',
'-DANDROID_PLATFORM=android-19',
'-DANDROID_STL=gnustl_static',
'-DANDROID_ARM_NEON=TRUE',
'-DANDROID_CPP_FEATURES=exceptions rtti'
}
}
}
externalNativeBuild {
cmake {
path 'src/main/jni/CMakeLists.txt'
}
//ndkBuild {
// path 'src/main/jni/Android.mk'
//}
}
}
For much more detail check Google's page on adding native code.
After this is setup correctly you can ./gradlew installDebug and off you go. You will also need to be aware that the NDK is moving to clang since gcc is now deprecated in the Android NDK.
Android Studio Clean and Build Integration - DEPRECATED
The other answers do point out the correct way to prevent the automatic creation of Android.mk files, but they fail to go the extra step of integrating better with Android Studio. I have added the ability to actually clean and build from source without needing to go to the command-line. Your local.properties file will need to have ndk.dir=/path/to/ndk
apply plugin: 'com.android.application'
android {
compileSdkVersion 14
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.example.application"
minSdkVersion 14
targetSdkVersion 14
ndk {
moduleName "YourModuleName"
}
}
sourceSets.main {
jni.srcDirs = [] // This prevents the auto generation of Android.mk
jniLibs.srcDir 'src/main/libs' // This is not necessary unless you have precompiled libraries in your project.
}
task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
def ndkDir = android.ndkDirectory
commandLine "$ndkDir/ndk-build",
'-C', file('src/main/jni').absolutePath, // Change src/main/jni the relative path to your jni source
'-j', Runtime.runtime.availableProcessors(),
'all',
'NDK_DEBUG=1'
}
task cleanNative(type: Exec, description: 'Clean JNI object files') {
def ndkDir = android.ndkDirectory
commandLine "$ndkDir/ndk-build",
'-C', file('src/main/jni').absolutePath, // Change src/main/jni the relative path to your jni source
'clean'
}
clean.dependsOn 'cleanNative'
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn buildNative
}
}
dependencies {
compile 'com.android.support:support-v4:20.0.0'
}
The src/main/jni directory assumes a standard layout of the project. It should be the relative from this build.gradle file location to the jni directory.
Gradle - for those having issues
Also check this Stack Overflow answer.
It is really important that your gradle version and general setup are correct. If you have an older project I highly recommend creating a new one with the latest Android Studio and see what Google considers the standard project. Also, use gradlew. This protects the developer from a gradle version mismatch. Finally, the gradle plugin must be configured correctly.
And you ask what is the latest version of the gradle plugin? Check the tools page and edit the version accordingly.
Final product - /build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
// Running 'gradle wrapper' will generate gradlew - Getting gradle wrapper working and using it will save you a lot of pain.
task wrapper(type: Wrapper) {
gradleVersion = '2.2'
}
// Look Google doesn't use Maven Central, they use jcenter now.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
Make sure gradle wrapper generates the gradlew file and gradle/wrapper subdirectory. This is a big gotcha.
ndkDirectory
This has come up a number of times, but android.ndkDirectory is the correct way to get the folder after 1.1. Migrating Gradle Projects to version 1.0.0. If you're using an experimental or ancient version of the plugin your mileage may vary.

gradle supports ndk compilation by generating another Android.mk file with absolute paths to your sources.
NDK supports absolute paths since r9 on OSX, r9c on Windows, so you need to upgrade your NDK to r9+.
You may run into other troubles as NDK support by gradle is preliminary. If so you can deactivate the ndk compilation from gradle by setting:
sourceSets.main {
jni.srcDirs = []
jniLibs.srcDir 'src/main/libs'
}
to be able to call ndk-build yourself and integrate libs from libs/.
btw, you have any issue compiling for x86 ? I see you haven't included it in your APP_ABI.

In my case, I'm on Windows and following the answer by Cameron above only works if you use the full name of the ndk-build which is ndk-build.cmd. I have to clean and rebuild the project, then restart the emulator before getting the app to work (Actually I imported the sample HelloJni from NDK, into Android Studio). However, make sure the path to NDK does not contain space.
Finally, my build.gradle is full listed as below:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.hellojni"
minSdkVersion 4
targetSdkVersion 4
ndk {
moduleName "hello-jni"
}
testApplicationId "com.example.hellojni.tests"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
sourceSets.main {
jni.srcDirs = [] // This prevents the auto generation of Android.mk
// sourceSets.main.jni.srcDirs = []
jniLibs.srcDir 'src/main/libs' // This is not necessary unless you have precompiled libraries in your project.
}
task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
def ndkDir = android.plugin.ndkFolder
commandLine "$ndkDir/ndk-build.cmd",
'-C', file('src/main/jni').absolutePath, // Change src/main/jni the relative path to your jni source
'-j', Runtime.runtime.availableProcessors(),
'all',
'NDK_DEBUG=1'
}
task cleanNative(type: Exec, description: 'Clean JNI object files') {
def ndkDir = android.plugin.ndkFolder
commandLine "$ndkDir/ndk-build.cmd",
'-C', file('src/main/jni').absolutePath, // Change src/main/jni the relative path to your jni source
'clean'
}
clean.dependsOn 'cleanNative'
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn buildNative
}
}
dependencies {
compile 'com.android.support:support-v4:21.0.3'
}

Android Studio 2.2 came out with the ability to use ndk-build and cMake. Though, we had to wait til 2.2.3 for the Application.mk support. I've tried it, it works...though, my variables aren't showing up in the debugger. I can still query them via command line though.
You need to do something like this:
externalNativeBuild{
ndkBuild{
path "Android.mk"
}
}
defaultConfig {
externalNativeBuild{
ndkBuild {
arguments "NDK_APPLICATION_MK:=Application.mk"
cFlags "-DTEST_C_FLAG1" "-DTEST_C_FLAG2"
cppFlags "-DTEST_CPP_FLAG2" "-DTEST_CPP_FLAG2"
abiFilters "armeabi-v7a", "armeabi"
}
}
}
See http://tools.android.com/tech-docs/external-c-builds
NB: The extra nesting of externalNativeBuild inside defaultConfig was a breaking change introduced with Android Studio 2.2 Preview 5 (July 8, 2016). See the release notes at the above link.

My issue on OSX it was gradle version. Gradle was ignoring my Android.mk.
So, in order to override this option, and use my make instead, I have entered this line:
sourceSets.main.jni.srcDirs = []
inside of the android tag in build.gradle.
I have wasted lot of time on this!

In the module build.gradle, in the task field, I get an error unless I use:
def ndkDir = plugins.getPlugin('com.android.application').sdkHandler.getNdkFolder()
I see people using
def ndkDir = android.plugin.ndkFolder
and
def ndkDir = plugins.getPlugin('com.android.library').sdkHandler.getNdkFolder()
but neither of those worked until I changed it to the plugin I was actually importing.

Related

Android NDK ndkbuildFailed

I tried to build android app with JNI and NDK by using tess-two but i keep getting this error. i'm using Android Studo 2.0, i've installed android ndk r11c. gradle build successfully but keep getting failed to build APK.
this is my android.mk file :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
IMAGE_PROCESSING_PATH := $(LOCAL_PATH)/../../../../Test/src
IMAGE_PROCESSING_JNI_PATH := $(LOCAL_PATH)/image_processing
TESS_TWO_PATH := $(LOCAL_PATH)/../../../../tess-two/tess-two
LEPTONICA_SRC_PATH := $(TESS_TWO_PATH)/jni/com_googlecode_leptonica_android/src
include $(IMAGE_PROCESSING_JNI_PATH)/Android.mk
This is the error message :
http://i.stack.imgur.com/yZATt.png
After taking a look onto your gradle build log:
Source cannot be found. You need to define the NDK_PROJECT_PATH to let it shop to the root folder of your project path.
As far as I know you can specify this in your gradle.build:
android {
sourceSets.main.jni.srcDirs = "src"
}
You can try to disable automatic ndk-build.
For this, update build.graddle file
android {
// ..... defaultConfig / buildTypes / etc ...
// SPECIFIC ROUTINE for NATIVE BUILD
sourceSets.main {
jniLibs.srcDir 'src/main/libs' //set .so files location to libs
jni.srcDirs = [] //disable automatic ndk-build call
}
// call regular ndk-build(.cmd) script from app directory
task ndkBuild(type: Exec) {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'ndk-build.cmd', 'NDK_DEBUG=0', '-C', file('src/main/jni').absolutePath
} else {
commandLine 'ndk-build', 'NDK_DEBUG=0', '-C', file('src/main/jni').absolutePath
}
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
}
You may need to update graddle.properties
android.useDeprecatedNdk=true
I solved this by giving it a full path :
task ndkBuild(type: Exec,description: 'run ndk-build') {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'D:\\Application\\android-ndk-r10e\\ndk-build.cmd', '-C', 'D:\\ransel\\Citeks\\app\\src\\main\\jni'
} else {
workingDir 'src/main/jni'
commandLine 'D:\\Application\\android-ndk-r10e\\ndk-build', '-C', 'D:\\ransel\\Citeks\\app\\src\\main\\jni'
}
}
Thank you :)

Android Studio, gradle, ndk, aar and the inclusion of several native shared libraries

I pretty much read through almost every question in stackoverflow about the subject.
I browsed documentation and work-notes of other people who are using AS + NDK + gradle to build an aar that would be included by other app.
I was able to build the .so in a different multi-project setup where the structure was different from the one shown in one aspect: it didn't have the first jni/ layer.
I added that extra jni layer so that I'd have a sharedObjectLib#2/ hierarchy. In that jni/ dir, all i have is a single Android.mk whose sole purpose is to include $(call all-subdir-makefiles). After I did that, gradle build reports the NDK failure:
"Error:(89) Android NDK: WARNING: There are no modules to build in this project!"
What I can't seem to be able to do is build multiple shared objects '.so' as part of the aar.
I would really like to know if (a) it is doable; and (b) some pointers to links and/or examples of gradle.build files that actually do that.
Here is the structure I currently have - skipping the usual directories created by Android Studio (v. 1.2.2, btw).
--rootProject/
--build.gradle
--gradle.properties
--local.properties
--settings.gradle
--rootProject.iml
--app/
--moduleProjectThatBuildsAAR/
--build.gradle
--build/
--libs
--src/
--main/
--res/
--AndroidManifest.xml
--jni/
--Android.mk (does include $(call all-subdir-makefiles))
--Application.mk
--sharedObjectLib#1/
--build.gradle
--src/
-- androidTest/
-- main/
--java/
--jni/
-- Android.mk
-- Application.mk
-- *.c and *.h files
--libs/
--obj/
-- build.gradle
It's pretty convoluted and I am hoping the experts would help with simplification.
thanks!
I am using 1.2.2 and found it easy to build the simple NDK projects
from the many tutorials floating around, but frustratingly difficult
to build an NDK project of any complexity. I will summarize what I
found, but I highly suggest reading
this blog
and this StackOverflow.
I found that Android Studio would completely ignore the
Android.mk file I created, and instead auto-generate its own.
To correct this, I had to first hack the build.gradle
script for my project, located at project/app/build.gradle.
You could probably hack the top-level build.gradle, if desired.
This may be what is happening in your case. The auto-generated
Android.mk looks in jni/ for .c source files and finds nothing
there.
This is my build.gradle. I build on a Windows box, so I hacked it for
Windows only. Uncomment the lines if you are using OSX or Linux.
project/app/build.gradle:
//import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.sample.app"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
//ENABLE CUSTOM ANDROID.MK >>
sourceSets.main.jni.srcDirs= [] //Disable automatic ndk-build.
sourceSets.main.jniLibs.srcDir 'src/main/libs'
//Call regular ndk-build script from app directory
task ndkBuild(type: Exec) {
workingDir file('src/main')
commandLine getNdkBuildCmd()
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn { ndkBuild }
}
task cleanNative(type: Exec) {
workingDir file('src/main')
commandLine getNdkBuildCmd(), 'clean'
}
clean.dependsOn cleanNative
}
//ENABLE CUSTOM ANDROID.MK <<
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.google.android.gms:play-services:7.5.0'
}
//ENABLE CUSTOM ANDROID.MK >>
def getNdkDir() {
if (System.env.ANDROID_NDK_ROOT != null)
return System.env.ANDROID_NDK_ROOT
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def ndkdir = properties.getProperty('ndk.dir', null)
if (ndkdir == null)
throw new GradleException("NDK location not found. Define location with ndk.dir in the local.properties file")
return (ndkdir)
}
def getNdkBuildCmd() {
def ndkbuild = getNdkDir() + "/ndk-build.cmd"
// def ndkbuild = getNdkDir() + "/ndk-build"
// if (Os.isFamily(Os.FAMILY_WINDOWS))
// ndkbuild += ".cmd"
return ndkbuild
}
//ENABLE CUSTOM ANDROID.MK <<

how to specify NDK_TOOLCHAIN_VERSION in gradle file for android ndk build

I am moving my Android project that uses ndk-build to use the gradle build system as described in the examples of the new build tools for android. In this link http://tools.android.com/tech-docs/new-build-system. I am looked at the gradle-samples-0.11 at the bottom of the page for inspiration.
So I managed to configure all of the pieces I need by including the following code in my build.gradle default config section.
ndk {
moduleName "MyModuleName"
ldLibs "log"
cFlags "-std=c++11 -fexceptions"
stl "gnustl_static"
}
I have this line in my Application.mk file in the original project:
NDK_TOOLCHAIN_VERSION := 4.9
It is the last piece I can't configure.
I am using NDK Revision 10. I need this NDK_TOOLCHAIN_VERSION:=4.9 because my build is reporting Error:(25, 11) error: expected nested-name-specifier before 'Integer' and the c++ code at that line looks like this.
using Integer = int16_t;
Does anyone have an idea on how I can solve this, please?
I've been trying to solve this too. But in the ended up by writing custom tasks to make Android Studio use Application.mk and Android.mk just like in eclipse.
My build.gradle looks like this
apply plugin: 'com.android.application'
android {
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
minSdkVersion 15
targetSdkVersion 20
versionCode 1
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
sourceSets.main {
jniLibs.srcDir 'src/main/libs'
jni.srcDirs = [] //disable automatic ndk-build call
}
}
task buildNative(type: Exec) {
def ndkBuild = null;
def ndkBuildingDir = new File("src/main/jni");
def hasNdk = false;
if (System.getenv("NDK_BUILD_CMD") != null) {
hasNdk = true;
ndkBuild = new File(System.getenv("NDK_BUILD_CMD"))
}
commandLine ndkBuild, "--directory", ndkBuildingDir
doFirst {
if (!hasNdk) {
logger.error('##################')
logger.error("NDK build failed!!")
logger.error('Reason: NDK_BUILD_CMD not set.')
logger.error('##################')
}
assert hasNdk: "NDK_BUILD_CMD not set."
}
}
tasks.withType(JavaCompile) { compileTask -> compileTask.dependsOn buildNative }
task cleanNative(type: Exec) {
def ndkBuild = null;
def ndkBuildingDir = new File("src/main/jni");
def hasNdk = false;
if (System.getenv("NDK_BUILD_CMD") != null) {
hasNdk = true;
ndkBuild = new File(System.getenv("NDK_BUILD_CMD"))
}
commandLine ndkBuild, "--directory", ndkBuildingDir, "clean"
doFirst {
if (!hasNdk) {
logger.error('##################')
logger.error("NDK build failed!!")
logger.error('Reason: NDK_BUILD_CMD not set.')
logger.error('##################')
}
assert hasNdk: "NDK_BUILD_CMD not set."
}
}
clean.dependsOn 'cleanNative'
For this to work, you need to set an environment variable NDK_BUILD_CMD to be set the the exact ndk-build executable.
On Windows, you can just set an environment variable NDK_BUILD_CMD point to your ndk-build.exe
On Mac,the path variables you set in your .bash_profile is not accessible on GUI applications(hence Android Studio will not be able to read them).
So edit your .bash_profile to be something like
GRADLE_HOME={path_to_gradle}
ANDROID_SDK_ROOT={path_to_sdk_dir}
ANDROID_HOME=$ANDROID_SDK_ROOT/platform-tools
ANDROID_NDK_HOME={path_to_ndk}
NDK_BUILD_CMD=$ANDROID_NDK_HOME/ndk-build
export PATH=$GRADLE_HOME/bin:$ANDROID_HOME:$ANDROID_SDK_ROOT/tools:$ANDROID_NDK_HOME:/opt/local/bin:/opt/local/sbin:$PATH
launchctl setenv GRADLE_HOME $GRADLE_HOME
launchctl setenv ANDROID_HOME $ANDROID_HOME
launchctl setenv ANDROID_NDK_HOME $ANDROID_NDK_HOME
launchctl setenv NDK_BUILD_CMD $NDK_BUILD_CMD
The launchctl lines will make your environment variables visible inside your Android Studio.
PS: The .bash_profile is run every time you open the terminal. So for this to work properly with Android Studio, you need to launch the terminal once and then run Android Studio. Otherwise the build will fail saying NDK_BUILD_CMD not set. I haven't found any way to set the values when Mac starts. If someone else can find a way, please feel free to suggest.
I've just had this problem, but my solution was actually in your initial question. The latest version of the NDK uses GCC 4.9 by default (https://developer.android.com/tools/sdk/ndk/index.html). This is only default on a 64bit machine it would seem. So you no longer need to set the NDK_TOOLCHAIN_VERSION property in your build.gradle file. So simply setting:
cFlags "-std=c++11 -fexceptions"
stl "gnustl_static"
in ndk{} Should be enough. Works for me.
although this question was asked a long time ago,
the proper solution as described in (https://developer.android.com/studio/projects/install-ndk):
If you install a specific version of the NDK and want to use it in a module, you specify it using the android.ndkVersion property in the module's build.gradle file, as shown in the following code sample.
android {
ndkVersion "major.minor.build"
}
In the latest version of Android Studio 1.5 and Gradle 2.10 with Experimental Plugins 0.7.0-alpha4, you can specify like this example it uses Clang toolchain with version 3.6..
ndk {
moduleName "MyModuleName"
ldLibs "log"
cFlags "-std=c++11 -fexceptions"
stl "gnustl_static"
toolchain "clang"
toolchainVersion = "3.6"
}
You can refer your NDK directory to know which toolchain and which versions are available.
For NDK r10e, GCC (4.8, 4.9) and Clang (3.5, 3.6) options are available. If you don't specify toolchain it will by default use GCC 4.9.
Refer LINK to know more about app gradle configurations.

Android studio - AndEngine ndk compilation error

I recently moved from Eclipse to Android Studio
And I keep getting compilation errors:
I get this error:
Error:Execution failed for task ':andEngine:compileReleaseNdk'.
com.android.ide.common.internal.LoggedErrorException: Failed to run command:
C:\Program Files\Android\android-ndk-r10\ndk-build.cmd NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=C:\Users\myUser\AndroidstudioProjects\MyApllicationName\andEngine\build\intermediates\ndk\release\Android.mk APP_PLATFORM=android-14 NDK_OUT=C:\Users\myUser\AndroidstudioProjects\MyApllicationName\andEngine\build\intermediates\ndk\release\obj NDK_LIBS_OUT=C:\Users\myUser\AndroidstudioProjects\MyApllicationName\andEngine\build\intermediates\ndk\release\lib APP_ABI=all
Error Code:
1
I set up in "local.propeties":
sdk.dir=C\:\\Program Files (x86)\\Android\\android-studio\\sdk
ndk.dir=C\:\\Program Files\\Android\\android-ndk-r10
I download the recent NDK and put it in
C\:\\Program Files\\Android\\android-ndk-r10
allso I added it to the envirenment variables under
"ANDROID_NDK" and "NDK_HOME"
What am I doing wrong?
How can I compile AndEngine using android-studio?
With Android Studio, NDK support is preliminary and your *.mk files are ignored.
To solve the issue you're encountering, you should post more information regarding your project structure and the content of your build.gradle files.
Otherwise you can make it work by setting Android Studio/gradle so it reuses your *.mk files by deactivating the default NDK integration, making it call ndk-build(.cmd) by itself, and using standard libs/ location for .so files integration, like so:
import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
defaultConfig{
minSdkVersion 15
targetSdkVersion 19
versionCode 101
versionName "1.0.1"
}
sourceSets.main {
jniLibs.srcDir 'src/main/libs'
jni.srcDirs = [] //disable automatic ndk-build call
}
// call regular ndk-build(.cmd) script from app directory
task ndkBuild(type: Exec) {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
} else {
commandLine 'ndk-build', '-C', file('src/main').absolutePath
}
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
}
For our setup, spaces in NDK path were the issue. Moving NDK to path without spaces solved the problem for us.
We got the same error with error code 1, so I hope it will help someone with the same problem.
Our setup:
Gradle 2.1
Gradle Android plugin 0.13.2
Ndk r10b

Can't build project with android-ndk and Android Studio

I have an android project with FFmpeg and other external libraries. I downloaded the latest version of the ndk (ndk-r10) and am running Android Studio 0.8.0. I am also running Windows 8 64bit with the latest version of cygwin.
My project builds without issue and I added the ndk.dir to local.properties. When I try to run I get this error message:
The System cannot find the path specified
Error:Execution failed for task ':app:compileDebugNdk'.
com.android.ide.common.internal.LoggedErrorException: Failed to run command:
C:\Users\John1\AppData\Local\Android\android-ndk-r10\ndk-build.cmd
NDK_PROJECT_PATH=null
APP_BUILD_SCRIPT=C:\Users\John1\AndroidstudioProjects\android-project\app\build\intermediates\ndk\debug\Android.mk
APP_PLATFORM=android-18
NDK_OUT=C:\Users\John1\AndroidstudioProjects\android-project\app\build\intermediates\ndk\debug\obj
NDK_LIBS_OUT=C:\Users\John1\AndroidstudioProjects\android-project\app\build\intermediates\ndk\debug\lib
APP_ABI=all
Error Code:
1
Output:
The system cannot find the path specified.
Looking for advice. Thank you.
with Android Studio, NDK support is preliminary and your *.mk files are ignored.
You can make Android Studio/gradle reuse them by deactivating the default NDK integration, make it call ndk-build(.cmd) by itself, and use standard libs/ location for integrating .so files:
import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
defaultConfig{
minSdkVersion 15
targetSdkVersion 19
versionCode 101
versionName "1.0.1"
}
sourceSets.main {
jniLibs.srcDir 'src/main/libs'
jni.srcDirs = [] //disable automatic ndk-build call
}
// call regular ndk-build(.cmd) script from app directory
task ndkBuild(type: Exec) {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
} else {
commandLine 'ndk-build', '-C', file('src/main').absolutePath
}
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
}
If you need more information, here is my blog post on this topic: http://ph0b.com/android-studio-gradle-and-ndk-integration/

Categories

Resources