Search code examples
androidflutterdartgradle

Gradle build failed to produce an .apk file. It's likely that this file was generated under ~\<Project Name>\build, but the tool couldn't find it


I have a very annoying problem after executing (flutter clean), (File -> Invalidate Caches), and then rebuilding my Flutter project by (flutter pub get) ..

After I run my project, I encounter this damn message which says: (( Gradle build failed to produce an .apk file. It's likely that this file was generated under ~<Project Name>\build, but the tool couldn't find it.))

enter image description here

Could anyone help me and tell me how to solve this damn problem?? and why that damn Gradle is not unable to produce an APK??


Solution

  • What caused my damn problem is that in build.gradle(project), in this section of code:

    rootProject.buildDir = '../build'
    subprojects {
      project.buildDir = "${rootProject.buildDir}/${project.name}"
    }
    subprojects {
      project.evaluationDependsOn(':app')
    }
    tasks.register("clean", Delete) {
      delete rootProject.buildDir
    }
    

    I changed the code like that:

    rootProject.layout.buildDirectory = "../build"
    subprojects {
      project.layout.buildDirectory = "${rootProject.name}/${project.name}"
    }
    subprojects {
      project.evaluationDependsOn(":app")
    }
    tasks.register("clean", Delete) {
      delete rootProject.layout.buildDirectory
    }
    

    After I read that Project.buildDir is deprecated and Project.layout.buildDirectory should be used instead from here:

    https://docs.gradle.org/8.10.1/userguide/upgrading_version_8.html#deprecations_7

    Deprecated Project.buildDir is to be replaced by Project.layout.buildDirectory The Project.buildDir property is deprecated. It uses eager APIs and has ordering issues if the value is read in build logic and then later modified. It could result in outputs ending up in different locations.

    It is replaced by a DirectoryProperty found at Project.layout.buildDirectory. See the ProjectLayout interface for details.

    Note that, at this stage, Gradle will not print deprecation warnings if you still use Project.buildDir. We know this is a big change, and we want to give the authors of major plugins time to stop using it.

    Switching from a File to a DirectoryProperty requires adaptations in build logic. The main impact is that you cannot use the property inside a String to expand it. Instead, you should leverage the dir and file methods to compute your desired location.

    I think the problem is in this line:

    project.layout.buildDirectory = "${rootProject.name}/${project.name}"
    

    Although I will be thankful if anybody could reply to me and tell me how I can use project.layout.buildDirectory instead of project.buildDir in the Gradle code.

    Thank you.