19

I have some configuration I want to save it in my Android application and read it whenever I need , for instance, the server URL that it should try to access like that.

Is there any similar mechanism like web.config in ASP.NET available in Android?

A central configuration file that can be set up manually and then read by the application? Any help would be appreciated!

Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
JSBach
  • 4,679
  • 8
  • 51
  • 98
  • If you mean this link: http://stackoverflow.com/questions/5140539/android-config-file I have already read it, but I don't want to use shared preferences – JSBach Jun 10 '11 at 19:59
  • Possible duplicate of [Android Config File](http://stackoverflow.com/questions/5140539/android-config-file) – Eduardo Cuomo Feb 12 '17 at 00:49

5 Answers5

24

We use a .properties file in assets folder. It works out very well for us as we support multiple carriers with this, write to it (in case some values, sent from server, need to change. This is done at app start time, thus making our code configurable from server).

omermuhammed
  • 7,365
  • 4
  • 27
  • 40
  • 11
    Deleted old comment asking for code snippet, as I found one pretty easily. For anyone wanting an example of this, check out http://myossdevblog.blogspot.com/2010/02/reading-properties-files-on-android.html – Hovis Biddle Feb 15 '12 at 23:43
  • The only problem with this is that the config-file can not be changed/replaced/altered without also deploying the application anew, right? It's inside the .apk – avalancha Oct 28 '15 at 11:19
  • 1
    I thought I should share a few other solutions which I've answered in this question: http://stackoverflow.com/a/35527922/2657607 – grim Feb 20 '16 at 20:45
2

You can throw things like that into your strings.xml file. But, since you can't actually modify these values in real-time (since it's a distributed application rather than running on a server), throwing it into a constants class is quite acceptable.

Haphazard
  • 10,900
  • 6
  • 43
  • 55
  • Right, as you can modify property files on servers. You can't do that in Android apps since they aren't on your own machine so constants files should be fine. – Haphazard Jun 10 '11 at 19:55
  • By "can be set up manually", I assume he means before he uploads it to the market. If he states otherwise, I'll retract my suggestion. – Haphazard Jun 10 '11 at 19:57
  • Hi, thanks! I would like to do something "cleaner" than a constant class. But this may work too! :) – JSBach Jun 10 '11 at 20:00
1

You can use sq lite database files for it. You have a native API to read and write those and on top of that a command line tool. If you want to create an XML file instead, then it's no different than any other xml file (unless you are thinking about the Shared Preferences, which use an xml format to save the data, but I believe it's not the best API for your application).

Syed Ali
  • 279
  • 1
  • 4
  • 15
1

Use Shared Preferences.
Here's a link Shared Preferences

Fredrik
  • 1,282
  • 12
  • 15
  • 3
    Hi, thanks for your answer. I've read about it, but if I got it right, it is not a "file", it is some structure that I can't modify manually, right? – JSBach Jun 10 '11 at 19:58
0

I was stumped on this too, but came across Managed Configurations in the Android documentation.

Managed configurations, previously known as application restrictions, allow the enterprise administrator to remotely specify settings for apps. This capability is particularly useful for enterprise-approved apps deployed to a managed profile.

It allows you to set a default value in case you rather not getting into the enterprise admistration business but leaves that option open for the future.

There is a caveat. This only works if your app is registered for EMM. Otherwise you will retrieve an empty map of restrictions.

Omar Abdel Bari
  • 934
  • 9
  • 17