title: Android Gradle avoid CMake project unnecessary rebuild
body: <p>I have <em><strong>Android Gradle + CMake (NDK)</strong></em> 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:</p>
  <pre><code>./app/.externalNativeBuild/cmake/prodDebug/armeabi/libs/mylib/libmylib.a
  ./app/.externalNativeBuild/cmake/devDebug/armeabi/libs/mylib/libmylib.a
  </code></pre>
  <p>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:</p>
  <pre><code>add_subdirectory(&quot;libs/mylib&quot; &quot;${CMAKE_CURRENT_SOURCE_DIR}/libs/mylib/output&quot;)
  </code></pre>
  <p>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?</p>
  <p>EDIT1:
  Btw, rebuilds are done even if flavors don't specify any C++ flags etc - essentially they are same just different application/version name:</p>
  <pre><code>    dev {
          applicationIdSuffix &quot;.dev&quot;
          versionNameSuffix &quot;-dev&quot;
      }
      prod {
          applicationIdSuffix &quot;.prod&quot;
          versionNameSuffix &quot;-prod&quot;
      }
  </code></pre>
  <p>EDIT2: There's also another rebuild issue. When changing any <code>CMakeLists.txt</code> (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 <em><strong>Gradle/Android Studio</strong></em> for building <em><strong>NDK</strong></em> projects.</p>