Thursday, July 19, 2012

How to use keytool in java to generate key store to use MapView in Android?

Android MapView needs a MD5 key to access Google Maps on Android device or emulator. To do that we need to generate key store using java toolkit's keytool.exe.

Following command shows how to generate key store using keytool:
Command: C:\Program Files\Java\jdk1.6.0_30\bin>keytool -genkey -alias android -keyalg RSA -keystore <keystre.jks-path> -keysize 2048 -keypass <password>

For above command you can you JDK 1.6 or JDK 1.7. I tried the command with both versions.

How to find the MD5 key store if you have already generated it in past?
Command: keytool -list -v -keystore <keystore path>

Above command will ask you for that password. Provide the password you used to generate the key store. If you are using JDK 1.7 then don't forget to mention "-v" in above command otherwise you will get "SHA1" key only as output.

Issues faced:
1. I noticed in few post people mentioned that use keytool.exe from JDK 1.6, using JDK 1.7 key tool generates key store in SHA1 format instead of MD5 format which is required for MapView. That is not the case. Above command (keytool -list -v -keystore <keystore path>) will give you output of all the key in all available formats.

2. While generating key store you might get error -> keytool error: java.io.FileNotFoundException: keystore.jks (Access is denied)
This can happen because you don't have write permission in the directory where you are planning to generate key store.

3. You might also get Google map signed api key errors in Android which will look like follows:
java.io.IOException: Server returned: 3
This can happen if you have forgotten to generate debug keystore in MD5 format. Following command might just do the trick for you:

C:\Program Files\Java\jdk1.6.0_30\bin>keytool -list -alias <androiddebugkey> -keystore <debug.keystore path> -storepass android -keypass <password>

Output:
androiddebugkey, Jul 17, 2012, PrivateKeyEntry,
Certificate fingerprint (MD5): 12:16:C2:...
Copy above output printed after key "(MD5):" text and use it to obtain your apiKey from link: https://developers.google.com/android/maps-api-signup

For more information look at link:
https://developers.google.com/maps/documentation/android/mapkey#getdebugfingerprint

Note: Debug key only works in debug mode. Debug key will not work in production mode. When you upload your app on Android Market Place, you will have to generate key again. Look at LINK for more information.

References:
  1. Android MapView tutorial: https://developers.google.com/maps/documentation/android/hello-mapview
  2. The Most Common Java Keytool Keystore Commands: http://www.sslshopper.com/article-most-common-java-keytool-keystore-commands.html
  3. http://stackoverflow.com/questions/7633867/android-mapview-loading
  4. http://stackoverflow.com/questions/7296467/google-map-signed-api-key-errors-in-android

No comments:

Post a Comment