Search code examples
android-studioandroid-ndkndk-build

Android Studio native lib build error using ndk-build


I would like to build c/c++ code with Android Studio instead of using the ndk-build script manually (up to now, i have always used Android.mk and Application.mk and ndk-build script to build .so library). I am trying to configure the project. In my build.gradle, i have the following lines:

externalNativeBuild {
        ndkBuild {
            path 'src/main/jni/Android.mk'
        }
    }

Under the directory src/main/jni there is the Application.mk file and ALL the C/C++ source code:

jni/
 -jpeg8d-master/   (a directory)
 -other directories..
 -android.mk
 -application.mk
 -common.h
 -other .c/.cpp/.h files

the file common.h refers to the file jpeglib.h under the (local) directory jpeg8d-master/. In the old way (running ndk-build under the jni/ folder), everything worked. When i try to make the project (Ctrl-F9) with Android Studio, on the Build tab i see the following error:

Build command failed.
Error while executing process D:\android\android-ndk-r16b\ndk-build.cmd with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=C:\android\android-studio-workspace\MyAPP\app\src\main\jni\Android.mk NDK_APPLICATION_MK=C:\android\android-studio-workspace\MyApp\app\src\main\jni\Application.mk APP_ABI=x86_64 NDK_ALL_ABIS=x86_64 NDK_DEBUG=1 APP_PLATFORM=android-21 NDK_OUT=C:/android/android-studio-workspace/MyApp/app/build/intermediates/ndkBuild/debug/obj NDK_LIBS_OUT=C:\android\android-studio-workspace\MyApp\app\build\intermediates\ndkBuild\debug\lib C:/android/android-studio-workspace/MyApp/app/build/intermediates/ndkBuild/debug/obj/local/x86_64/libnative.so}
[x86_64] Compile++      : native <= wrapper.cpp
In file included from C:/android/android-studio-workspace/MyApp/app/src/main/jni/wrapper.cpp:29:
C:/android/android-studio-workspace/MyApp/app/src/main/jni/common.h:31:10: warning: non-portable path to file '"eigen/Eigen/Dense"'; specified path differs in case from file name on disk [-Wnonportable-include-path]
#include "eigen/eigen/Dense"
         ^~~~~~~~~~~~~~~~~~~
         "eigen/Eigen/Dense"
C:/android/android-studio-workspace/MyApp/app/src/main/jni/common.h:209:10: fatal error: 'jpeglib.h' file not found
#include        "jpeglib.h"
                ^~~~~~~~~~~
1 warning and 1 error generated.
make: *** [C:/android/android-studio-workspace/MyApp/app/build/intermediates/ndkBuild/debug/obj/local/x86_64/objs/native/wrapper.o] Error 1

Maybe the ndk-build command is not called from within the jni/ directory, or something else is missing. Any help?


Solution

  • You are right, the current directory when Android Studio runs ndk-build is the app directory (where you have the build.gradle file, in your case it's C:/android/android-studio-workspace/MyApp/app).

    You should fix your Android.mk to have all include paths relative to LOCAL_PATH, e.g.

    LOCAL_C_INCLUDES += $(LOCAL_PATH)/eigen
    

    and so on.