androidandroid-gradle-pluginndk-build

Change ndk-build output locations


My app has the following structure

Android
|
|-> app
|    |
|    |-> build [1]
|    |-> src
|    |    |-> main
|    |    |     |-> assets
|    |    |     |-> java
|    |    |     |-> jni 
|    |    |     |     |-> Android.mk
|    |    |     |     |-> Application.mk
|    |    |     |
|    |    |     |-> jniLibs
|    |    |     |      |-> armeabi
|    |    |     |      |-> armeabi-v7a
|    |    |     |
|    |    |     |-> res
|    |    |     |-> AndroidManifest.xml
|
|-> build [2]

I am building my .so libraries with ndk-build command in a linux machine. I use it like:

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

The build process works fine and output files are produced with no errors.

My problem is that the result files are not "directed to the proper position"

The libs generated at :

.app/src/main/libs/armeabi-v7a/libMyGeneratedLibrary.so
.app/src/main/libs/armeabi/libMyGeneratedLibrary.so

and the obj files at :

.app/src/main/obj/local/armeabi-v7a/libMyGeneratedLibrary.so
.app/src/main/obj/local/armeabi/libMyGeneratedLibrary.so

I would like the output to produced in different locations:

  1. The libs at jniLibs. ie. ./app/src/main/jniLibs/armeabi*
  2. The obj under one of the build folders. ie. build[1] or build[2].

Is there any possible way to achieve that by changing some parameter to the .mk files or to the build command?


Edit

TARGET_OUT does not seem to work in NDK r6b

Executing command ndk-build -C Android/app/src/main/ TARGET_OUT='Android/app/src/main/jniLibs/$(TARGET_ARCH_ABI)'

Warnings as the following appear for each generated file.

android-ndk-r6b/build/core/build-binary.mk:217: warning: overriding commands for target 'Android/app/src/main/jniLibs//objs/***/****.o'
android-ndk-r6b/build/core/build-binary.mk:217: warning: ignoring old commands for target 'Android/app/src/main/jniLibs//objs/***/****.o'

Solution

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

    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.