Nikki Tune Nikki Tune - 1 month ago 14
Android Question

How should I store API URLs in Android, if they are dependent on locale?

Right now they are just stored on a class called

Globals
as static fields.
I'm not the one who made the app, but I am considering putting them in localized
strings.xml
files, such as
<string name="API_URL">http://someurl</string>
. Is this good or bad practice?

UPDATE:
I chose the answer that I feel answers the question most comprehensibly.
But after some re-thinking, I have chosen a different solution alltogether.
Given that URLs are actually based on the country which the app should be distributed for, it doesn't make sense to switch them based on locale, as the URLs should stay the same regardless of the language on the phone.
Instead, I have implemented Gradle Flavors, which create different APKs based on different settings and such. It allows you to create variations of the same app with the small changes that you need. :) So now I have the URLs in a flavor-specific file.

Thank you to everyone who took their time to comment and help me.

Answer

I agree with puneet, it's neither good nor bad. It depends on what you are doing with the API Urls.

Are you going to append them later with user input? If so I would suggest you keep them as global variables that way you can modify the API URL programatically as needed.

If the API Url are complete and will not need to be appended then putting them in the strings.xml would be fine. Just remember that you would still have to create a local String variable in the java to hold the text from the API_URL in the string.xml, which seems inefficient if what you're aiming for is to write less code.

Comments