Debug Signing Config on Gradle Product Flavors

2019-01-22 04:11发布

问题:

I've got a project where I have several device-specific product flavors, and each flavor needs to be signed with a different config:

productFlavors {
    nexus7 {
        signingConfig signingConfigs.nexus7
    }
    nexus4 {
        signingConfig signingConfigs.nexus4
   }
}

This works great when building a 'release' variant. However, when using a 'debug' variant (e.g. when I build Nexus4Debug), Gradle is using the default android debug key. In my case, I'm highly dependent on these builds being signed the right way, and my application is relatively useless if signed with the default debug key. Anyone know if there's a way to specify the signing config for each variant?

I know I can do it per build type, a la:

buildTypes {
    debug {
        signingConfig signingConfigs.nexus4
    }
}

but this limits me to always using the same signing config for debug builds of both flavors.

PS - Understand this is a little bit of an edge use case here. This is for an enterprise project where we're testing custom ROMs and system-signed apps on a number of different Nexus devices.

回答1:

Try adding this to your build.gradle. It will specify which signingConfig to use for each flavor when building the debug build type:

buildTypes {
    debug {
        productFlavors.nexus4.signingConfig signingConfigs.nexus4
        productFlavors.nexus7.signingConfig signingConfigs.nexus7
    }
}


回答2:

I got another solution after android plugin build. 1.1.3

productFlavors {
    nexus7 {
        signingConfig signingConfigs.nexus7
    }
    nexus4 {
        signingConfig signingConfigs.nexus4
    }
}
buildTypes {
    release {
        debuggable false
        zipAlignEnabled true
    }
    debug {
        initWith release
        debuggable true
        zipAlignEnabled false
    }
}

As build type "release" will use flavor signing config (as there is no specification), after debug init with release build, it will also has the same signing config.

Build type "debug" needs to be init with "release" as if signing config is not provided, it will use Android default debug signing key.

Update

The problem is that android.buildTypes.debug.signingConfig has a default value while release does not.

The solution may be breakage in the future.

Anyway, still work with android plugin build 2.3.2



回答3:

Works on 2.2.1

buildTypes {
    release {
    }
    debug {
        signingConfig android.buildTypes.release.signingConfig
    }
}