Passing -P parameters to gradle from android studio

For example let's try to pass server base endpoint while building project. First we need to define a method which gets url and sets it

def serverUrl = "https://mydefaulturl.com"
task(runProgram){
   if(project.hasProperty("url")){
       serverUrl = url;
   }
}

In your buildTypes create your base url with this value:

debug{
   buildConfigField("java.lang.String","BASE_URL","$serverUrl")
}

Now we can pass debug build an url and use it.

You can build your project from terminal with the command below:

./gradlew installDebug task runProgram '-Purl="https://yournewurl.com"'

Run > Edit Configurations..

enter image description here

Replace "abcdef" in Script parameters by your params.


In order to provide parameter to run option of Android Studio you should setup them under AndroidStudios Compile Preferences

AndroidStudio > Preferences...

Under Compiler: ComandLineOptions enter image description here