How to copy Gradle caches to another offline machine?

The cache is specifically not portable due to absolute file references. If your user name is different, your ~/.gradle is likely in a different location, which will break the cache. Rather than rely on the implementation details of the cache, it would be better to work at higher level.

One way you could do this is to gather all of your dependencies in an archive. For example, something like this should grab everything in all configurations:

task bundleDependencies(type: Tar) {
    baseName = 'dependencies'
    configurations.each { configuration ->
        if (configuration.canBeResolved) { from configuration }
    }
}

Transfer the archive to the offline machine, extract, and use a flatDir repository (maybe conditionally if you want to use it when offline without adding it each time):

repositories {
    if (gradle.startParamter.offline) {
        flatDir { dirs 'path/to/extracted/dependencies/tar' }
    }
}

You shouldn’t have to change anything else in your build.