Luke Needham
Luke Needham

Reputation: 3989

Why does the order of `buildTypes` matter?

I'm running into a strange issue with gradle.

My app/build.gradle file has a section which looks like this:

buildTypes {
        debug {
            ...
        }
        release {
            signingConfig signingConfigs.release
            ...
        }
        beta {
            initWith release
            ...
        }
    }

When I run ./gradlew assembleBeta, the output is app-beta.apk, which I can install on my device.

However, I want to change the order of my build types, purely for aesthetics. So it now looks like:

buildTypes {
        debug {
            ...
        }
        beta {
            initWith release
            ...
        }
        release {
            signingConfig signingConfigs.release
            ...
        }
    }

This syncs fine, but when I run ./gradlew assembleBeta, the output is now app-beta-unsigned.apk! And I cannot install this apk on a device - it tells me installation failed.

How is it possible that simply by changing the order in which my build types are declared, my apk can become broken?

Upvotes: 0

Views: 81

Answers (1)

Luke Needham
Luke Needham

Reputation: 3989

The reason the apk is broken after changing the order of build types is because, in this situation, gradle requires that the release build type is defined before the beta build type.

Why is this? It's because beta refers to release in the line: initWith release

If release is defined after beta, then the initWith release command will silently fail. This is because, at the point initWith release is run, release does not exist. Although it may seem like Gradle should be able to look-ahead to the definition of release later in the file, it cannot.

Then, when running ./gradlew assembleBeta, the beta apk is built successfully - but the apk is unsigned - as alluded to by the name of the output file, app-beta-unsigned.apk. In the previous setup, beta was getting its signingConfig from release. When initWith release fails, the beta build simply has no signingConfig set - so the output apk is of course unsigned.

Upvotes: 1

Related Questions