It doesn’t resolve your issue, but it can be a different solution.
You can use gradle.properties inside your root project and define:
VERSION_NAME=1.2.1
VERSION_CODE=26
Then in your build.gradle you can use:
versionName project.VERSION_NAME
versionCode Integer.parseInt(project.VERSION_CODE)