2

I have a string in res/strings.xml

<string name="clientId">dfgljkwm51</string>

I want to use the string inside my retrofit interface class:


public class RetrofitInterfaces {
        public interface GetAlbum{
             @Headers("Authorization: Client-ID " + clientId) //Want to use the String valye here.
             @GET
             Call<Feed> listRepos(@Url String url);
        }
}

This is how im using the retrofit call:

 private void makeNetworkCall(String url) {
        RetrofitInterfaces.GetAlbum service = RetrofitClientInstance.getRetrofitInstance()
                .create(RetrofitInterfaces.GetAlbum.class);
        Call<Feed> call = service.listRepos(url);
        call.enqueue(new Callback<Feed>() {
            . . . 
}

How do I pass this value into my Retrofit interfaces class so that I can use the value in the headers?

DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83

3 Answers3

4

strings.xml is there for localization, you probably shouldn't be using it to store api key values.

You can instead use BuildConfig fields :

buildConfigField "String", "API_KEY", '"key"'

Which you can then access anywhere with BuildConfig.API_KEY

Complete example, this is in your Build.Gradle(:app)

defaultConfig {
        applicationId "your app"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode buildVersionCode
        versionName buildVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        buildConfigField "String", "API_KEY", '"api key here"'
    }

BuildConfig fields have the additional benefit of providing a way of keeping your source code safe when uploading it as open source, by allowing you to read your key into a build config variable from an external file

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
  • I've found the BuildConfig folder however it says "Files under the "build" folder are generated and should not be edited". https://i.imgur.com/uubkt1X.png – DIRTY DAVE Nov 02 '20 at 10:11
  • you shouldn't and can't add or edit anything in there, make these changes in your build.gradle(:app) @DIRTYDAVE – a_local_nobody Nov 02 '20 at 10:11
  • Interesting, what are the pros and cons of using buildConfigField versus a constant class like the answer above? https://stackoverflow.com/a/64643448/11110509 – DIRTY DAVE Nov 02 '20 at 10:15
  • you can change the BuildConfig value per variant of your app. if you have a premium version/flavour, you can define the premium key, if you have a test app, you can define a test key, but in code you'll always just reference `BuildConfig.API_KEY` – a_local_nobody Nov 02 '20 at 10:17
  • 1
    see [here](https://stackoverflow.com/a/42601391/4729721) @DIRTYDAVE – a_local_nobody Nov 02 '20 at 10:18
  • Thanks, didn't know this. Is it safer to store it in the defaultConfig compared to creating a Constant Java class? Or does it not really matter? – DIRTY DAVE Nov 02 '20 at 10:21
  • 1
    doesn't matter, if this is a project you're going to publish as open source, this isn't the correct approach to go with, you should rather be reading it from a file, see [here](https://stackoverflow.com/q/33134031/4729721). basically, store the key in a file and read it from that file when your app gets compiled using gradle, then you don't publish the file containing the key where you upload your code, so others don't have it, but we're going off-topic here :) – a_local_nobody Nov 02 '20 at 10:24
  • Ahhh okay so thats how they do it! Thanks for answering all my questions I appreciate it. – DIRTY DAVE Nov 02 '20 at 10:28
4

Make a class for constant values

public class Constants {

public static final String key = "as23rsffwr2";

}

Access like this

Constants.key
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Haseeb Mirza
  • 432
  • 2
  • 7
0

I would just create a constant as Haseeb Mirza mentioned. A build config field can be useful when you need to have a different client id in a different environment.

  • 1
    with a buildconfig field, you can [keep your key hidden](https://stackoverflow.com/a/33136057/4729721) if you're publishing your code, so that's an extra benefit to using it – a_local_nobody Nov 02 '20 at 10:35