Gradle script to autoversion and include the commit hash in Android

I need to write a gradle script to auto version my application on every commit. I need to also include the commit hash as a reference in the application for testers. I am confused how auto version...