androidgradlebuild.gradlegradle-taskandroid-buildconfig

Android Gradle: call buildConfigield inside gradle task or method


I'm trying to add BuildConfig variable using gradle. I see that buildConfigField is valid when I call it on gradle buildTypes: release/debug and also on defaultConfig

but I'm trying to create this inside gradle task. for example:

task myTask{
    buildConfigField 'String', 'Test', '"value"'
} 

and then call my task. but in that case when I call myTask, gradle is failed and I'm getting:

"could not find method buildConfigField" 

is there any option to call buildConfigField and make it work inside task?


Solution

  • Thanks to @Skenia suggesting this post

    I was able to find a solution, using the answer of Marian Klühspies there.

    so for anyone who encounter this in the future, you can use this

    android.defaultConfig.buildConfigField "String", "value", "1"
    

    inside tasks and this will work