Changing gradle cache directory

I’m trying to change gradle cache location depending on environment I’m on. I want it to be some specific folder on CI and default for local builds. I need this because windows has 260 characters limit for file names and our Windows CI Agent fails to build APK. On Mac and Linux it works as expected but Windows…

My gradle.properties would be something like for CI:

android.enableBuildCache=true
android.buildCacheDir=cicache/cache

Is there a way to programatically change that depending on environment?

Also, ideally it should be done once, in one file/script without replicating all the work on many agents.