What is the best way to secure the API key with Android SDK

Published on Author Code Father
What is the best way to secure the API key with Android SDK?
How to store API key safely with Android SDK?Best Practice for storing private API keys in Android
  1. As it is, your compiled application contains the key strings, but also the constant names APP_KEY and APP_SECRET. Extracting keys from such self-documenting code is trivial, for instance with the standard Android tool dx.
  2. You can apply ProGuard. It will leave the key strings untouched, but it will remove the constant names. It will also rename classes and methods with short, meaningless names, where ever possible. Extracting the keys then takes some more time, for figuring out which string serves which purpose.

    Note that setting up ProGuard shouldn’t be as difficult as you fear. To begin with, you only need to enable ProGuard, as documented in project.properties. If there are any problems with third-party libraries, you may need to suppress some warnings and/or prevent them from being obfuscated, in proguard-project.txt. For instance:

    -dontwarn com.dropbox.**
    -keep class com.dropbox.** { *;} 

    This is a brute-force approach; you can refine such configuration once the processed application works.

  3. You can obfuscate the strings manually in your code, for instance with a Base64 encoding or preferably with something more complicated; maybe even native code. A hacker will then have to statically reverse-engineer your encoding or dynamically intercept the decoding in the proper place.
  4. You can apply a commercial obfuscator, like ProGuard’s specialized sibling DexGuard. It can additionally encrypt/obfuscate the strings and classes for you. Extracting the keys then takes even more time and expertise.
  5. You might be able to run parts of your application on your own server. If you can keep the keys there, they are safe.

In the end, it’s an economic trade-off that you have to make: how important are the keys, how much time or software can you afford, how sophisticated are the hackers who are interested in the keys, how much time will they want to spend, how much worth is a delay before the keys are hacked, on what scale will any successful hackers distribute the keys, etc. Small pieces of information like keys are more difficult to protect than entire applications. Intrinsically, nothing on the client-side is unbreakable, but you can certainly raise the bar.

(I am the developer of ProGuard and DexGuard)


1 . In register OR Login send some device key such as (SERIAL number , MANUFACTURER , etc..) to server and store .

2 . Generate a key in server side (Web service) and send it to device and store it .

3 . Generate a session for account and device . (Device can access to account or not).

4 . in any request make sure session is set and make your user aware witch sessions was activated on account .

Hope this is best regular way .

if you use this way if hacker even accessible to stored data cannot access the account on your server .