Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio CMake build once for all build variants

I have a project with C++ code (JNI) and lots of build variants and combinations. These are used to implement different brandings / flavors of the app (i.e. colors, styles, icons, ...).

For example there could be build variants for n customers :

  • customerNDebug
  • customerNDebugproguard
  • customerNRelease.

Anyway, the C/C++ layer is the same for each product flavor.

Normally CMake in Android Studio compiles C code for every build variant. This makes sense if you have build variants like "debug" and "release" where the resulting code actually differs. However, for the build variants I have, the compiled output is always the same.

At the moment the compilation of C/C++ code is done using an external tool in our project and I want to compile using CMake in Android Studio only for proper IDE support of C/C++ code. So for me a single build would suffice.

Is it possible to tell Android Studio to build C/C++ code only once, no matter the build variants and flavors?

like image 783
mihca Avatar asked Sep 05 '25 08:09

mihca


1 Answers

By default, Android Studio IDE together with CMake and Gradle will generate a series of native build tasks named with externalNative<BuildVariant>Build according to your build types and flavours. If you want to twist this behaviour, some workaround is as below:

  1. Create an Android Studio module project which only builds your native code, e.g. shared-native.
  2. Let the rest of your modules dependent on this project.

For this solution, you need to consider below points:

  1. Put your .so files into a proper location that other projects can see and link with.
  2. You can only include debug and release build types for this shared-native module project to avoid too many times of re-build. Or you can simply to let your other projects depends on the release type so that it will be built only ONCE.
like image 97
shizhen Avatar answered Sep 08 '25 04:09

shizhen