build cmake ExternalProject with gradle

2019-06-13 05:30发布

问题:

I am trying to use an existing native library as part of an android project. I followed instructions about adding C and C++ Code to a project for android studio. I got to the point where I have a functioning CMakeLists.txt file (tested independently) and I've instructed gradle to use it to compile the native code.

The library I'm using uses make so I use ExternalProject_Add to instruct cmake how to compile it. Here is the relevant code:

ExternalProject_Add(foo
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../libfoo
    CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../libfoo/configure
        --host=arm-linux-androideabi
    BUILD_COMMAND ${MAKE}
    INSTALL_COMMAND true)

This is the relevant part of the build.gradle for the module:

externalNativeBuild {
    cmake {
        path '../cpp/CMakeLists.txt'
    }
}

When I run ./gradlew assembleDebug, the external project is not compiled. After adding an executable (named bar) that depends on foo in cmake, the configure step runs but the build step doesn't. I modified the code of the library so that compilation would fail, but gradle returns success anyway).

Here are the last lines of the output of ./gradlew assembleDebug after these changes.

configure: creating ./config.status
config.status: creating Makefile
config.status: executing depfiles commands
[6/10] Performing build step for 'foo'
[7/10] Performing install step for 'foo'
[8/10] Completed 'foo'
[9/10] Building C object CMakeFiles/bar.dir/bar.c.o
[10/10] Linking C executable bar

BUILD SUCCESSFUL in 18s

Any ideas what is happening? What am I doing wrong?

I'm using gradle 4.1 and cmake 3.6.

回答1:

you should use BUILD_IN_SOURCE ON otherwise your BINARY_DIR is somewhere else so make do nothing IMHO.



回答2:

See https://stackoverflow.com/a/47636956/192373. You don't need fictional executables. You do need two separate targets, though.

add_library(foolib SHARED IMPORTED)
add_dependencies(foolib foo)
set_target_properties(foolib IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../libfoo/…/libfoo.so)

You don't necessarily need BUILD_IN_SOURCE ON, better set BINARY_DIR somewhere in build/intermediates, and pass the same to make.



标签: gradle cmake