Gradle, which is now Android Studio’s default and recommended build system, can help you automate many tasks that you currently might be doing manually.

Gradle is an open source build automation system that builds upon the concepts of Apache Ant and Apache Maven and introduces a Groovy-based domain-specific language (DSL) instead of the XML form used by Apache Maven for declaring the project configuration. (Wikipedia)

As you know, every Android app must declare an app’s versionCode (a monotonically increasing number for each version of the app), and versionName (usually a human-readable string of the form majorVersion.minorversion.patchVersion) Here’s how you can specify everything in exactly one place and use it everywhere needed.

Define individual components of the version number and compute Version Number. 

Start off with defining individual components of the version number—major version, minor version and patch level—as separate Gradle variables in app level gradle. This is the one place you’d update the numbers for every version you release.Then have Gradle compute the versionCode from these three components as a place-value-based number.

The versionName is a straightforward string concatenation of the three components.

Generate a string resource.

Next, have Gradle generate a string resource for you automatically, which you can use in your about app’s XML layouts & Java code (as @string/app_version & getResources().getString(R.string.app_version) respectively).

If version number is being used in other activities of the app, it can easily be accessed from the string resources and a change in app version can be done at just a single place and is reflected across.
Additionally if you are using separate API endpoints for production and development or have different values for some string resources you can easily define them as a string resource under buildTypes as shown below
The version number of google support libraries can also be better managed by defining it as a gradle variable and changing this variable makes it easier and effortless  to update all the support libraries.
Screenshot 2018-10-14 at 10.56.14 AM
Screenshot 2018-10-14 at 10.56.04 AM
If you have any suggestions, queries or pro tips to better manage gradle feel free to use the comments section below.