I developed a game on Android. I am currently saving most of the game stats in a Database. However the app does not utilize more than a single row in the DB. I am now interested in introducing some new stats but this will cause my DB to re-install and thus clear out everyone's progress. In order to avoid this in the future I am considering storing the game stats with SharedPreferences instead. My question is how many different things can be stored that way before it becomes a problem. In total I would be storing around 40 values, all integers.
-
40 integers are fine for storing in the SharedPreferences – Alexander Kulyakhtin Mar 28 '17 at 21:42
4 Answers
SharedPreferences are written to xml files, so the maximum size of a file on Android is how large a SharedPreferences xml file can be. I can safely say that 40 integer values will not be a problem.
The maximum size of a value in a SharedPreferences file is limited to the maximum size of the value you are attempting to store. (Meaning you can't put a String value that is longer than Strings can be in Java.)
The only thing I would suggest is making sure to batch the edits as much as possible (meaning don't .commit()
each change) and also don't create a new editor for each change. (These are just good practices.)
SharedPreferences settings = getSharedPreferences(PREFS_FILE_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("firstValue", mFirst);
editor.putInt("secondValue", mSecond);
editor.putInt("thirdValue", mThird);
// Commiting the edits
editor.commit();

- 1,364
- 3
- 18
- 30

- 2,671
- 1
- 18
- 14
-
3"longer than Strings can be in Java" - Strings can have 2^31 - 1 = 2 billion characters. Well, that may rely on the internal storage of the phone then. – Teo Inke Jun 05 '15 at 16:09
I dont know about any limitations but regarding your problem with everyones progress being wiped out. You can override the onUpgrade method in your SQLite class and then migrate everyones data over to the newer database.

- 8,900
- 14
- 59
- 91
-
2Exactly. See the notepad examples - you can use ALTER TABLE to add new columns. Or you could add a whole new table. You could switch to SharedPreferences, but since you already went for a database approach, you may as well stick with it. – EboMike Jul 08 '10 at 08:32
-
I think I am going to try out the SharedPreferences model and see which I like more. I'll keep the DB around though because at some point I plan to allow for multiple saved games. Thanks for the tip about Alter Table, it will assist me in migrating to SharedPreferences and keep everyone's progress saved. – Tim Jul 08 '10 at 22:17
DO NOT use shared preferences for game data. I can download your game and with the tool below hack it to pieces. If your money is stored there I'll just change it to 4.2 million gold and call it good.
https://www.kingoapp.com/root-tutorials/hack-app-game-cheat-droid.htm
What's stopping someone from editing their XML file with a shared preferences editor?
I can't believe nobody thought about this while answering your question. Your limitations are significant in a videogame setting. There is nothing stopping anybody from editing all of their shared preferences data to hack the bleep out of your game.

- 138
- 10
-
If you want to use SP for storing game data you can encrypt it for ex. with 'javax.crypto.Cipher' that'll be much much harder to hack. – Jan Rozenbajgier Jan 18 '23 at 22:13
-
It's still just not its intended use, encrypting it in that location isn't a good idea because once that encryption is cracked by a single user, your entire world falls down, and at that point you have to decrypt the entire userbase and re-encrypt it with a new uncracked, crackable cipher. SP is for saving things like a dark mode toggle or other basic non-sensitive configurations. – Krausladen Jan 24 '23 at 18:48
As mentioned in other answers, Shared Preferences is only really limited by phone storage and simplicity.

- 440
- 1
- 5
- 15