0

As seen in oficial android documentation I can use build variants and build my application via different settings.

Therefore, I made my own variants:

android {
    defaultConfig {
        manifestPlaceholders = [hostName:"www.example.com"]
        ...
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        debug {
            applicationIdSuffix ".debug"
            manifestPlaceholders = [hostName:"local.example.com"]
            debuggable true
        }

        /**
         * The `initWith` property allows you to copy configurations from other build types,
         * then configure only the settings you want to change. This one copies the debug build
         * type, and then changes the manifest placeholder and application ID.
         */
        staging {
            initWith debug
            manifestPlaceholders = [hostName:"staging.example.com"]
            applicationIdSuffix ".debugStaging"
        }
    }
}

And I have a class where I load all the settings:


class ApiUtils {

   public function getApiUrl(String path){
      // I want to populate it with appropriate url from build config
      String url="";
      return url+path;
   }
}

How I can populate the url variable with the value from hostName as provided in manifestPlaceholders?

Dimitrios Desyllas
  • 9,082
  • 15
  • 74
  • 164
  • 2
    Don't you have a BuildConfig class that was generated? You can access the values through there. See this: https://stackoverflow.com/questions/21365928/gradle-how-to-use-buildconfig-in-an-android-library-with-a-flag-that-gets-set or this: https://developer.android.com/studio/build/gradle-tips – pringi Feb 16 '22 at 15:54
  • Actually I have zero idea what are you talking about. you can offer me a demo as an answer. – Dimitrios Desyllas Feb 16 '22 at 15:55
  • literally type `BuildConfig.` and see if your variable shows up – a_local_nobody Feb 16 '22 at 16:02
  • Yeah as mentioned, `manifestPlaceholders` is to inject values into AndroidManifest, if you want to use your values from build scripts in code, you need to use BuildConfig, so something like [this](https://stackoverflow.com/a/30796601/3225458) basically – romtsn Feb 16 '22 at 16:44

0 Answers0