使用口味和尺寸进行Gradle签名

时间:2017-01-10 07:38:08

标签: android gradle

是否有人知道如何独立签署多种口味和尺寸的应用程序? 我试图检索任务的名称以提供签名配置,但它没有用。

这是我的gradle文件:

signingConfigs {
    vanillaPaid {
        ...
    }
    vanillaFree {
        ...
    }
    chocolatePaid {
        ...
    }
    chocolateFree {
        ...
    }
}

flavorDimensions "taste", "type"

productFlavors {
    vanilla {
        dimension "taste"
        applicationIdSuffix ".vanilla"
    }
    chocolate {
        dimension "taste"
        applicationIdSuffix ".chocolate"
    }
    free {
        dimension "type"
        ...
    }
    paid {
        dimension "type"
        ...
    }
}

buildTypes {
    release {
        def taskNames = project.gradle.startParameter.taskNames
        if (taskNames.size() > 0) {
            if (taskNames.get(0).contains('VanillaPaid')) {
                signingConfig signingConfigs.vanillaPaid
            } else if (taskNames.get(0).contains('VanillaFree')) {
                signingConfig signingConfigs.vanillaFree
            } else if(...) {
                ...
            }
        }
        ...
    }
    debug {
        ...
    }
}

1 个答案:

答案 0 :(得分:0)

解决方案是:

signingConfigs {
    vanillaPaid {
        ...
    }
    vanillaFree {
        ...
    }
    chocolatePaid {
        ...
    }
    chocolateFree {
        ...
    }
}

flavorDimensions "taste", "type"

productFlavors {
    vanilla {
        dimension "taste"
        applicationIdSuffix ".vanilla"
    }
    chocolate {
        dimension "taste"
        applicationIdSuffix ".chocolate"
    }
    free {
        dimension "type"
        ...
    }
    paid {
        dimension "type"
        ...
    }
}

applicationVariants.all { variant ->
    if (variant.name.equals("vanillaPaidRelease")) {
        variant.mergedFlavor.setSigningConfig(signingConfigs.vanillaPaid)
    } else if (variant.name.equals("vanillaFreeRelease")) {
        variant.mergedFlavor.setSigningConfig(signingConfigs.vanillaFree)
    } else if (variant.name.equals("chocolatePaidRelease")) {
        variant.mergedFlavor.setSigningConfig(signingConfigs.chocolatePaid)
    } else if (variant.name.equals("chocolateFreeRelease")) {
        variant.mergedFlavor.setSigningConfig(signingConfigs.chocolateFree)
    }
}

buildTypes {
    release {
        ...
    }
    debug {
        ...
    }
}

如果需要,还可以设置应用程序标识符:

applicationVariants.all { variant ->
    if (variant.name.equals("vanillaFreeRelease")) {
        variant.mergedFlavor.setSigningConfig(signingConfigs.vanillaFree)
        variant.mergedFlavor.setApplicationId("com.domain.custom.id")
    } else if (...) {
        ...
    }
}