Change ndk-build output locations

If you need to build for all available architectures, please use argument:

    TARGET_OUT='<your_build_dir>/$(TARGET_ARCH_ABI)'

If just one target architecture is enough, then use argument:

    TARGET_OUT=<your_build_dir>

Edit (for anyone that step on this topic with newer NDK versions):

  • Use NDK_LIBS_OUT to change libs output (requires NDK r9 and above)
  • Use NDK_OUT to change obj output (requires NDK r7c and above)

Build command is is:

ndk-build NDK_LIBS_OUT=./jniLibs NDK_OUT=./obj

Previous answer (applicable in any case):

How I solve it. Not really elegant but at least it works.

Because my building mechanism is quite complicated so I had a couple of custom gradle tasks and scripts too, it doesn't really matter to me the single command for the build.

So I kept build command as is

<my_ndk_path>/ndk-build -C <my_project_path>/Android/app/src/main/

Add a the folder creation (-p don't generate error if it exists)

mkdir -p <my_project_path>/Android/app/src/main/jniLibs/

Then I move the .so files to the final location with the sync command.

rsync -avh --remove-source-files                        \
      <my_project_path>/Android/app/src/main/libs/*     \
      <my_project_path>/Android/app/src/main/jniLibs/

You can do something similar for the obj folder. I just modified my .gitignore so I left it as is.


The better and more simple way to specify out put locations is linking Gradle to your native library. After that, using buildStagingDirectory options in ndkBuild block to specify out put location:

android {
    externalNativeBuild {
        ndkBuild {
            // Tells Gradle to put outputs from external native
            // builds in the path specified below.
            buildStagingDirectory  "src/main/libs"
        }
    }
}

Then just Sync Gradle and build, done.


You can use NDK_LIBS_OUT to change libs output and NDK_OUT to change obj output. Just like this : ndk-build NDK_LIBS_OUT=./jniLibs NDK_OUT=./obj