0

I have created 4 different flavours in build variants in my app. I have added version and suffix in the configuration in my app's build.gradle.. Below is the code:

productFlavors {
    qa {
        applicationIdSuffix ".qa"
        versionCode 1
        versionName "1.0"
        //manifestPlaceholders = [application: ".utils.Application"]
    }

    demo {
        applicationIdSuffix ".demo"
        versionCode 1
        versionName "1.0"
        //manifestPlaceholders = [application: ".utils.Application"]
    }


    dev {

        versionCode 1
        versionName "1.0"
        //manifestPlaceholders = [application: ".utils.Application"]
        //signingConfig signingConfigs.release
    }

    uat {
        applicationIdSuffix ".uat"
        versionCode 1
        versionName "1.0"
        //manifestPlaceholders = [application: ".utils.Application"]
    }
}

In my app, I use a file APIAddresses.java for selecting the URLs to hit according to the environment. Is there any way I can use only this file out of all the source code in build.gradle only to configure URLs for build variants?

May be create different copies of this file for each build variant, something similar to this.

productFlavors {
    qa {
        applicationIdSuffix ".qa"
        versionCode 1
        versionName "1.0"
        APIAddresses_QA
    }

    demo {
        applicationIdSuffix ".demo"
        versionCode 1
        versionName "1.0"
        APIAddresses_DEMO
    }

}

Thank you!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
The Bat
  • 1,085
  • 1
  • 13
  • 31
  • Add your different java file in flavor based folder structure and you can used it accordingly. – Abhishek Patel Feb 16 '18 at 10:41
  • Possible duplicate of [Using Build Flavors - Structuring source folders and build.gradle correctly](https://stackoverflow.com/questions/16737006/using-build-flavors-structuring-source-folders-and-build-gradle-correctly) – lelloman Feb 16 '18 at 10:47

2 Answers2

0

Yes you can use one version of your file APIAddresses.java for all your variants.

In your build.gradle, add

flavorDimensions "main"

Put your APIAddresses.java file in the app\src\main directory.

In your code, you can then generate different URLs for each variant using the variable

BuildConfig.FLAVOR

matdev
  • 4,115
  • 6
  • 35
  • 56
0

If you don't want to add lot of if-else conditions in your classes, you can add new buildConfigField to each flavor:

productFlavors {
    qa {
        applicationIdSuffix ".qa"
        versionCode 1
        versionName "1.0"
        buildConfigField "String", "SERVER_URL", '"https://server1.com"'
    }

    demo {
        applicationIdSuffix ".demo"
        versionCode 1
        versionName "1.0"
        buildConfigField "String", "SERVER_URL", '"https://server2.com"'
    }

    ...
}

After that you can call BuildConfig.SERVER_URL on Java side, and according to each flavor, it should return proper URL in my example

Natig Babayev
  • 3,128
  • 15
  • 23