使用flavor签名配置覆盖调试构建类型签名配置

spi*_*ce7 13 android android-gradle-plugin

我有一个有2种口味的Android应用程序:internal而且production,还有2种构建类型:debugrelease.

我正在尝试根据风格分配签名配置,根据文档是可行的.我看了之后发现了其他答案,但似乎都没有.所有内容都会编译,但应用程序正在使用本机的本地调试密钥库进行签名.

这是我的gradle文件:

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 22
        versionCode 1
        versionName "1.0.0"
    }

    signingConfigs {
        internal {
            storeFile file("../internal.keystore")
            storePassword "password"
            keyAlias "user"
            keyPassword "password"
        }
        production {
            storeFile file("../production.keystore")
            storePassword "password"
            keyAlias "user"
            keyPassword "password"
        }
    }

    productFlavors {
        internal {
            signingConfig signingConfigs.internal
            applicationId 'com.test.test.internal'
        }
        production {
            signingConfig signingConfigs.production
            applicationId 'com.test.test'
        }
    }

    buildTypes {
        debug {
            applicationIdSuffix ".d"
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    variantFilter { variant ->
        if (variant.buildType.name.equals('debug')
                && variant.getFlavors().get(0).name.equals('production')) {
            variant.setIgnore(true);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:我也在编译 classpath 'com.android.tools.build:gradle:1.1.3'

spi*_*ce7 23

似乎默认情况下,Android signingConfig在调试构建类型(android调试密钥库)上有一个集合,当signingConfig为构建类型设置时,signingConfig会忽略调整.

解决的办法是设置signingConfignull上调试版本类型.然后将使用signingConfig给定的味道:

buildTypes {
        debug {
            // Set to null to override default debug keystore and defer to the product flavor.
            signingConfig null
            applicationIdSuffix ".d"
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,为什么在`applicationIdSuffix`上有`.d`?那有必要吗? (2认同)
  • 不,这不对。我给我的调试版本一个单独的包而不是我的发布版本,所以它们可以安装在同一台设备上而不会相互干扰。不过我强烈推荐它:-) (2认同)
  • 没有必要将调试键设置为空。我们可以为每种类型和风格设置唯一的密钥:http://stackoverflow.com/a/35057525/2557258 (2认同)
  • 向您的回答致敬。保存我的风味概念项目..为我工作 (2认同)
  • 哇,这不是很直观。感谢您的回答!这有记录在任何地方吗? (2认同)