Remember my last post “Generating App Hash key for SMS Retriever API for (Xamarin incl.)…” where I shared with you guys how to successfully generating the App hash key in both MacOs and Windows environments without any hassle?
Now this is the improved alternative of it! making the whole process much easier specially for Xamarin Android development!
So earlier we looked at how we could generate the App Hash key straight away from MacOS Terminal or on Windows with the keystore hex generation followed by the Xamarin Android helper method execution to retrieve the app hash key!
That specific helper method I had extracted partially from the Google’s official documentation sample regarding the SMS Retriever API, which I couldn’t completely port to Xamarin Android at that time while I was working on my private project.
But later I took a proper look at it and figured out how to completely port it to Xamarin Android, with a few minutes of back and forth between the Java code and C# Xamarin namespaces! 😀
Easier and Faster method!
Now with the fully ported code, as an extension helper class, we could easily generate the Hash key for our Xamarin Android app either on Windows or Mac using Visual Studio, without having to use any external Terminal or Command prompt.
but you need to follow some pre-requisites to get it to properly work..
Let’s get started!
Get your pre-requisites right!
You need to make sure you’re using the correct Keystore file to sign your APK during build, therefore navigate to your Xamarin Android project properties and set the correct Keystore file path and configuration.
In Visual Studio on Windows:
In Visual Studio on Mac:
Finally make sure you have given the proper app package name or app id to your app in the same project properties. ex: com.testapp.xyz
Why you ask? Since we’re directly generating the app hash code from the project itself, we need to make sure during the compilation the app, the final APK is signed using the keystore we are hoping to use in production. So we can retrieved the signed hash hex from the app Context at run time and the app id to generate the app hash code. 🙂
Then let’s begin the AppHashKeyHelper implementation… 😉
Let the implementation begin…
Let the implementation begin! lol 😀 So here is the complete ported helper class for generating the app hash key for your Xamarin Android app!
Simply copy the whole code to anywhere in your Xamarin Android project, and you could straight away call it from anywhere in the app to generate the app hash key!
We’re simply passing in the Android App Context, which will then first retrieve the signed kestore hash string from the Package Manager.
Then we retrieve the app package name from the Context itself, and move ahead with the usual encrypted key generation.
I would recommend calling this from your MainActivity itself, since this is just a one time execution in your local debug environment.
There you go! Simple as that! 😀
Now once again, keeping in mind to make sure to remove the above helper class call from your project code, before you submit your final APK to the Play Store, since google does not recommend generating the App’s hash key during user run time. Just simply retrive your hash key and remove the code, and then set it up in your SMS API gateway execution.
Thats it! 😀
Share the love! 😀 ❤