How to read hostname from meta-data when providing Retrofit in DI Modules?











up vote
0
down vote

favorite












I am using Dagger 2 + Retrofit to implement my interfaces which sends/receives data to/from my web service



I am referring Philippe BOISNEY's AppModule.java as below



private static String BASE_URL = "https://api.github.com/";

@Provides
Gson provideGson() { return new GsonBuilder().create(); }

@Provides
Retrofit provideRetrofit(Gson gson) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(BASE_URL)
.build();
return retrofit;
}


However I have a question that what if I have multiple hosts of my web services, such as Production, Staging and Development?



I already setup those different hosts connected to Build Config in my AndroidManifest.xml, but I don't have an idea how to read meta-data in AppModule, in order to replace BASE_URL with corresponding build config



Please kindly advice me, thank you










share|improve this question


























    up vote
    0
    down vote

    favorite












    I am using Dagger 2 + Retrofit to implement my interfaces which sends/receives data to/from my web service



    I am referring Philippe BOISNEY's AppModule.java as below



    private static String BASE_URL = "https://api.github.com/";

    @Provides
    Gson provideGson() { return new GsonBuilder().create(); }

    @Provides
    Retrofit provideRetrofit(Gson gson) {
    Retrofit retrofit = new Retrofit.Builder()
    .addConverterFactory(GsonConverterFactory.create(gson))
    .baseUrl(BASE_URL)
    .build();
    return retrofit;
    }


    However I have a question that what if I have multiple hosts of my web services, such as Production, Staging and Development?



    I already setup those different hosts connected to Build Config in my AndroidManifest.xml, but I don't have an idea how to read meta-data in AppModule, in order to replace BASE_URL with corresponding build config



    Please kindly advice me, thank you










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am using Dagger 2 + Retrofit to implement my interfaces which sends/receives data to/from my web service



      I am referring Philippe BOISNEY's AppModule.java as below



      private static String BASE_URL = "https://api.github.com/";

      @Provides
      Gson provideGson() { return new GsonBuilder().create(); }

      @Provides
      Retrofit provideRetrofit(Gson gson) {
      Retrofit retrofit = new Retrofit.Builder()
      .addConverterFactory(GsonConverterFactory.create(gson))
      .baseUrl(BASE_URL)
      .build();
      return retrofit;
      }


      However I have a question that what if I have multiple hosts of my web services, such as Production, Staging and Development?



      I already setup those different hosts connected to Build Config in my AndroidManifest.xml, but I don't have an idea how to read meta-data in AppModule, in order to replace BASE_URL with corresponding build config



      Please kindly advice me, thank you










      share|improve this question













      I am using Dagger 2 + Retrofit to implement my interfaces which sends/receives data to/from my web service



      I am referring Philippe BOISNEY's AppModule.java as below



      private static String BASE_URL = "https://api.github.com/";

      @Provides
      Gson provideGson() { return new GsonBuilder().create(); }

      @Provides
      Retrofit provideRetrofit(Gson gson) {
      Retrofit retrofit = new Retrofit.Builder()
      .addConverterFactory(GsonConverterFactory.create(gson))
      .baseUrl(BASE_URL)
      .build();
      return retrofit;
      }


      However I have a question that what if I have multiple hosts of my web services, such as Production, Staging and Development?



      I already setup those different hosts connected to Build Config in my AndroidManifest.xml, but I don't have an idea how to read meta-data in AppModule, in order to replace BASE_URL with corresponding build config



      Please kindly advice me, thank you







      android retrofit dagger






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 10:35









      holy934

      328




      328
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          You can define in build.gradle several flavor types like dev, prod and stage and for each flavor define build config variable



          productFlavors {
          dev {
          buildConfigField "String", "SERVER_URL", ""your dev url""
          }
          stage {
          buildConfigField "String", "SERVER_URL", ""your stage url""
          }
          prod {
          buildConfigField "String", "SERVER_URL", ""your prod url""
          }
          }


          And after that use it



          private static String BASE_URL = BuildConfig.SERVER_URL;


          If you like to provide it dynamically using dagger, you can do it in that way



          @Module
          public class AppModule {
          @Named("server_url")
          @Provides
          String provideServerUrl() {
          return "https://api.github.com/";
          }

          @Provides
          Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
          Retrofit retrofit = new Retrofit.Builder()
          .addConverterFactory(GsonConverterFactory.create(gson))
          .baseUrl(url)
          .build();
          return retrofit;
          }
          }


          Another way of dynamically providing server url using dagger - using builder. For example,



          @Component(AppModule.class)
          interface AppComponent {
          @Component.Builder
          interface Builder {
          @BindsInstance
          Builder serverUrl(@Named("server_url") String url);

          AppComponent build();
          }
          }

          @Module
          public class AppModule {
          @Provides
          Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
          Retrofit retrofit = new Retrofit.Builder()
          .addConverterFactory(GsonConverterFactory.create(gson))
          .baseUrl(url)
          .build();
          return retrofit;
          }
          }

          DaggerAppComponent.Builder()
          .appModule(new AppModule())
          .serverUrl("https://api.github.com/")
          .build();





          share|improve this answer























          • Really appreciate @ConstOrVar suggestion so that I probably know how to correct my codes as my own answer
            – holy934
            Nov 9 at 2:36


















          up vote
          0
          down vote













          I give up reading corresponding host from meta-data in AndroidManefist, instead, I switch to another approach by referring @ConstOrVar's suggestion



          Here is my app/build.gradle, I have 2 flavors which describes my staging and production host respectively



          productFlavors {
          production {
          buildConfigField("String", "SERVER_HOST", ""myproductionost.com"")
          }
          staging {
          buildConfigField("String", "SERVER_HOST", ""mystaginghost.com"")
          }
          }


          Then here is my codes in my AppModule.java



          @Module
          public class AppModule {

          @Provides
          Retrofit provideRetrofit(Gson gson) {
          Retrofit retrofit = new Retrofit.Builder()
          .addConverterFactory(GsonConverterFactory.create(gson))
          .baseUrl("https://" + BuildConfig.SERVER_HOST)
          .build();
          return retrofit;
          }
          }


          By doing so, Retrofit will tells which host of webservice it shall talk with based on BuildVariant automatically






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205937%2fhow-to-read-hostname-from-meta-data-when-providing-retrofit-in-di-modules%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            You can define in build.gradle several flavor types like dev, prod and stage and for each flavor define build config variable



            productFlavors {
            dev {
            buildConfigField "String", "SERVER_URL", ""your dev url""
            }
            stage {
            buildConfigField "String", "SERVER_URL", ""your stage url""
            }
            prod {
            buildConfigField "String", "SERVER_URL", ""your prod url""
            }
            }


            And after that use it



            private static String BASE_URL = BuildConfig.SERVER_URL;


            If you like to provide it dynamically using dagger, you can do it in that way



            @Module
            public class AppModule {
            @Named("server_url")
            @Provides
            String provideServerUrl() {
            return "https://api.github.com/";
            }

            @Provides
            Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
            Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl(url)
            .build();
            return retrofit;
            }
            }


            Another way of dynamically providing server url using dagger - using builder. For example,



            @Component(AppModule.class)
            interface AppComponent {
            @Component.Builder
            interface Builder {
            @BindsInstance
            Builder serverUrl(@Named("server_url") String url);

            AppComponent build();
            }
            }

            @Module
            public class AppModule {
            @Provides
            Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
            Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl(url)
            .build();
            return retrofit;
            }
            }

            DaggerAppComponent.Builder()
            .appModule(new AppModule())
            .serverUrl("https://api.github.com/")
            .build();





            share|improve this answer























            • Really appreciate @ConstOrVar suggestion so that I probably know how to correct my codes as my own answer
              – holy934
              Nov 9 at 2:36















            up vote
            2
            down vote



            accepted










            You can define in build.gradle several flavor types like dev, prod and stage and for each flavor define build config variable



            productFlavors {
            dev {
            buildConfigField "String", "SERVER_URL", ""your dev url""
            }
            stage {
            buildConfigField "String", "SERVER_URL", ""your stage url""
            }
            prod {
            buildConfigField "String", "SERVER_URL", ""your prod url""
            }
            }


            And after that use it



            private static String BASE_URL = BuildConfig.SERVER_URL;


            If you like to provide it dynamically using dagger, you can do it in that way



            @Module
            public class AppModule {
            @Named("server_url")
            @Provides
            String provideServerUrl() {
            return "https://api.github.com/";
            }

            @Provides
            Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
            Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl(url)
            .build();
            return retrofit;
            }
            }


            Another way of dynamically providing server url using dagger - using builder. For example,



            @Component(AppModule.class)
            interface AppComponent {
            @Component.Builder
            interface Builder {
            @BindsInstance
            Builder serverUrl(@Named("server_url") String url);

            AppComponent build();
            }
            }

            @Module
            public class AppModule {
            @Provides
            Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
            Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl(url)
            .build();
            return retrofit;
            }
            }

            DaggerAppComponent.Builder()
            .appModule(new AppModule())
            .serverUrl("https://api.github.com/")
            .build();





            share|improve this answer























            • Really appreciate @ConstOrVar suggestion so that I probably know how to correct my codes as my own answer
              – holy934
              Nov 9 at 2:36













            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            You can define in build.gradle several flavor types like dev, prod and stage and for each flavor define build config variable



            productFlavors {
            dev {
            buildConfigField "String", "SERVER_URL", ""your dev url""
            }
            stage {
            buildConfigField "String", "SERVER_URL", ""your stage url""
            }
            prod {
            buildConfigField "String", "SERVER_URL", ""your prod url""
            }
            }


            And after that use it



            private static String BASE_URL = BuildConfig.SERVER_URL;


            If you like to provide it dynamically using dagger, you can do it in that way



            @Module
            public class AppModule {
            @Named("server_url")
            @Provides
            String provideServerUrl() {
            return "https://api.github.com/";
            }

            @Provides
            Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
            Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl(url)
            .build();
            return retrofit;
            }
            }


            Another way of dynamically providing server url using dagger - using builder. For example,



            @Component(AppModule.class)
            interface AppComponent {
            @Component.Builder
            interface Builder {
            @BindsInstance
            Builder serverUrl(@Named("server_url") String url);

            AppComponent build();
            }
            }

            @Module
            public class AppModule {
            @Provides
            Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
            Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl(url)
            .build();
            return retrofit;
            }
            }

            DaggerAppComponent.Builder()
            .appModule(new AppModule())
            .serverUrl("https://api.github.com/")
            .build();





            share|improve this answer














            You can define in build.gradle several flavor types like dev, prod and stage and for each flavor define build config variable



            productFlavors {
            dev {
            buildConfigField "String", "SERVER_URL", ""your dev url""
            }
            stage {
            buildConfigField "String", "SERVER_URL", ""your stage url""
            }
            prod {
            buildConfigField "String", "SERVER_URL", ""your prod url""
            }
            }


            And after that use it



            private static String BASE_URL = BuildConfig.SERVER_URL;


            If you like to provide it dynamically using dagger, you can do it in that way



            @Module
            public class AppModule {
            @Named("server_url")
            @Provides
            String provideServerUrl() {
            return "https://api.github.com/";
            }

            @Provides
            Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
            Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl(url)
            .build();
            return retrofit;
            }
            }


            Another way of dynamically providing server url using dagger - using builder. For example,



            @Component(AppModule.class)
            interface AppComponent {
            @Component.Builder
            interface Builder {
            @BindsInstance
            Builder serverUrl(@Named("server_url") String url);

            AppComponent build();
            }
            }

            @Module
            public class AppModule {
            @Provides
            Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
            Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl(url)
            .build();
            return retrofit;
            }
            }

            DaggerAppComponent.Builder()
            .appModule(new AppModule())
            .serverUrl("https://api.github.com/")
            .build();






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 8 at 18:07

























            answered Nov 8 at 13:13









            ConstOrVar

            91446




            91446












            • Really appreciate @ConstOrVar suggestion so that I probably know how to correct my codes as my own answer
              – holy934
              Nov 9 at 2:36


















            • Really appreciate @ConstOrVar suggestion so that I probably know how to correct my codes as my own answer
              – holy934
              Nov 9 at 2:36
















            Really appreciate @ConstOrVar suggestion so that I probably know how to correct my codes as my own answer
            – holy934
            Nov 9 at 2:36




            Really appreciate @ConstOrVar suggestion so that I probably know how to correct my codes as my own answer
            – holy934
            Nov 9 at 2:36












            up vote
            0
            down vote













            I give up reading corresponding host from meta-data in AndroidManefist, instead, I switch to another approach by referring @ConstOrVar's suggestion



            Here is my app/build.gradle, I have 2 flavors which describes my staging and production host respectively



            productFlavors {
            production {
            buildConfigField("String", "SERVER_HOST", ""myproductionost.com"")
            }
            staging {
            buildConfigField("String", "SERVER_HOST", ""mystaginghost.com"")
            }
            }


            Then here is my codes in my AppModule.java



            @Module
            public class AppModule {

            @Provides
            Retrofit provideRetrofit(Gson gson) {
            Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl("https://" + BuildConfig.SERVER_HOST)
            .build();
            return retrofit;
            }
            }


            By doing so, Retrofit will tells which host of webservice it shall talk with based on BuildVariant automatically






            share|improve this answer

























              up vote
              0
              down vote













              I give up reading corresponding host from meta-data in AndroidManefist, instead, I switch to another approach by referring @ConstOrVar's suggestion



              Here is my app/build.gradle, I have 2 flavors which describes my staging and production host respectively



              productFlavors {
              production {
              buildConfigField("String", "SERVER_HOST", ""myproductionost.com"")
              }
              staging {
              buildConfigField("String", "SERVER_HOST", ""mystaginghost.com"")
              }
              }


              Then here is my codes in my AppModule.java



              @Module
              public class AppModule {

              @Provides
              Retrofit provideRetrofit(Gson gson) {
              Retrofit retrofit = new Retrofit.Builder()
              .addConverterFactory(GsonConverterFactory.create(gson))
              .baseUrl("https://" + BuildConfig.SERVER_HOST)
              .build();
              return retrofit;
              }
              }


              By doing so, Retrofit will tells which host of webservice it shall talk with based on BuildVariant automatically






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                I give up reading corresponding host from meta-data in AndroidManefist, instead, I switch to another approach by referring @ConstOrVar's suggestion



                Here is my app/build.gradle, I have 2 flavors which describes my staging and production host respectively



                productFlavors {
                production {
                buildConfigField("String", "SERVER_HOST", ""myproductionost.com"")
                }
                staging {
                buildConfigField("String", "SERVER_HOST", ""mystaginghost.com"")
                }
                }


                Then here is my codes in my AppModule.java



                @Module
                public class AppModule {

                @Provides
                Retrofit provideRetrofit(Gson gson) {
                Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .baseUrl("https://" + BuildConfig.SERVER_HOST)
                .build();
                return retrofit;
                }
                }


                By doing so, Retrofit will tells which host of webservice it shall talk with based on BuildVariant automatically






                share|improve this answer












                I give up reading corresponding host from meta-data in AndroidManefist, instead, I switch to another approach by referring @ConstOrVar's suggestion



                Here is my app/build.gradle, I have 2 flavors which describes my staging and production host respectively



                productFlavors {
                production {
                buildConfigField("String", "SERVER_HOST", ""myproductionost.com"")
                }
                staging {
                buildConfigField("String", "SERVER_HOST", ""mystaginghost.com"")
                }
                }


                Then here is my codes in my AppModule.java



                @Module
                public class AppModule {

                @Provides
                Retrofit provideRetrofit(Gson gson) {
                Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .baseUrl("https://" + BuildConfig.SERVER_HOST)
                .build();
                return retrofit;
                }
                }


                By doing so, Retrofit will tells which host of webservice it shall talk with based on BuildVariant automatically







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 9 at 3:13









                holy934

                328




                328






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205937%2fhow-to-read-hostname-from-meta-data-when-providing-retrofit-in-di-modules%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Schultheiß

                    Liste der Kulturdenkmale in Wilsdruff

                    Android Play Services Check