如何配置构建类型与产品风格?

时间:2017-02-03 16:55:34

标签: android continuous-integration android-gradle fastlane

基于这个答案https://stackoverflow.com/a/27908019/5156317我有一个跟进问题:什么使应用程序与产品口味不同?我试图将其与我的XCode设置进行比较,如下所示:

  • 使用测试后端的Dev App
  • 使用生产后端的Dev App
  • 使用测试后端的测试应用程序(企业分发)
  • 使用生产后端的测试应用程序(企业分发
  • 使用生产后端的实时应用程序(应用程序商店分发)

我对android设置的想法:

buildTypes: debug_test debug_production //不需要企业应用,因为任何设备上都可能有未签名的应用 释放

口味: 对myApp

感谢您的支持!

2 个答案:

答案 0 :(得分:15)

好吧,为了使用不同的后端,我不会指定比debugrelease更多的构建类型。 相反,我会使用其中一些技巧:

  • 更多口味,
  • 自定义构建配置字段(文档here),
  • 结合多种产品口味(文档here)。

您可以使用BuildConfig类在应用程序代码中访问构建类型,构建flavor和自定义字段。

简单口味的方法

  • 构建类型:

    • debug
    • release
  • 口味:

    • dev
    • test
    • live

这将导致这些构建变体(您不必使用所有这些变体):

  • devDebug
  • devRelease
  • testDebug
  • testRelease
  • liveDebug
  • liveRelease

使用维度组合多种口味的方法

  • 风味尺寸:

    • backend
    • target
  • 构建类型:

    • debug
    • release
  • 口味:

    • target维度:
      • dev
      • test
      • live
    • backend维度:
      • production
      • test

这将导致这些构建变体(同样,您不必使用所有这些变体):

  • productionDevDebug
  • productionDevRelease
  • productionTestDebug
  • productionTestRelease
  • productionLiveDebug
  • productionLiveRelease
  • testDevDebug
  • testDevRelease
  • testTestDebug
  • testTestRelease
  • testLiveDebug
  • testLiveRelease

使用构建字段

在构建类型中使用其他值并构建flavor声明,例如:

buildConfigField "boolean", "production_backend", "false"

buildConfigField "String", "backend", "\"production\""

答案 1 :(得分:2)

build.gradle

class Globals {
    static String devDebug = "_devDebug"
    static String devRelease = "_devRelease"
    static String stagingQA = "_stagingQa"
    static String prodRelease = "_prodRelease"
    static String prodDebug = "_prodDebug"

    def firstproduct = "firstproductFP"
    def secondproduct = "secondproductFP"

//     Product key
    static String FP = "FP"
    static String SP = "SP"
}

android {

    buildTypes {
        debug {}
        qa {}
        release {}
    }

    flavorDimensions "client", "backend"
    productFlavors { 

//          First Product (FP)
        FP_dev {    
            dimension 'backend'
            buildConfigField("String", "TEST", "\"FP_dev\"")
        }
        FP_staging {
            dimension 'backend'
            buildConfigField("String", "TEST", "\"FP_staging\"")
        }
        FP_prod {
            dimension 'backend'
            buildConfigField("String", "TEST", "\"FP_prod\"")
        }
        firstproduct {
            dimension 'client'
            ...
        }


//          Second Product (SP)
        SP_dev {    
            dimension 'backend'
            buildConfigField("String", "TEST", "\"SP_dev\"")
        }
        SP_staging {
            dimension 'backend'
            buildConfigField("String", "TEST", "\"SP_staging\"")
        }
        SP_prod {
            dimension 'backend'
            buildConfigField("String", "TEST", "\"SP_prod\"")
        }
        secondproduct {
            dimension 'client'
            ...
        }
}

    variantFilter {
        variant ->
           def needed = variant.name in [
                    Globals.firstproduct + Globals.FP + Globals.devDebug,
                    Globals.firstproduct + Globals.FP + Globals.stagingQA,
                    Globals.firstproduct + Globals.FP + Globals.prodRelease,

                    Globals.secondproduct + Globals.FP + Globals.devDebug,
                    Globals.secondproduct + Globals.FP + Globals.stagingQA,
                    Globals.secondproduct + Globals.FP + Globals.prodRelease
            ]
            variant.setIgnore(!needed)
    }

}

此解决方案的方法允许针对多种产品口味具有多个客户端编译和后端环境。

我所做的是将后端的开发环境与调试android的编译相关联,将后端的暂存与android的qa关联,并将后端的生产与android的发行版相关联。请记住,在某些情况下,您需要调试生产环境或混淆开发环境,此解决方案允许这样做。

  • firstproductFP_devDebug
  • firstproductFP_stagingQa
  • firstproductFP_prodRelease
  • secondproductSP_devDebug
  • secondproductSP_stagingQa
  • secondproductSP_prodRelease

编译 firstproductFP_devDebug 的示例:

BuildConfig.java

public static final String FLAVOR = "firstproductFP_dev";
public static final String FLAVOR_client = "firstproduct";
public static final String FLAVOR_backend = "FP_dev";
public static final String BUILD_TYPE = "debug";

请注意,在VariantFilter {}范围内,不能使用buildConfigField()来根据构建类型和产品风味来编译值。这迫使我们使用 flavorDimensions 和大量的 productsFlavors 。活动构建版本也不能重命名。

重要提示:变量的值必须与变量名匹配 产品口味

GL

来源: