37

I'm currently using Android Studio and I'm trying to do a rebuild but I get the following error.

Gradle: 
FAILURE: Build failed with an exception.

* What went wrong:
Project 'MyProject' not found in root project 'MyProject'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

I would attempt the "Try: run with --stacktrace" but, I'm unsure how to do this. Can anyone help my troubleshoot this problem? Thanks!

1

7 Answers 7

34

I had the same issue, which can have different solutions and reasons.

Sync may be required

Solution

Delete .gradle folder (which is in project's root folder), and in Android Studio, from File menu click "Sync Project with Gradle Files" option.

Reason

Once this happened to me after switching Git branches:

  • In branch feature/a I had a Gradle module named a.
  • In branch feature/b there was no module a.

After I switched from feature/a to feature/b, I received the following build output:

Project 'a' not found in root project 'MyApp'

It seems that Android Studio didn't sync with Gradle files after switching to another branch.

Project structure mismatch

Solution

Required structure preview:

root-folder/
  |
  |--- app/
  |      |
  |      |--- build.gradle
  |
  |--- build.gradle
  |--- settings.gradle

Steps:

  • Ensure the Root-Project's files are placed in a sub-folder of root-folder, for example, let's call it "app" folder.

  • Then import said app folder in root-folder's settings.gradle file, like:

    include ':app'
    
    rootProject.name = 'root-folder'
    project(':app').name = 'root-project'
    
  • Finally, ensure root-folder's build.gradle file does not "apply plugin: 'com.android.application'", which should be applied in app/build.gradle file instead.

In other words, Anything related to App needs to be moved to a sub-folder (of root-folder if not already), and the root-folder's build.gradle file should only have buildscript { ... } block (or any setting applied to all sub-projects).

Reason

It happened after upgrading our Android-Studio IDE, that whenever we were trying to run Android-tests, the latest IDE version 2021.1.1 (at time of writting) had a bug and OP's error was logged.

2
  • This partially solves my problem. If I Sync Project with Gradle Files in branch A, solves the problem for Branch A. I then checkout branch B and get the same error in branch B - so I do the same (Sync Project with Gradle Files) and it fixes the problem for Branch B. But if I go back and checkout Branch A, I get the original error Project 'MyProject' not found in root project 'MyProject' any suggestions? Commented Mar 30, 2021 at 21:29
  • Yamashiro Rion Thanks a lot my friend. Commented Sep 1, 2022 at 18:17
16

Delete .gradle folder in your project's root folder and resync gradle from Android Studio.

0
10

Assuming your'e using Windows, open command prompt and navigate to your project root and run gradlew build to see the error in more detail

cd \androidstudio\MyProject
gradlew build

or run stacktrace with

gradlew build --stacktrace

Duplicate answer can be found here Android Studio: Where is the Compiler Error Output Window?

8
  • thanks! it says Task 'build' not found in root project 'MyProject' -- not sure what it means right now googling it.
    – daveomcd
    Commented May 25, 2013 at 21:01
  • try this link also stackoverflow.com/questions/14093024/…
    – xxstevenxo
    Commented May 25, 2013 at 21:03
  • thanks, after looking at both i decided to close studio and reopen. Now the rebuild worked, but it says Error running MyProject-MyProject: Module is not found ... so looking into that now.
    – daveomcd
    Commented May 25, 2013 at 21:19
  • Update: Noticed that my Project folder has a blue square on it's icon, but the folder with the same project name within doesn't. If I create a new project it does however. Not sure if this is useful information or not.
    – daveomcd
    Commented May 25, 2013 at 21:28
  • Hmm.. I'm not too sure about that, both my folders have a blue square on them. Are you able to run gradlew build on a new project? (ie. MyProject2)
    – xxstevenxo
    Commented May 25, 2013 at 21:33
7

If people are still having problems:

I had this happen to me and it was because I renamed my app directory manually when trying to rename my project. projects\Hello World\app should be there, and it contains build, libs, src folders, and some files. If it is called something else then renaming it to app through Windows explorer might fix it.

1
  • 1
    In my case I put directly app. Like gradle -q dependencies app:dependencies --configuration compile
    – AlexAndro
    Commented Jan 11, 2018 at 15:04
3

I have the same problem. Just go to your settings.gradle in MyProject directory (i.e. root) and change file like..

include ':app'
rootProject.name = "MyProject"

and then invalidate the cache and restart the project from the file menu.

2

I got this error because module already had its own settings.gradle

So I had to remove/rename that settings.gradle.


My root settings.gradle adding modules like

include "polkaj-ss58",
        "polkaj-scale",
        "polkaj-json-types",
        "polkaj-scale-types",
        "polkaj-schnorrkel",
        "polkaj-common-types",
        "polkaj-api-base",
        "polkaj-api-http",
        "polkaj-api-ws",
        "polkaj-tx"//,


include(':balance')
project(':balance').projectDir = file('./examples/balance')

Gradle likely suggest to use Build Composition to build module from different projects

https://docs.gradle.org/current/userguide/composite_builds.html

0

Clearing cache from Android Studio and manually didn't solve problem. Also I had two dots before each gradle file, for example :build.gradle. Issue appeared after upgrading AS to 2022.2.1 Patch 2

Solution that work for me: Close project, remove it from recent and open once again. On third attempt AS should show prompt like - "Sync project with existing gradle files", press "Yes". After that project should start to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.