How do I create different build variants pointing to different servers?

I am using gradle.build to auto build my app. I want to create three different APKs, each pointing to a different service url.

How can I use buildVariants (productFlavors in gradle). But I cannot figure out where to set the three urls in gradle.

How can i do this?

+3


source to share


2 answers


It's really easy to do this with gradle.

productFlavors {
    first_server {
        buildConfigField "String", "SERVER_URL", "\"https://first_server_url/\""
    }
    second_server {
        buildConfigField "String", "SERVER_URL", "\"https://second_server_url/\""
    }
}

      



You can find more information here .

So, you can easily access this variable with BuildConfig.SERVER_URL

+4


source


You can use as shown below,

In Gradle:

productFlavors{
        serverone {
            applicationId "com.example.krishna.mysample.serverone"
            version 1.1
        }

        servertwo {
            applicationId "com.example.krishna.mysample.servertwo"
            version 1.1
        }
        serverthree {
            applicationId "com.example.krishna.mysample.serverthree"
            version 1.1
        }
    }

      



In the folder structure in the application:

src
  -->main
  -->serverone
  -->servertwo
  -->serverthree

      

If different logic

MainActivity is required then MainActivity is only placed in serverone, servertwo and serverthree folder remaining classes are keep in main folder

. And perform various functions in this activity.

+3


source







All Articles