androidcodebaseandroid-flavors

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!


Solution

  • 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.