Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
613 views
in Technique[技术] by (71.8m points)

android - Signing product flavors with gradle

I am tyring to migrate my projects to gradle. One of my projects has multiple product flavors and each one of them has to be signed with a different signingConfig in its release version. So this is what I tried so far:

buildscript {
    ...
}

apply plugin: 'android'

android {
    compileSdkVersion 17
    buildToolsVersion '17'

    signingConfigs {
        flavor1 {
            storeFile file("keystore")
            storePassword "secret"
            keyAlias "aliasForFlavor1"
            keyPassword "secretFlavor1"
        }

        flavor2 {
            storeFile file("keystore")
            storePassword "secret"
            keyAlias "aliasForFlavor2"
            keyPassword "secretFlavor2"
        }
    }

    productFlavors {
        flavor1 {
            signingConfig signingConfigs.flavor1
        }

        flavor1 {
            signingConfig signingConfigs.flavor2
        }
    }
}

dependencies {
    ...
}

When I run gradle build I get a groovy.lang.MissingFieldException and the following error message:

No such field: signingConfigs for class: com.android.build.gradle.internal.dsl.GroupableProductFlavorFactory

So I assume the productFlavors.* part of the Gradle script is not the right place to put code signing configurations.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can declare signing config for each flavor in buildType. Here is my gradle file for release signing flavors with different keystores.

android {
  signingConfigs {
    configFirst {
        keyAlias 'alias'
        keyPassword 'password'
        storeFile file('first.keystore')
        storePassword 'password'
    }

    configSecond {
        keyAlias 'alias'
        keyPassword 'password'
        storeFile file('second.keystore')
        storePassword 'password'
    }
  }

  compileSdkVersion 23
  buildToolsVersion "23.0.2"
  defaultConfig {
        minSdkVersion 14
        targetSdkVersion 23
  }

  productFlavors{
    flavor1 {
        applicationId "com.test.firstapp"
    }

    flavor2 {
        applicationId "com.test.secondapp"
    }
  }

  buildTypes {
    release {
        productFlavors.flavor1.signingConfig signingConfigs.configFirst
        productFlavors.flavor2.signingConfig signingConfigs.configSecond           

        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'),
                'proguard-rules.pro'

    }
  }
}

buildTypes block should be placed after productFlavors block, I mean order is important.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...