android studio 3.1: build:gradle:3.1.0 – Absolute path are not supported when setting an output file name

Just in case this helps, this error means that now it’s not allowed to have absolute paths on anything related to the apk’s file name. I can attach you my BEFORE and AFTER to achieve what I needed (to have the APK in the project/app/build/ folder:

BEFORE gradle 3.1.0

applicationVariants.all { variant ->
    variant.outputs.all { output ->
        outputFileName = new File(
                output.outputFile.parent,
                output.outputFile.name)
    }
}

IN or AFTER gradle 3.1.0

applicationVariants.all { variant ->
    variant.outputs.all { output ->
        outputFileName = new File(
                "./../../../../../build/",
                output.outputFile.name)
    }
}

I think using “./../../../” is bad solution… I use common gradle script for several projects and I want to make code to be independency from depth of output dir.

After some researching I found this solution for gradle plugin 3.1.2:

applicationVariants.all { variant ->
    variant.outputs.all { output ->
        def relativeRootDir = output.packageApplication.outputDirectory.toPath()
                 .relativize(rootDir.toPath()).toFile()
        output.outputFileName = new File( "$relativeRootDir/release", newOutputName)
    }
}

I experienced the same issue. I haven’t put the effort in to figure out exactly what’s happened but there’s a simple fix.

Just remove the root from your new file and trust the framework, i.e. change your code to

outputFileName = new File("release", releaseFileName.toLowerCase())

That was sufficient for us. We don’t care about where the apk goes, only the name of the apk for a particular flavour.

Leave a Comment