Android Studio failed build NDK project non-zero e

2019-01-17 23:17发布

问题:

I imported a project that I was working on from an Eclipse Android SDK environment to the new Android Studio. Trying to run the project I get this

Error:Execution failed for task ':app:compileDebugNdk'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'ndk/ndk-build'' finished with non-zero exit value 2

So first of all for me it's weird to see this error, because working in Eclipse Android SDK before, I ran ndk-build by myself which I still do. So is Android Studio running ndk-build for me now? Because when I run it in Terminal I have no problem at all and here it tells me bad exit-value.

And then I searched on the internet and found a "quick fix", which is to create an empty .c file, because you can't build/run ndk project with no .c files. This, not surprisingly, does not work for me because we have plenty of .c files in the .jni folder.

Just to mention I'm on a Macbook Pro under Yosemite, and the project should have no problems in itself.

Thanks in advance for any help.

回答1:

This is known problem of Android Studio, caused by very limited support of NDK-enabled projects. Answering to your question: yes, Android Studio now run ndk-build on its own, but it ignore existing Android.mk and generate one on the fly (and do it wrong for any non-trivial NDK-enabled project). Practically, the best way to fix this is to disable Android Studio's limited NDK support and call ndk-build from gradle script. Here I've described it more detailed and how to get it fixed.



回答2:

For simple, you can try to insert sourceSets.main.jni.srcDirs = [] to android block in build.gradle file. This will tell gradle to look for your C++ files in different path.



回答3:

You can disable compileDebugNdk, but still have the jni folder visible in Android Studio. In your build.gradle, add the following clause:

tasks.all { task ->
    if (task.name.startsWith('compile') && task.name.endsWith('Ndk')) {
        task.enabled = false
    }
}


回答4:

To see what is the exact problem do these:
1- Make sure you have set ndk path correctly, for example in my project I put ndk.dir=/usr/local/opt/android-ndk-r13b in local.properties file
2- Go to app directory of your android app
3- Run this command: <ndk_build_path> -C <jni_path> NDK_OUT=<jniLibDir> all NDK_DEBUG=1 example :

/usr/local/opt/android-ndk-r13b/ndk-build -C /MYAPP/app/src/main/jni -j 4 NDK_OUT=../jniLibs all NDK_DEBUG=1

then you can see what's going wrong.



回答5:

Encountered same error while importing a project from windows to mac, I tried all the above solutions, did not work. Finally noticed that there were "multiple target patterns" which was causing this error.

Solution : Delete the "obj" folder within your NDK and rebuild.

Done !!



回答6:

I almost tried every solution available. I use Windows 10, Android Studio 1.5.1 and the only thing that worked, is the solution here (http://answers.opencv.org/question/58551/androidstudiondk-finished-with-non-zero-exit-value2/)

In the Apps>build.gradle, add this under the android object.

android{
...
sourceSets { 
 main { 
      res.srcDirs = ['src/main/res'] 
      jniLibs.srcDirs = ['src/main/jniLibs'] 
      jni.srcDirs = [] // This prevents the auto generation of Android.mk 
 } 
} 

In this version of Android Studio, adding the NDK path in File>Project Structure>NDK Path automatically adds it to local.properties file, you don't edit anything else.