Auto-updating an Android Project’s versionCode
This is a quick tip I’ve enjoyed using over the past few years in some of the Android apps I’ve worked on. Instead of having to manually update the “versionCode” of your build.gradle script, you can easily automate it by adding a single line of code. This technique is especially useful if you have a continuous integration (CI) job constantly pushing updates out to Google Play for your testers to play around with. Automation is always better, right?
Anyway, to get started go ahead and open your “app/build.gradle” file and add the following line of code to the start of the file:
def buildVersionCode = Integer.parseInt(new Date().format("yyMMddHH"))
Scroll down a bit in the file until you find your existing “versionCode” setting. It may look something like this:
versionCode 1
Simply change it to:
versionCode buildVersionCode
Now, the next time you build, your versionCode will be based on today’s date and time and will always increment between builds.
If you don’t see a “versionCode” in your build.gradle file, check to see if it’s defined in your AndroidManifest.xml file. If you see it there, just remove it and add both “versionCode” and “versionName” to your build.gradle file under the android > defaultConfig section. It should be at the same level as your applicationId.

Do you use a different technique? Let me know in the comments below or by reaching out to me on Twitter!
Hello sir, i have problem with this run tasks, how to fix it? May you help me please to solve it?
WARNING: Configuration ‘compile’ is obsolete and has been replaced with ‘implementation’ and ‘api’.
Thank you
best regard
Hey Noomharn! This isn’t truly a problem just yet. Your app should still compile. However, support for the “compile” keyword within your Gradle configuration files will be removed at some point in the near future. If you’d like this message to go away, the easiest thing to do is to change all “compile” configurations in your dependency tree to “api”. You can read more about these two different configuration keywords on the official site here: https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#new_configurations.