我想把我的项目迁移到gradle。我的一个项目有多种产品风格,每种产品都必须在其发布版本中使用不同的signingConfig
进行签名。所以这就是我到目前为止所做的:
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 {
...
}
当我运行gradle build
时,我收到groovy.lang.MissingFieldException
并收到以下错误消息:
No such field: signingConfigs for class: com.android.build.gradle.internal.dsl.GroupableProductFlavorFactory
所以我认为Gradle脚本的productFlavors
。*部分不是放置代码签名配置的正确位置。
答案 0 :(得分:63)
您可以为signing config
中的每个flavor
声明buildType
。这是我的gradle文件,用于发布使用不同密钥库的签名风格。
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
块应放在productFlavors
块之后,我的意思是订单很重要。
答案 1 :(得分:44)
根据user guide,支持flavorConfigs。
这里的问题与signingConfigs对象的范围有关。我刚刚将它分配给productFlavors
块内的变量,但在flavor1
flavor块之外以解决问题:
productFlavors {
def flavor1SigningVariable = signingConfigs.flavor1
flavor1 {
...
signingConfig flavor1SigningVariable
...
}
答案 2 :(得分:5)
android的gradle插件仅支持每个构建类型的签名,而不是每个版本。原因是任何给定的变体(构建类型+风格)只能由一个键签名,但可以是几个flavor组的组合。例如,您的风味组可以是cpu(x86 / arm)和版本(免费/付费),这就是四种不同的变体。
您正在寻找的解决方案是为您的不同版本创建单独的构建类型。例如,您的构建类型可能是debug
,release
,release-beta
,如下所示:
...
android {
...
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
signingConfig signingConfigs.release
}
release-beta {
initWith release
signingConfig signingConfigs.release-beta
}
}
}
上面的initWith
只是告诉gradle release-beta
应该是release
构建类型的副本,只能使用其他密钥签名。
答案 3 :(得分:0)
.gitignore
...
/keystore.properties
.keystore.properties
storeFile=../mystore.jks
storePassword=...
keyAliasFlavor1=...
keyPasswordFlavor1=...
keyAliasFlavor2=...
keyPasswordFlavor2=...
app/build.gradle
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(rootProject.file('keystore.properties')))
android {
...
buildTypes {
debug {
...
manifestPlaceholders = [appNameSuffix: " Dev"]
applicationIdSuffix ".dev"
}
release {
...
manifestPlaceholders = [appNameSuffix: ""]
productFlavors.all { flavor ->
flavor.signingConfig = android.signingConfigs.create("${flavor.name}")
flavor.signingConfig.storeFile = rootProject.file(keystoreProperties["storeFile"])
flavor.signingConfig.storePassword = keystoreProperties["storePassword"]
flavor.signingConfig.keyAlias = keystoreProperties["keyAlias${flavor.name}"]
flavor.signingConfig.keyPassword = keystoreProperties["keyPassword${flavor.name}"]
}
}
}
productFlavors {
Flavor1 {
applicationId "..."
manifestPlaceholders = [appNameBase: "MyApp 1"]
}
Flavor2 {
applicationId "..."
manifestPlaceholders = [appNameBase: "MyApp 2"]
}
// ... and many other flavors without taking care about signing configs
// (just add two lines into keystore.properties for each new flavor)
}
}
app/src/main/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
...
<application android:label="${appNameBase}${appNameSuffix}" ...>
...
</application>
</manifest>