gradleramdisk

Gradle - make use of RAMdisk


I just run into idea of using RAMdisk for compilation results 1

How to use RAMdisk with Gradle?

I guess it is worth moving .gradle and build folders into RAMdisk.


Solution

  • 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 to have a general RAM disk for other purposes as well, for instance I use it for my $TMP. Anyway, 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, though!
    
    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-builds/${rootProject.name}/${project.name}"
            println "BUILDING TO RAMDISK: buildDir=$buildDir"
        }
    }
    

    Remove debug println statements as you see fit.