I know how to create a buildConfigField variable and set its value in my build.gradle
file. E.g. buildConfigField 'String', 'BUILD_VALUE', 'HELLO WORLD'
.
I also know how to pass in an argument from gradle command line via task. For instance, if I do ./gradleW propertyTypes -Dargs=test1 -Pargs=test2
in terminal and have the following in my build.gradle
:
task propertyTypes(){
doLast{
if (project.hasProperty("args")) {
println "Project property ["+project.getProperty("args")+"]"
}
println "System property ["+System.getProperty("args")+"]"
}
}
it will output test2
and test1
to terminal.
However, I don't know how to put them together. I want to be able to pass in an argument (e.g. "Hello World") via the gradle commandline and then have that set as a buildConfigField for use in the program itself. When I try though, either task doesn't know what a buildConfigField is or the buildConfigField doesn't know the properties passed into the task.
Any insight on how I can make this work?
android { android.applicationVariants.all { variant -> variant.buildConfigField "String", 'BUILD_VALUE', "\"${getArgsValueSomehow()}\"" } }
wheregetArgsValueSomehow
returns some string ? – SaudergetArgsValueSomehow
-ness of it. – Red