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?

4
  • 2
    Don't you have a BuildConfig class that was generated? You can access the values through there. See this: stackoverflow.com/questions/21365928/… or this: developer.android.com/studio/build/gradle-tips
    – pringi
    Feb 16, 2022 at 15:54
  • Actually I have zero idea what are you talking about. you can offer me a demo as an answer. Feb 16, 2022 at 15:55
  • literally type BuildConfig. and see if your variable shows up Feb 16, 2022 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 basically
    – romtsn
    Feb 16, 2022 at 16:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.