Home > database >  How to define a shared build variable?
How to define a shared build variable?

Time:04-11

I'm trying to set up custom variable for each build type.

My build.gradle file looks like this:

buildTypes {
    each {
        buildConfigField "string", "SHARED_URL", "https://stackoverflow.com/"
    }
    debug {
        buildConfigField "string", "PRIVATE_URL", "https://debugoverflow.com/"
    }
    release {
        buildConfigField "string", "PRIVATE_URL", "https://releaseoverflow.com/"

        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

And I want to have an access to the SHARED_URL in my code by final String sharedUrl = BuildConfig.SHARED_URL; both in the debug and in the release build. So, how can I achieve this goal?

P.S. I'm understand that I can just copy variable SHARED_URL in the both builds, but I don't want to boilerplating.

CodePudding user response:

You can put your common buildConfigField in defaultConfig:

android {
  defaultConfig {
    buildConfigField "string", "SHARED_URL", "https://stackoverflow.com/"
  }

  buildTypes {
      debug {
        buildConfigField "string", "PRIVATE_URL", "https://debugoverflow.com/"
      }
      release {
        buildConfigField "string", "PRIVATE_URL", "https://releaseoverflow.com/"

        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
      }
  }
}

SHARED_URL will now be available for all build variants.

  • Related