问题
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?
回答1:
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: 
- Create an Android Studio module project which only builds your native code, e.g. shared-native.
- Let the rest of your modules dependent on this project.
For this solution, you need to consider below points:
- Put your .sofiles into a proper location that other projects can see and link with.
- You can only include debugandreleasebuild types for thisshared-nativemodule 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.
来源:https://stackoverflow.com/questions/53847619/android-studio-cmake-build-once-for-all-build-variants