Search code examples
androidgradlebuildsigningandroid-build-type

Android Studio: Is creation of a signing configuration necessary when creating a custom build type?


I have created custom buildTypes as follows and am not using the default debug and release buildTypes

buildTypes {
        releasefree {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        releasepro {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            applicationIdSuffix ".pro"
        }
        debugfree {
            shrinkResources true
            applicationIdSuffix ".debug"
            debuggable true
        }
        debugpro {
            shrinkResources true
            applicationIdSuffix ".pro.debug"
            debuggable true
        }
    }

Reason for doing this: I have several productFlavors and need to have a PRO version for each and I felt this was easier than creating a seperate PRO flavor for each free one. My code handles the difference using APPLICATION_ID from BuildConfig class. Another reason is i have customized classes and will need to replicate twice if i have two separate flavors. I know i can configure sourcesets, but I've had problems with that when i have too many flavors. Very difficult to track.

Now the issue: When i try to run the application using a custom buildType build variant, it asks me to create a signing configuration for each custom build type.

This is the error message i see

Similarly i see a message in the Run console when executing:

Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES]

Installation failed since the APK was either not signed, or signed incorrectly. If this is a Gradle-based project, then make sure the signing configuration is specified in the Gradle build script.

I understand that it may be required. But, what I was trying to find out was: Is there a setting what i could change so that the debugfree and debugpro buildTypes I've created could bypass the signing configuration requirement just like the default debug buildType? I know creating a signing configuration is a one minute thing and I'll do it if i do not get something soon. But just out of curiosity I want to understand what needs to be done to make the custom buildType work like the default debug buildType and not require a signing configuration.

Tried setting

debuggable true

(which was the only difference in the properties of th default debug buildType and my custom debug buildType) hoping it would work like the default debug buildType, but did not. What else need to be changed for the custom buildType to work like the default one i.e. not require a signing configuration.


Solution

  • Build types don't have strict inheritance. However, you can use what amounts to a copy constructor:

    debugfree.initWith(buildTypes.debug)
    

    where debugfree is a build type that you want to define, and debug is the build type you want to copy from.

    Do this before the rest of the initialization of the new build type (otherwise, initFrom() may wipe out parts of that initialization).

    In particular, since debug has a signing configuration already set up, starting a new build type by initializing from debug uses that same debug signing configuration.

    However, be very very careful when using the debug build type as a starting point. You don't want to accidentally wind up trying to ship apps with that debug signing configuration.