用于多个变体的多个signingConfigs [英] Multiple signingConfigs for multiple variants

查看:316
本文介绍了用于多个变体的多个signingConfigs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为不同的变体设置不同的签名配置?

例如,我们目前有两种类型的Debug/Beta/Release Build/Beta/Release,免费和付费,有6种变体.为了使操作更简单,让我们忘记Debug变体,而只关注freeBeta/paidBeta/freeRelease/paidRelease.

我想要的是让每个变体使用单独的不同signingConfig.

到目前为止,我唯一能找到的解决方案是将signingConfigs放入buildTypes中,以便所有Beta变体都具有相同的signingConfigs:

buildTypes {
    beta {
        signingConfigs.beta
    }
    release {
        signingConfigs.release
    }
}

或者,使用风味,在这种情况下,所有免费变体都将具有相同的signingConfigs:

productFlavors {
    free {
        signingConfig signingConfigs.free
        applicationId 'com.example.free'
    }
    paid {
        signingConfig signingConfigs.paid
        applicationId 'com.example.paid'
    }
}

在当前的productFlavor闭包中,有没有办法做到这一点?只能通过重写android.applicationVariants.all { variant ->并基于某种命名方案或其他丑陋的技巧为每个应用程序变体手动应用signingConfig来解决此问题吗?

我还找到了此答案,但是它似乎无法在最新的构建工具中使用;编译时出现以下错误:

失败:构建失败,并出现异常.

  • 位置:构建文件'/home/dev/projects/app/build.gradle'行:61

  • 出了什么问题:评估项目':app'时出现问题.

    在ProductFlavor容器上找不到属性免费".

解决方案

链接的答案实际上运行良好.我得到了像这样编译(使用buildTools 1.3.1和gradle-wrapper 2.7).您遇到的错误(在ProductFlavor容器上找不到属性免费")最可能是由于以下事实:您的构建类型是在build.gradle中的productFlavors之前定义的.

这不起作用

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

这将起作用 (只需交换productFlavors和buildType的定义顺序)

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

这是一个完整的示例:

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                
        }
    }

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, resulting in 6 variants. To make it a bit easier, let's forget the Debug variants and only focus on freeBeta/paidBeta/freeRelease/paidRelease.

What I'd like, is for each variant to use a separate different signingConfig.

So far the only solutions I could find is either putting the signingConfigs in the buildTypes so all Beta variants would have the same signingConfigs:

buildTypes {
    beta {
        signingConfigs.beta
    }
    release {
        signingConfigs.release
    }
}

Alternatively, using the flavors, in which case all free variants would have the same signingConfigs:

productFlavors {
    free {
        signingConfig signingConfigs.free
        applicationId 'com.example.free'
    }
    paid {
        signingConfig signingConfigs.paid
        applicationId 'com.example.paid'
    }
}

Is there a way to do this in the current productFlavor closure? Can this only be fixed by overridding the android.applicationVariants.all { variant -> and manually applying a signingConfig for each application variant based on some naming scheme or some other ugly hack?

I also found this answer, but it doesn't appear to work in the latest build tools; when compiling I get the following error:

FAILURE: Build failed with an exception.

  • Where: Build file '/home/dev/projects/app/build.gradle' line: 61

  • What went wrong: A problem occurred evaluating project ':app'.

    Could not find property 'free' on ProductFlavor container.

解决方案

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                
        }
    }

这篇关于用于多个变体的多个signingConfigs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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