Gradle - make use of RAMdisk

in root build.gradle

allprojects {
    buildDir = "/path/to/build/${rootProject.name}/${project.name}"
}

See also

  • Gradle global build directory
  • Change gradle build directory in android studio?
  • Change Gradle's working directory when compiling a Groovy project

and docs https://gradle.org/docs/current/userguide/writing_build_scripts.html


Just for completeness, here's how I configured Gradle (and thereby also Android Studio) on Ubuntu 14.04 to always build to RAM disk:

My ~/.bashrc contains this line in the end:

. ~/bin/mkramdisk # Setup personal RAM disk on login.

My ~/bin/mkramdisk is listed below. I suppose you could omit this script and simply use e.g. /dev/shm/${System.env.USER}/gradle-buildsin the following step, but I like having a general RAM disk for other purposes as well, for instance I use it for my $TMP so here goes:

# Setup personal RAM disk.

# This script should be sourced, hence the missing +x flag.
# Source it from e.g. from ~/.bashrc or run it from crontab
# at @reboot event (doesn't work with encrypted homedir btw.)

export RAMDISK=$HOME/tmp/ramdisk
if [ ! -d $RAMDISK ]; then
        [ -d /dev/shm/$USER-ramdisk ] || install -vd /dev/shm/$USER-ramdisk -o $USER -m 700
        [ -d ~/tmp ] || mkdir -v ~/tmp
        [ -L ~/tmp/ramdisk ] || ln -vs /dev/shm/$USER-ramdisk ~/tmp/ramdisk
fi

# Use personal RAM disk for $TMP.
export TMP=$RAMDISK

NOTE to Macintosh users: It seems you can modify mkramdisk to instead contain this command to make it work on your system.

To finally answer the question, my ~/.gradle/init.gradle is this:

println "Loaded personal ~/.gradle/init.gradle"

gradle.projectsLoaded {
    rootProject.allprojects {
        buildDir = "${System.env.RAMDISK}/gradle-build/${rootProject.name}/${project.name}"
        println "BUILDING TO RAMDISK: buildDir=$buildDir"
    }
}

Remove debug println statements as you see fit.

Tags:

Ramdisk

Gradle