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).