The
Android operating system has lots of built-in security features, such
as application sand boxing, protection against buffer and integer
overflow attacks, and segregated memory areas for program
instructions and data. As a result, simple Android apps that don't
perform any file system or networking operations can often be
considered secure by default. If
you are developing a more complex app, however, it is your
responsibility to make it secure and protect the privacy of your
users. In this blog, I'm going to list some of the best practices you
should follow to build a secure Android app that doesn't leak data or
permissions, and is, in general, less vulnerable to malicious apps
that might be installed on the user's device.
1. Use Internal Storage for Sensitive Data
Every Android app has an internal
storage directory associated with it. Files inside this directory are
very secure because they use the
MODE_PRIVATE
file creation mode by default. This means the files cannot be
accessed by any other app on the device. Therefore, it is a best
place to store all the sensitive data of your app in the internal
storage directory.
To determine the absolute path of your
app's internal storage directory, it is recommended that you use the
getFilesDir() method. Once you know its
path, referencing files inside it is as simple as referencing files
inside any other directory.
For example, here's how you could
reference a file called myfile.dat in the internal
storage directory of your app:
File myFile = new
File(getFilesDir(), "myfile.dat");
2. Encrypt Data on External Storage
The internal storage capacity of an
Android device is often limited. Therefore, at times, you might have
no choice but to store sensitive data on external storage media, such
as a removable SD card.
Because data on external storage media
can be directly accessed by both users and other apps on the device,
it is important that you store it in an encrypted format. One of the
most popular encryption algorithms used by developers today is
AES(Advanced
Encryption Standard) with
a key size of 256 bits.
Writing code to encrypt and decrypt your
app's data using the
javax.crypto
package, which is included in the Android SDK, can be confusing.
Therefore, most developers prefer using third party libraries, such
as Facebook's
Conceal library, which are usually much easier to
work with.3. Validate User Input
On Android, invalid user input doesn't
usually lead to security issues like buffer overruns. However, if you
allow users to interact with a SQLite database or a content provider
that internally uses a SQLite database, you must either rigorously
sanitise user input or make use of parameterized queries. Failing to
do so makes your data vulnerable to SQL injection attacks.
4. Avoid Asking for Personal Data
User privacy is given a lot of
importance these days. Therefore, unless you have a good reason and a
very secure infrastructure to collect, store, and transmit personal
user information, you must avoid directly asking for it in your apps.
A better approach to user authentication
and user profile information look up on Android is through the Google
Identity Platform. Google Identity Platform
allows users to quickly sign in to your app using their Google
account. After a successful sign in through the platform, whenever
necessary, your app can easily look up various details about the
user, such as the user's name, email address, profile photo,
contacts, and more. Alternatively, you could use free services like
Firebase
that can manage user authentication for you.
If you must handle user credentials
yourself, it is recommended that you store and transmit them in the
form of secure hashes. The most straightforward way to generate
different types of hashes using the Android SDK is by using the
MessageDigest class.




