8

I've just updated gradle from 6 to 7 and AGP from 4 to 7. There is part of my gradle setup that stopped compiling.

applicationVariants.all { variant ->
        if (variant.getName().contains("prodSe")) {               
            variant.mergedFlavor.signingConfig = signingConfigs.prodSeSigningConfig
        }
}

This now causes:

Cannot resolve which method to invoke for [class com.android.build.gradle.internal.dsl.SigningConfig_Decorated] due to overlapping prototypes between: [interface com.android.builder.model.SigningConfig] [interface com.android.build.api.dsl.ApkSigningConfig]

Feels like variant.mergedFlavor now returns a type inheriting from both SigningConfig & ApkSigningConfig that have same methods. Not sure how to fix this.

Jacek Kwiecień
  • 12,397
  • 20
  • 85
  • 157
  • I used to do `(variant.mergedFlavor as AbstractProductFlavor).setSigningConfig` it is still valid syntax on Gradle 7. But this now leads to https://stackoverflow.com/questions/66579530/error-the-apk-for-your-currently-selected-variant-unknown-output-is-not-signe/68702750#68702750 – Jemshit Aug 23 '21 at 06:21
  • Did you manage to fix it? – Jemshit Aug 23 '21 at 07:55
  • https://issuetracker.google.com/issues/197430188 – Jemshit Aug 25 '21 at 20:08

2 Answers2

1

I had the same problem and I ended up setting signingConfigs inside a variantFilter block https://developer.android.com/studio/build/build-variants#filter-variants

So in your case, it will be something like this:

variantFilter { variant ->
    if (variant.getName().contains("prodSe")) {
        android.defaultConfig.signingConfig signingConfigs.prodSeSigningConfig
    }
}
ppodgorski
  • 81
  • 5
0

@ppodgorski answer is correct but today we can use a better solution using the androidComponents block. I'm using Gradle 7.4 and AGP 7.3.1 and the following is the code that lets you choose the right signingConfig based on your variant. Keep in mind that my android app is multi-flavour with 2 dimensions.

signingConfigs {
    play_store {
        storeFile file(properties.getProperty('play_store_storeFile'))
        keyAlias properties.getProperty('play_store_keyAlias')
        storePassword properties.getProperty('play_store_storePassword')
        keyPassword properties.getProperty('play_store_keyPassword')
        v1SigningEnabled true
        v2SigningEnabled true
    }
    smart {
        storeFile file(properties.getProperty('smart_storeFile'))
        keyAlias properties.getProperty('smart_keyAlias')
        storePassword properties.getProperty('smart_storePassword')
        keyPassword properties.getProperty('smart_keyPassword')
        v1SigningEnabled true
        v2SigningEnabled true
    }
    huawei_store {
        storeFile file(properties.getProperty('huawei_store_storeFile'))
        keyAlias properties.getProperty('huawei_store_keyAlias')
        storePassword properties.getProperty('huawei_store_storePassword')
        keyPassword properties.getProperty('huawei_store_keyPassword')
        v1SigningEnabled true
        v2SigningEnabled true
    }
}

flavorDimensions "brand", "services"

productFlavors {
// different flavours definition
}

androidComponents {
    onVariants(selector().all(), { variant ->
        if (variant.name == "xxyyzzGmsRelease") {
            variant.signingConfig?.setConfig(signingConfigs.play_store)
        } else if (variant.name == "xxyyzzHmsRelease") {
            variant.signingConfig?.setConfig(signingConfigs. huawei_store)
        } else if (variant.name.contains("aabbccGmsRelease")) {
            variant.signingConfig?.setConfig(signingConfigs.smart)
        }
    })
}
Mariusz Wiazowski
  • 2,118
  • 1
  • 16
  • 17
  • I tried the above with Gradle 7.4.1 and AGP 7.3.1 and when I do Build > Build Bundle(s) / APK(s) > Build APK(s) I end up with an unsigned APK. How do you get the signed APK with the above config? – Eselfar Feb 23 '23 at 14:21