Android Studio build fails with “Task ” not found in root project ‘MyProject’.”

Though late in answering, this is a hard one to Google (the single quotes) and it’s not clear what’s happening. I don’t have the reputation yet to comment or ask for scope (or post 3 links), so this answer may be a little tedious.

To answer fast, you may have multiple Gradle plugins in your project.

Synchronize Gradle Wrapper and Plugins

My issue seemed to start with a corrupted IML file. Android Studio (between closing and reopening a project) began complaining an IML was gone (it wasn’t) and a module should be deleted, which I declined. It persisted, I upgraded to AS 0.8.7 (canary channel) and got stuck on the OP issue (Task ” not found in root project). This completely blocked builds so I had to dig in to Gradle.

My repair steps on OSX (please adjust for Windows):

  1. Upgrade Android Studio to 0.8.7
    • Preferences | Updates | Switch “Beta Channel” to “Canary Channel”, then do a Check Now.
    • You might be able to skip this.
  2. Checked the Gradle wrapper (currently 1.12.2; don’t try to use 2.0 at this time).
    • Assuming you don’t need a particular version, use the latest supported distribution

      $ vi ~/project/gradle-wrapper.properties ... distributionUrl=http\://services.gradle.org/distributions/gradle-1.12-all.zip

    • This can be set in Android Studio at Preferences | Gradle (but 0.8.7 was giving me ‘invalid location’ errors).

    • The ‘wrapper’ is just a copy of Gradle for each Android Studio project. It allows you to have Gradle 2 in your OS, and different versions in your projects. The Android Developer docs explain that here.

    • Then adjust your build.gradle files for the plugin. The Gradle plugin version must be compatible with the distribution/wrapper version, for the whole project. As the Tools documentation (tools.android.com/tech-docs/new-build-system/user-guide#TOC-Requirements) is slightly out of date, you can set the plugin version too low (like 0.8.0) and Android Studio will throw an error with the acceptable range for the wrapper.

Example, in build.gradle, you have this plugin:

dependencies {
    classpath "com.android.tools.build:gradle:0.12.+"
}

You can try switching it to the exact version, like this:

dependencies {
    classpath "com.android.tools.build:gradle:0.12.2"
}

and (after recording what version you’re changing from in each case) verifying that every build.gradle file in your project pulls in the same plugin version. Keeping the “+” should work (for 0.12.0, 0.12.1, 0.12.2, etc), but my build succeeded when I updated Google’s Volley library (originally gradle:0.8.+) and my main project (originally 0.12.+) to the fixed version: gradle:0.12.2.

Other checks

  1. Ensure you don’t have two Android Application modules in the same Project
    • This may interact with the final solution (different Gradle versions, above), and cause

      UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define (various classes)

    • To check, Build | Make Project should not pop up a window asking what application you want to make.

  2. Invalidate your caches
    • File | Invalidate Caches / Restart (stackoverflow.com/a/19223269/513413)
  3. If step 2 doesn’t work, delete ~/.gradle/ (www.wuttech.com/index.php/tag/groovy-lang-closure/)
    • Quit Android Studio
    • $ rm -rf ~/.gradle/
    • Start Android Studio, then sync:
      • Tools | Android | Sync Project with Gradle Files
    • Repeat this entire sequence (quit…sync) a few times before giving up.
  4. Clean the project
    • Build | Clean Project
  5. Project structure mismatch; see https://stackoverflow.com/a/64962359/8740349 for details.

If You See This…

In my recent builds, I kept seeing horrible fails (pages of exceptions) but within seconds the messages would clear, build succeeded and the app deployed. Since I could never explain it and the app worked, I never noticed that I had two Gradle plugins in my project. So I think the Gradle plugins fought each other; one crashed, the other lost its state and reported the error.

If you have time, the 1-hour video “A Gentle Introduction to Gradle” (www.youtube.com/watch?v=OFUEb7pLLXw) really helped me approach the Gradle build files, tasks, build decisions, etc.

Disclaimer

I’m learning this entire stack, on a foreign OS, after working a different career…all at the same time and under pressure. In the last few months I have hit every wall I think Android has; I’ve been here quite often and this is my first post. I thought this was a hard fix, so I sincerely apologize if the quality of my answer reflects the difficulty I had in getting to it.

Leave a Comment