I am creating an App and i want to store username and password in app itself. The username password is not entered by user. The credentials is common for everyone. I want to save the username and password in app code itself. What is the secure method to save in android?
Asked
Active
Viewed 502 times
2 Answers
1
You can use SharedPreference
to store data permanently in the Android
app.
In your Activity
,
//get the sharedPrefs
val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return
//store the data in sharedPrefs file
with (sharedPref.edit()) {
putString("Some_key_user_name",userNameText)
commit()
}
//read the stored data form sharedPrefs
val storedUserName = sharedPref.getString("Some_key_user_name", defaultValue)
For the password, you can use the same approach. However, if the password is so precious(which it is in most cases) you can encrypt the data stored in sharedPreference file. Look ahead with this
As an example in my Activity
,
val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return
with (sharedPref.edit()) {
putString("MY_HELLO_WORLD_KEY","helloWorld")
commit()
}

iCantC
- 2,852
- 1
- 19
- 34
-
Can you show a simple example code for the above? Suppose i want to hardcode password 'helloWorld' in the app. how can i do that? – Rizwan May 11 '20 at 15:28
0
- You can use key store to save your private keys securely
check this Securely Storing Keys in Android Keystore
- you can also use EncryptedPreferences to store simple data in an encrypted way.

Oussèma Hassine
- 741
- 1
- 7
- 18