Split a Gradle Build File

Posted by in Software

TL;DR: If you can, use ext {} with constants (hash). You can do call pointer things too!

In the past days I’ve been trying to add some functionality to my Gradle build file so the version would be determined from the git tag. I’ve found this excellent entry showing how to get the actual string. My code ended up being like this:

Long build file

The problem I have is that with every custom thing, the build file increases. Then, finding an error of a particular thing becomes more and more difficult. So, I’ve been looking for a way to modularise the build system and I found something.

My versions.gradle looks like this now:

Then, in my build.gradle I can use:

See how it uses VERConsts[] hash. Using the VERConsts['NAME_CALL']() will do the same.

I guess this doesn’t work every time, but if you can manage to call your functions etc. in their own file (like calling getVersionName() in versions.gradle), you can place the result in a hash and export it :)

HTH,


A little experiment: If you find this post and ad below useful, please check the ad out :-)