How to set buildConfigField parameters dynamically

2019-02-15 14:12发布

问题:

I have following fields in ProductFlavors of build.gradle,

productFlavors {

    Flavor1 {
        applicationId "com.example.A"
        buildConfigField 'int', 'ID', '123'
    }
}

How can i update these 2 fields according to inputs given from Jenkins.?

Thanks in advance!!

回答1:

You can use following steps:

In your app level build.gradle:

buildscript {
    ext{
        appId="com.example.A"
        Id=123 
    }
    ...
}

change your fields as follows:

productFlavors {

    Flavor1 {
        applicationId appId
        buildConfigField 'int', 'ID', "$Id"
    }
}

From jenkins pass the parameters:

gradlew assesmbleFlavor1 -PappId="${APPLICATION_ID}" -PId="${ID}"

Where ${APPLICATION_ID} and ${ID} are parameters defined in jenkins