Multiple signingConfigs for multiple variants

前端 未结 2 1736
无人共我
无人共我 2020-12-05 12:28

How can I set different signing configs for different variants?

For instance, we currently have the buildtypes Debug/Beta/Release with 2 flavors, free and paid, resu

相关标签:
2条回答
  • 2020-12-05 12:30

    The https://stackoverflow.com/a/32810290/3961802 answer will not work.

        beta {
            productFlavors.free.signingConfig signingConfigs.freeBeta
            productFlavors.paid.signingConfig signingConfigs.paidBeta
        }
        release {
            productFlavors.free.signingConfig signingConfigs.freeRelease
            productFlavors.paid.signingConfig signingConfigs.paidRelease                
        }
    

    In this case, the release build type will overwrite all flavors. So signing config for the freeBeta will be freeRelease.

    At the moment, the only solution that I know is to sign all the build variants in a separate task.

    signingConfigs {
    
        bananaDebug {}
        bananaBeta {}
        bananaRelease {}
    
        orangeDebug {}
        orangeBeta {}
        orangeRelease {}
    
        lemonDebug {}
        lemonBeta {}
        lemonRelease {}
    }
    
    productFlavors {
    
        banana {}
    
        orange {}
    
        lemon {}
    }
    
    buildTypes {
    
        debug {}
    
        beta {}
    
        release {}
    }
    
    applicationVariants.all {
        def flavorName = it.getFlavorName()
        def buildTypeName = it.buildType.name
        def buildVariantName = flavorName + buildTypeName.capitalize()
        def currentSigningConfig = signingConfigs.getByName(buildVariantName)
    
        it.mergedFlavor.signingConfig = currentSigningConfig
        // If you want to sign debug build
        buildTypes.debug.signingConfig currentSigningConfig
    }
    
    0 讨论(0)
  • 2020-12-05 12:48

    The answer linked is actually working fine. I got it to compile like this (with buildTools 1.3.1 and gradle-wrapper 2.7). The error you were having (Could not find property "free" on ProductFlavor container) is most certainly due to the fact that your build types are defined before the productFlavors in your build.gradle

    This won't work

    signingConfigs {
        freeBeta {}
        freeRelease {}
        paidBeta {}
        paidRelease {}
    }    
    buildTypes {
        debug {}
        beta {}
        release {}
    }
    productFlavors {
        free {}
        paid {}
    }
    

    This would work (simply swapping the definition order of the productFlavors and buildType)

    signingConfigs {
        freeBeta {}
        freeRelease {}
        paidBeta {}
        paidRelease {}
    }    
    productFlavors {
        free {}
        paid {}
    }    
    buildTypes {
        debug {}
        beta {}
        release {}
    }
    

    Here's a full working example:

    signingConfigs {
            freeBeta {
                keyAlias 'freeBeta'
                keyPassword 'test'
                storeFile file('C:/keystore.jks')
                storePassword 'test'
            }
            freeRelease {
                keyAlias 'freeRelease'
                keyPassword 'test'
                storeFile file('C:/keystore.jks')
                storePassword 'test'
            }
            paidBeta {
                keyAlias 'paidBeta'
                keyPassword 'test'
                storeFile file('C:/keystore.jks')
                storePassword 'test'
            }
            paidRelease {
                keyAlias 'paidRelease'
                keyPassword 'test'
                storeFile file('C:/keystore.jks')
                storePassword 'test'
            }
        }
    
        productFlavors {
            free {
    
            }
            paid {
    
            }
        }
    
        buildTypes {
            debug {
    
            }
            beta {
                productFlavors.free.signingConfig signingConfigs.freeBeta
                productFlavors.paid.signingConfig signingConfigs.paidBeta
            }
            release {
                productFlavors.free.signingConfig signingConfigs.freeRelease
                productFlavors.paid.signingConfig signingConfigs.paidRelease                
            }
        }
    
    0 讨论(0)
提交回复
热议问题