5

I have an Android/NDK application and a build script which uses gradlew.bat to automate build/install.

The build.gradle uses CMakeLists.txt and GLOB_RECURSE internally in order to build the C++ files. However, if I add a new .cpp file to my C++ source code directory, I have to manually start Android Studio and run "Build/Refresh Linked C++ Projects", otherwise the buildscript will not pick up the added .cpp files and I get linker errors.

I would like to have the buildscript to perform the "Refresh Linked C++ Projects" action, but I cannot find a Gradle task which performs this.

Does anyone know how I can perform this action, with or without Gradle, from command line?

Viktor Sehr
  • 12,825
  • 5
  • 58
  • 90
  • 1
    The easiest way to refresh your CMake is to delete the `.externalNativeBuild` directory before you run gradlew. You can add such action as a custom `clean` task to **build.gradle**. – Alex Cohn Aug 04 '19 at 20:23
  • That would force a rebuild of everything every time I build, isn't it possible to just refresh the list? (Perhaps removing build.ninja or android_gradle_build.json) – Viktor Sehr Aug 05 '19 at 08:27
  • 1
    Note that, with newer plugin, the directory is named `.cxx` instead of `.externalNativeBuild` – Alex Cohn Aug 05 '19 at 20:53
  • 1
    *"Refresh Linked C++ Projects"* does exactly that: full recompile. But you don't need to refresh the list every time you build. Run this custom `clean` task only when necessary. Usually, if you add cpp files, you change some other code to call them. Therefore, CMake will fail because it won't have the refreshed list of files. This would mean it's time to run `clean`. – Alex Cohn Aug 05 '19 at 20:58
  • @ViktorSehr Did you find a solution to this ? – Anonymous Mar 09 '21 at 11:10
  • I append a line to CMakeLists.txt in order to force an update – Viktor Sehr Mar 09 '21 at 11:51

2 Answers2

1

A workaround would be delete your CMakeCache.txt before executing other tasks. This will force the CMake to regenerate building files.

Yingjie Ye
  • 11
  • 2
1

you have to make sure you've added

externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }

on your build.gradle of your module. and if still Refresh Linked C++ Projects is disabled you can remove .cxx and rebuild again.

Mohammad Davari
  • 430
  • 3
  • 13