Gradle:如何构建不同版本类型的不同风味?

113

有人能告诉我是否可以通过命令行只构建我的不同风味中的一个吗?

目前我还没有找到例如执行以下命令的方法:

gradle buildDev 

当Dev是我不同的味道之一时。实际上,我必须执行:

gradle build

所有口味都已经构建完成。

我想跳过一些口味。这可行吗?

谢谢。


先尝试运行gradle而不使用参数,然后读取输出...将会提示运行gradle tasks... - Selvin
4个回答

179

虽然没有特定于口味的build任务版本,但有特定于口味的assembleinstall任务版本。assemble将创建APK;install将在设备/模拟器上安装它。

例如,在此示例项目中,我定义了两个产品风味(chocolatevanilla)和三种总构建类型(debugreleasemezzanine)。

运行gradle tasks命令会显示如下内容,其中包括:

Build tasks
-----------
assemble - Assembles all variants of all applications and secondary packages.
assembleChocolate - Assembles all builds for flavor Chocolate
assembleChocolateDebug - Assembles the Debug build for flavor Chocolate
assembleChocolateDebugTest - Assembles the Test build for the ChocolateDebug build
assembleChocolateMezzanine - Assembles the Mezzanine build for flavor Chocolate
assembleChocolateRelease - Assembles the Release build for flavor Chocolate
assembleDebug - Assembles all Debug builds
assembleMezzanine - Assembles all Mezzanine builds
assembleRelease - Assembles all Release builds
assembleTest - Assembles all the Test applications
assembleVanilla - Assembles all builds for flavor Vanilla
assembleVanillaDebug - Assembles the Debug build for flavor Vanilla
assembleVanillaDebugTest - Assembles the Test build for the VanillaDebug build
assembleVanillaMezzanine - Assembles the Mezzanine build for flavor Vanilla
assembleVanillaRelease - Assembles the Release build for flavor Vanilla

Install tasks
-------------
installChocolateDebug - Installs the Debug build for flavor Chocolate
installChocolateDebugTest - Installs the Test build for the ChocolateDebug build
installChocolateMezzanine - Installs the Mezzanine build for flavor Chocolate
installChocolateRelease - Installs the Release build for flavor Chocolate
installVanillaDebug - Installs the Debug build for flavor Vanilla
installVanillaDebugTest - Installs the Test build for the VanillaDebug build
installVanillaMezzanine - Installs the Mezzanine build for flavor Vanilla
installVanillaRelease - Installs the Release build for flavor Vanilla
uninstallAll - Uninstall all applications.
uninstallChocolateDebug - Uninstalls the Debug build for flavor Chocolate
uninstallChocolateDebugTest - Uninstalls the Test build for the ChocolateDebug build
uninstallChocolateMezzanine - Uninstalls the Mezzanine build for flavor Chocolate
uninstallChocolateRelease - Uninstalls the Release build for flavor Chocolate
uninstallVanillaDebug - Uninstalls the Debug build for flavor Vanilla
uninstallVanillaDebugTest - Uninstalls the Test build for the VanillaDebug build
uninstallVanillaMezzanine - Uninstalls the Mezzanine build for flavor Vanilla
uninstallVanillaRelease - Uninstalls the Release build for flavor Vanilla

2
然后,当我想要构建该版本的APK时,我必须使用assembleXXX。很酷。谢谢。 - Jose M Lechon
12
@Lechon: 执行命令 gradle assembleChocolateDebug 将会在你的项目中生成一个路径为 build/apk/HelloProductFlavors-chocolate-debug-unaligned.apk 的文件。不过,我不能排除只有在口感可口的情况下才有效的可能性。 :-) - CommonsWare
2
@Zainodis:这个答案已经超过一年了,他们可能已经添加了一个任务来为所有产品口味组装调试构建类型。 - CommonsWare
3
如果你有模块,请不要忘记添加模块前缀 :<module>:assemble<FlavorName> - Torge
1
@Dwebtron:如果你只是想要一个带有产品风格的 build.gradle,请查看 https://github.com/commonsguy/cw-omnibus/blob/v9.0/Manifest/Merger/app/build.gradle - CommonsWare
显示剩余14条评论

32

我会简化@CommonsWare所提供的答案,因为在阅读该答案时我有点困惑。

考虑这些是产品口味

  • 开发版
  • 预发布版
  • 生产版

运行

gradlew task

这将列出所有产品口味以及其构建类型。

assemble - Assembles all variants of all applications and secondary packages.
assembleAndroidTest - Assembles all the Test applications.
assembleDebug - Assembles all Debug builds.
assembleDEV - Assembles all DEV builds.
assemblePREPROD - Assembles all PREPROD builds.
assemblePROD - Assembles all PROD builds.
assembleRelease - Assembles all Release builds.

通过这个,您可以轻松选择口味,并将基于所选口味生成构建。

gradlew assemblePREPROD


25

如果你的产品口味是巧克力,你可以这样做

./gradlew assembleChocolateRelease
或者
./gradlew assembleChocolateDebug

5

补充以上答案,如果要构建 Android Bundle(AAB),则可以使用以下方法

# build flavor 'flavorName' only
./gradlew bundleFlavorName

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接