Generate Facebook Android SDK keyhash using java code


Trouble generating keyhash for Facebook SDK integration with your application, here is the java code to generate keyhash.

Add the following code inside onCreate()

try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "YOUR_PACKAGE_NAME", 
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            System.out.println("KeyHash : "+ Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
     } catch (NameNotFoundException e) {
   } catch (NoSuchAlgorithmException e) {
 }

NOTE: Do not forget to replace YOUR_PACKAGE_NAME with your Main activity package name. After running the application you will find the key hash in LogCat.

Generate Facebook Android SDK key hash using java code
Generate Facebook Android SDK key hash using java code



Recent Posts:




Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.