Android Studio CMake 为所有构建变体构建一次 [英] Android Studio CMake build once for all build variants

查看:23
本文介绍了Android Studio CMake 为所有构建变体构建一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 C++ 代码 (JNI) 和许多构建变体和组合的项目.这些用于实现应用程序的不同品牌/风格(即颜色、样式、图标等).

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, ...).

例如,可能有 n 个客户的构建变体:

For example there could be build variants for n customers :

  • customerNDebug
  • customerNDebugproguard
  • customerNRelease.

无论如何,C/C++ 层对于每种产品风格都是相同的.

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

通常 CMake 在 Android Studio 中为每个构建变体编译 C 代码.如果您有诸如debug"和release"之类的构建变体,其中生成的代码实际上不同,那么这很有意义.但是,对于我拥有的构建变体,编译后的输出始终相同.

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.

目前,C/C++ 代码的编译是使用我们项目中的外部工具完成的,我想在 Android Studio 中使用 CMake 进行编译,只是为了 C/C++ 代码的适当 IDE 支持.所以对我来说,一次构建就足够了.

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.

是否可以告诉 Android Studio 只构建一次 C/C++ 代码,而不管构建变体和风格如何?

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

推荐答案

默认情况下,Android Studio IDE 与 CMake 和 Gradle 一起会生成一系列以 externalNativeBuild 命名的原生构建任务根据您的构建类型和风格.如果你想扭转这种行为,一些解决方法如下:

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. 创建一个仅构建您的本机代码的 Android Studio 模块项目,例如共享原生.
  2. 让你的其余模块依赖于这个项目.

对于此解决方案,您需要考虑以下几点:

For this solution, you need to consider below points:

  1. 将您的 .so 文件放到其他项目可以看到和链接的适当位置.
  2. 您只能为此shared-native 模块项目包含debugrelease 构建类型,以避免多次重新构建.或者您可以简单地让您的其他项目依赖于发布类型,以便它只构建ONCE.
  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.

这篇关于Android Studio CMake 为所有构建变体构建一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆