2

I am trying to release my app on Google Play. I have a Facebook login in my app. Up until yesterday all was working fine till the time I was running the application with debug.keystore. But when I use my own release key and sign my application Facebook doesn't login and I cant seem to figure out why.

Followed this link and did all that was meth as well : so : key-hash-for-android-facebook-app

I changed machines, I changed platforms (windows and mac osx ML) as well to get a solution but the same issue. IT DOES NOT LOG IN. The below code gives me the proper hash key when i use debug.keystore where as when i sign the application even with different keys I get the same Hashkey ( which I have come to a conclusion after lots of trials that the key i get is wrong)

PackageInfo info;
try {
    info = getPackageManager().getPackageInfo("com.you.name", PackageManager.GET_SIGNATURES);
    for (Signature signature : info.signatures) {
        MessageDigest md;
        md = MessageDigest.getInstance("SHA");
        md.update(signature.toByteArray());
        String something = new String(Base64.encode(md.digest(), 0));
        //String something = new String(Base64.encodeBytes(md.digest()));
        Log.e("hash key", something);
    }
} catch (NameNotFoundException e1) {
    Log.e("name not found", e1.toString());
} catch (NoSuchAlgorithmException e) {
    Log.e("no such an algorithm", e.toString());
} catch (Exception e) {
    Log.e("exception", e.toString());
}

So is there any kind of extra steps which we need to take when signing the application with the release key. Please HELP.

Community
  • 1
  • 1
iSmita
  • 1,292
  • 1
  • 9
  • 24
  • 1
    Code you pasted here to find key is a separate application? – Biraj Zalavadia Sep 04 '13 at 06:39
  • I just posted that code to say that I have used it. I have put the proper package name in the actual source code. – iSmita Sep 04 '13 at 06:42
  • 2
    1. Create apk from your release key. 2. Edit the code to get the keyhash on your mail using intents. 3. Copy paste the hashkey from your mail to FB. 4. Remove the email intent code. 5. Create apk using the same key. – Sunil Mishra Sep 04 '13 at 07:08
  • 1
    @Smita,first you have to create a release key for your app not the debug key that you are using. Also, for that release key you have to generate a new key hash for your facebook application and then add this key into your facebook app from the facebook developer console, I think it should work – Salman Khan Sep 04 '13 at 07:20

5 Answers5

11

Here is what I did that solved the problem:

  1. Used openssl-0.9.8e_WIN32 Instead of openssl-0.9.8k_WIN32
  2. I was not using alias in the following or maybe I was entering a space in the alias because of which the key which was generated was wrong.

I used this instead:

keytool -exportcert -alias <aliasNameUseInReleaseKeystore> -keystore <ReleasekeystoreFilePath> | openssl sha1 -binary | openssl base64

P.S. The method which I have posted in my question is really useless. It did nothing more than confuse me to no end.

TylerH
  • 20,799
  • 66
  • 75
  • 101
iSmita
  • 1,292
  • 1
  • 9
  • 24
7

I have found a fantastic solution to manage debug and release environments.

1.Generate two hashes, for debug using this command:

keytool -exportcert -alias androiddebugkey -keystore c:\Users\YourUser\.android\debug.keystore | openssl sha1 -binary | openssl base64

And this one for release:

keytool -exportcert -alias "yourAliasUsedWhenYouGeneratedTheKey" -keystore "C:\Users\YourUser\AppData\Local\Android\android-studio\key.jks" | openssl sha1 -binary | openssl base64

2.Go to Facebook Applications and create TWO applications, one "Your Application" and other "Your Application (debug)". Then assign the debug hash to the debug application and the release hash to the normal application (obvious).

3.Get both Application Id and write them in the strings.xml file this way:

<string name="app_id">123456789</string>
<string name="app_id_debug">987654321</string>

4.And finally, in your code, register the appId programatically in your Facebook Login fragment this way:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    uiHelper = new UiLifecycleHelper(getActivity(), callback);
    uiHelper.onCreate(savedInstanceState);

    String appId;

    try {
        ApplicationInfo appinfo = getActivity().getPackageManager().getApplicationInfo(getActivity().getPackageName(), 0);
        boolean isDebugMode = (0 != (appinfo.flags &= ApplicationInfo.FLAG_DEBUGGABLE));

        if (isDebugMode)
            appId = getString(R.string.app_id_debug);
        else
            appId = getString(R.string.app_id);
    } catch (PackageManager.NameNotFoundException e) {
        appId = getString(R.string.app_id);
    }

    Session session = new Session.Builder(getActivity().getBaseContext()).setApplicationId(appId).build();
    Session.setActiveSession(session);

    return inflater.inflate(R.layout.fragment_facebook_login, container, false);
}

This way you will use the right appId and the right application on every environment without changing anything!!

Jose Ignacio Hita
  • 874
  • 1
  • 10
  • 17
  • We can add more than one hash for an app in App dashboard -> Settings. – Paramvir Singh Mar 03 '14 at 07:28
  • keytool -exportcert -alias "yourAliasUsedWhenYouGeneratedTheKey" -keystore "C:\Users\YourUser\AppData\Local\Android\android-studio\key.jks" | openssl sha1 -binary | openssl base64 In this command what is yourAliasUsedWhenYouGeneratedTheKey – Prabha1 Nov 24 '14 at 09:21
6

For Linux

Open Terminal :

For Debug Build

keytool -exportcert -alias androiddebugkey -keystore debug.keystore | openssl sha1 -binary | openssl base64

you wil find debug.keystore from ".android" folder copy it from and paste on desktop and run above command

For release Build

keytool -exportcert -alias <aliasNameUseInReleseKeystore> -keystore <RelesekeystoreFilePath> | openssl sha1 -binary | openssl base64

NOTE : Make sure In Both case it must ask for password. If it does not asks for password that means something is wrong in command.

Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
  • 1
    hey I am trying the release command and have been struggling with this for a while I get a key but it does not ask me for a password my path is this does this look right? `~/Users/loaner/spencerjames.jks` –  Jan 07 '15 at 19:25
2

Here is what I did that solved the problem:

Used openssl-0.9.8e_WIN64 Instead of openssl-0.9.8k_WIN64 from here

keytool -exportcert -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEY_PATH> | PATH_TO_OPENSSL_LIBRARY\bin\openssl sha1 -binary | PATH_TO_OPENSSL_LIBRARY\bin\openssl base64

example :

keytool -exportcert -alias "yourAliasKey" -keystore "C:\Users\YourUser\AppData\Local\Android\android-studio\key.jks" | PATH_TO_OPENSSL_LIBRARY\bin\openssl sha1 -binary | PATH_TO_OPENSSL_LIBRARY\bin\openssl base64

use your playstore keystore alias as RELEASE_KEY_ALIAS and its's saved path with file name as RELEASE_KEY_PATH.

Note: use your playstore keystore password when if ask for type password.

Dinil ps
  • 321
  • 3
  • 10
0

Try this below code in on create method

 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());
            Log.i("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));//will give developer key hash
            Toast.makeText(getApplicationContext(),Base64.encodeToString(md.digest(), Base64.DEFAULT), Toast.LENGTH_LONG).show(); //will give app key hash or release key hash

            }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }
King of Masses
  • 18,405
  • 4
  • 60
  • 77