[FIXED] Android Gradle avoid CMake project unnecessary rebuild

Issue

I have Android Gradle + CMake (NDK) project with multiple flavors. C++ source files / libraries in subprojects are unnecessarily rebuilt after switching to flavor which was not built previously. Which is kind a expected giving that output folder is flavor specific and doesn’t exist when building for the 1st time:

./app/.externalNativeBuild/cmake/prodDebug/armeabi/libs/mylib/libmylib.a
./app/.externalNativeBuild/cmake/devDebug/armeabi/libs/mylib/libmylib.a

But I have lot of flavors and large libraries to build and don’t want to rebuild C++ libraries which are not depending on flavor – there are libraries which should be same for every flavor when built. I tried to fix this by specifying different output directory, so that it’s not depending on flavor:

add_subdirectory("libs/mylib" "${CMAKE_CURRENT_SOURCE_DIR}/libs/mylib/output")

Every flavor now shares same output directory of mylib library, but this time library is rebuild all the time when switching flavors. When using ordinary CMake without Gradle all works as expected. Is there any way to fix unnecessary rebuilds when compiling from Android Studio / Gradle?

EDIT1:
Btw, rebuilds are done even if flavors don’t specify any C++ flags etc – essentially they are same just different application/version name:

    dev {
        applicationIdSuffix ".dev"
        versionNameSuffix "-dev"
    }
    prod {
        applicationIdSuffix ".prod"
        versionNameSuffix "-prod"
    }

EDIT2: There’s also another rebuild issue. When changing any CMakeLists.txt (also subproject ones) it triggers entire rebuild of tree – all parent projects, subprojects (even unrelated ones) are rebuilt. Currently this is a stopper to using Gradle/Android Studio for building NDK projects.

Solution

I solved it by adding CCache to my CMakeLists.txt:

find_program(CCACHE_FOUND ccache)
if(CCACHE_FOUND)
    set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
    set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
endif(CCACHE_FOUND)

As described here:

https://stackoverflow.com/a/24305849/408877

Answered By – Paul Slocum

Answer Checked By – Cary Denson (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published