okuzawatsの日記

Android / Kotlin Enthusiast 🤖

Android Signing per Flavors with Gradle

I think you sometimes want to sign Android APK with different keystore per build flavors. You can do this with signingConfigs and buildTypes of app/build.gradle.

First, define signingConfigs inside android block of your app/build.gradle as many as the number of signings. Inside the signingCofigs block, load storeFile and keys. Maybe from .properties file when you build APK in your local or environmental variables when you build APK in CI server.

android {
    // omitted
    signingConfigs {
        dev {
            storeFile = // TODO
            storePassword // TODO
            keyAlias // TODO
            keyPassword // TODO
        }
        stg {
            storeFile = // TODO
            storePassword // TODO
            keyAlias // TODO
            keyPassword // TODO
        }
        prd {
            storeFile = // TODO
            storePassword // TODO
            keyAlias // TODO
            keyPassword // TODO
        }
    }
    // omitted
}

Secondly, define productFlavors as always.

android {
    // omitted
    flavorDimensions 'default'
    productFlavors {
        dev {
            dimension "default"
            applicationIdSuffix ".dev"
        }
        stg {
            dimension "default"
            applicationIdSuffix ".stg"
        }
        prd {
            dimension "default"
        }
    }
    // omitted
}

Lastly, define signingConfigs per the flavors defined in buildTypes block. productFlavors.yourflavor.signinfConfig is the signing configuration for the flavor. signingConfigs.config is the signing configuration defined in signingConfigs.

android {
    // omitted
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }

        release {
            productFlavors.dev.signingConfig signingConfigs.dev
            productFlavors.stg.signingConfig signingConfigs.stg
            productFlavors.prd.signingConfig signingConfigs.prd
        }
    }
    // omitted
}

I guess now you can sign APK with different keystore for different flavors. Maybe buildTypes block should be defined after signingConfigs block and productFlavors block.

android {
    signingConfigs {
        // omitted
    }

    flavorDimensions 'default'
    productFlavors {
        // omitted
    }

    buildTypes {
        // omitted
    }
}

#Android #Gradle

About me 😎

profile

茨城県つくば市在住のソフトウェアエンジニアです。専門領域はAndroidアプリ開発で、特にアーキテクチャに興味があります。某社でAndroidアプリ開発のテックリードをしています。

Jetpack ComposeによるAndroid MVVMアーキテクチャ入門の著者です。

👉 もっと詳しく

Writing 📝

Android MVVMアーキテクチャ入門

Androidアプリ開発の初学者に向けた、MVVM(Model-View-ViewModel)アーキテクチャの入門書を書きました。初学者の方を確実にネクストレベルに引き上げる技術書です。

👉 もっと詳しく

See Also 👀