[Android] How to add debug buildTypes which do not need to be signed
by Riley MacDonald, April 18, 2018

When writing a sample Android application I wanted to add two build variants. One written in Kotlin, the other in Java. Because this application was intended for sample purposes only I wanted users to be able to clone the repo and immediately build each variant of the application. I shouldn’t need to include any signing configurations or keystores as they’re unnecessary for this project. Unfortunately when attempting to build via Android Studio I received the following error:

Error: The apk for your currently selected variant (app-variantName.apk) is not signed. Please specify a signing configuration for this variant (variantName).

Here’s the build.gradle:

android {
    // Adding the new variants
    buildTypes {
        variantOne {
        }
        variantTwo {
        }
    }
 
    // Share the resources in main
    sourceSets {
        variantOne {
            res.srcDirs = mainResourceDir
        }
        variantTwo {
            res.srcDirs = mainResourceDir
        }
    }
 
    // Ignore the debug and release variants to favor use of variantOne and variantTwo
    variantFilter { variant ->
        if (variant.buildType.name.equals('release') || variant.buildType.name.equals('debug')) {
            variant.setIgnore(true)
        }
    }
}

Android Documentation
The Android documentation on build variants and signing specifically states “Gradle does not sign your release build’s APK unless you explicitly define a signing configuration for this build”. So why is Gradle complaining about signing the configuration here? Is this a bug with the Android Gradle plugin?

Workaround
I was able to workaround this issue by using the initWith() method (copies all properties from the given build type) and providing the default debug build variant (which I’m ignoring):

android {
    buildTypes {
        variantOne {
            initWith debug
        }
        variantTwo {
            initWith debug
        }
    }
}

Success, I’m now able to build each variant without the need for signing.

Open the comment form

Leave a comment:

Comments will be reviewed before they are posted.

User Comments:

Bartek on 2019-12-07 00:44:02 said:
Thanks! Saved me hours of headache ;)