3

This is project level gradle.build file:

 buildscript {
    ext {
        server_test = 'http://192.618..'
        server_main = "https://68.5..."
        another_value = "test"
    }

    ext.kotlin_version = '1.3.41'

....
....

I'm using these values in my app level gradle.build file.

Is there ay way to access these variables in my Java Project files also!.

pz64_
  • 2,212
  • 2
  • 20
  • 43

2 Answers2

1

using buildConfigFields in gradle (app level, not root level), you can :)

buildConfigField "String", "variable_name", "variable_value"

as a complete example :

  productFlavors {

    dev {
        dimension "your_dimension_name"

buildConfigField "String", "SERVER_TEST", "\"http://192.618..\""
buildConfigField "String", "SERVER_MAIN", "\"https://68.5...\""
buildConfigField "String", "ANOTHER_VALUE", "test"

    }

you can then access these variables throughout your project by using :

      BuildConfig.SERVER_TEST

Have a look at this link for a more complete answer if this didn't help :

How to generate buildConfigField with String type

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
1

if you are trying to explicitly access gradle extensions in java code, the answer is no: it's not possible, see this : Android - Read build.gradle properties inside class, and also see this Is it possible to declare a variable in Gradle usable in Java?

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51