How to auto increment version in gradle

I have a multi module gradle project.
i have to increment my version automaticallly when i build this project.
please help me what i have to add in build .gradle file ,so the version is dynamically change.

I’ve done this in the past by having a seperate version.txt file which is referenced in build.gradle. Eg

version = file('version.txt').text.trim()

Then to build & release version 1.5 (for example) you could do something like

echo 1.5 > version.txt && gradle release && echo 1.6-SNAPSHOT > version.txt


thanks for your reply.