Skip to content Skip to sidebar Skip to footer

Gradle 3.0 BuildException Cannot Create Directory

I'm on windows and updated to Android Studio (AS) 3.0 RC1. When I try to built our project with gradle via AS or terminal it stops, the Stacktrace is at the end. The weird part is,

Solution 1:

With the stable release of Android Studio 3.0 I digged a little deeper. Turns out our script for naming the APK didn't work, but was only breaking on Windows.

Old Script:

applicationVariants.all { variant ->
    variant.outputs.each { output ->
        def outputFile = output.outputFile
        if (outputFile != null && outputFile.name.endsWith('.apk')) {
            def fileName = outputFile.name.replace('.apk', "-${versionName}.apk")
            output.outputFile = new File(outputFile.parent, fileName)
        }
    }
}

Fixed Script:

applicationVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "${variant.name}-${variant.versionName}.apk"
    }
}

Solution 2:

In your gradle file (module level), are you using applicationVariants to rename your apk or things like this ? I had to remove this part in my gradle file to solve this issue.


Solution 3:

I am building same app with Android Studio 3.0 on both Linux or Windows. After upgrading to Gradle 3.0.1, I had a similar issue on Windows, while it was working fine on Linux:

Error:Execution failed for task ':smsreminder:packageDebug'. > Cannot create directory C:\Users\Florent\AndroidStudioProjects\MyApp\myapp\build\outputs\apk\debug\C:\Users\Florent\AndroidStudioProjects\MyApp\myapp\build\outputs\apk\debug

And thanks to above answer from Leo, I made this more friendly apk name:

outputFileName = "${variant.name}-${variant.versionName}.apk"
=> produces: debug-x.y.apk

outputFileName = output.outputFile.name + ".${variant.versionName}.apk"
=> produces: MyAppName.x.y.apk

Otherwise, I found this working too if one want to keep default :

if (org.gradle.internal.os.OperatingSystem.current().windows)
            outputFileName = "${variant.name}-${variant.versionName}.apk"
        else
            outputFileName = new File(output.outputFile.parent, outputFileName.replace(output.outputFile.name, defaultConfig.applicationId + ".apk"))

Solution 4:

android.applicationVariants.all { variant ->
  variant.outputs.all {
      outputFileName = "${variant.name}-${variant.versionName}.apk"

This code will fix the issue.!! There is a minor change to the above answer in this script


Post a Comment for "Gradle 3.0 BuildException Cannot Create Directory"