简体   繁体   中英

How to create different build variants pointing to different Servers?

I am using gradle.build for auto building my app. I want to generate three different APK's each pointing to different Service URL's.

How can I make use of buildVariants (productFlavors in gradle). But I'm not able to figure out where to set the three URL's in the Gradle.

How can I do this?

It is really easy to do with gradle.

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

You may want to find more information here .

So later you can easy access this variable by BuildConfig.SERVER_URL

You can use as like following,

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 folder structure In App:

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

If MainActivity is required to different logic then, that MainActivity is place in serverone, servertwo and serverthree folder remaining classes are keep in main folder only. And do diffent functionality on that Activities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM