gkpln3
gkpln3

Reputation: 1499

Building two different android apps on the same codebase that only differ by const

I am building two android applications, which relay on the same codebase but differ by the server address which they grab their files from.

Till now i've created two line of my server's address constant, and builded the application once with the first const, and second with the second const.

Is there any way to make my app compile twice, once with the first constant, and second with the second one?

I am using Android studio with Gradle build.

Thank you!

Upvotes: 0

Views: 587

Answers (2)

CaseyB
CaseyB

Reputation: 25058

Yeah, you can use Build Variants. You can move those strings into resources under the variants directory and the build will pull in the right one.

Here's the link to the full documentation for how to set them up: https://developer.android.com/studio/build/build-variants.html

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006934

You can use product flavors to teach Gradle to build two separate copies of your app, where your server address is defined in BuildConfig:

android {
  // other stuff here

  productFlavors {
    flavorOne {
      buildConfigField "String", "URL", '"https://..."'
    }

    flavorTwo {
      buildConfigField "String", "URL", '"https://..."'
    }
  }
}

In your Java code, refer to BuildConfig.URL to get the URL to use.

Upvotes: 2

Related Questions