Android Studio:创建自定义构建类型时是否需要创建签名配置?

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

我已经创建了如下自定义构建类型并且没有使用默认的调试和发布构建类型

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
        }
    }

这样做的原因: 我有几个 productFlavors,每个都需要一个 PRO 版本,我觉得这比为每个免费版本创建一个单独的 PRO flavors 更容易。我的代码使用来自 BuildConfig class 的 APPLICATION_ID 来处理差异。另一个原因是我已经定制了 classes,如果我有两种不同的口味,则需要复制两次。我知道我可以配置源集,但是当我有太多口味时我遇到了问题。很难追踪。

现在的问题是: 当我尝试 运行 使用自定义 buildType 构建变体的应用程序时,它要求我为每个自定义构建类型创建一个签名配置.

同样,我在执行时在 运行 控制台中看到一条消息:

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.

我知道这可能是必需的。但是,我试图找出的是:是否有我可以更改的设置,以便我创建的 debugfree 和 debugpro 构建类型可以像默认调试构建类型一样绕过签名配置要求? 我知道创建一个签名配置是一分钟的事情,如果我没有很快得到什么,我会做的。但出于好奇,我想了解需要做什么才能使自定义 buildType 像默认调试 buildType 一样工作,而不需要签名配置。

尝试设置

debuggable true

(这是默认调试 buildType 和我的自定义调试 buildType 的属性的唯一区别)希望它能像默认调试 buildType 一样工作,但没有。还需要更改哪些内容才能使自定义 buildType 像默认 buildType 一样工作,即不需要签名配置。

根据@CommonsWare 对我上面的问题的评论,我按如下方式更改了 buildTypes,它非常有效。在添加自定义配置之前需要使用默认 buildType 初始化自定义 buildType。是否用于调试以及发布自定义类型并且成功了。

再次张贴在这里,以防像我这样好奇的人能从中受益。

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

构建类型没有严格的继承。但是,您可以使用相当于复制构造函数的内容:

debugfree.initWith(buildTypes.debug)

其中 debugfree 是您要定义的构建类型,debug 是您要从中复制的构建类型。

在新构建类型的其余初始化之前执行此操作(否则,initFrom() 可能会清除部分初始化)。

特别是,由于 debug 已经设置了签名配置,因此通过从 debug 初始化开始新的构建类型使用相同的调试签名配置。

但是,在使用 debug 构建类型作为起点时,非常非常小心。您不希望意外结束尝试使用该调试签名配置发布应用程序。