eko eko - 8 days ago 6
Android Question

Android studio - build and run configurations

I'd like to have different configurations for debug and release versions. For the most part, By configuration I mean having different string constants, e.g. connection strings. Additionally I'd like to have a running configuration to be connected to a build configuration, so that when I select 'release' from the running dropdown, the correct version is automatically built. Is that even possible? Is there a way to use a different string resource file based on build configuration?

Answer

There is a product flavors functionality available in android studio. You have to add different flavors for your application in the app level build.gradle file. You can set them as follows:

  productFlavors {
    sandbox {
        versionCode 1
        versionName "1.0"
        applicationId "com.abc.sandbox"
        buildConfigField 'String', 'HOST', '"http://api/v1/"'
    }

    development {
        versionCode 1
        versionName "1.0"
        applicationId "com.abc.development"
        buildConfigField 'String', 'HOST', '"http://api/v1/"'
    }

    production {
        versionCode 1
        versionName "1.0"
        applicationId "com.abc.production"
        buildConfigField 'String', 'HOST', '"http://api/v1/"'
    }

}

You can run respective flavor by selecting it from build versions before running your application.