36

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!

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
daveomcd
  • 6,367
  • 14
  • 83
  • 137
  • https://stackoverflow.com/questions/49278875/error-project-app-not-found-in-root-project-projectname/56723341#56723341 – PKumar Jun 23 '19 at 11:18

7 Answers7

33

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.

Top-Master
  • 7,611
  • 5
  • 39
  • 71
Yamashiro Rion
  • 1,778
  • 1
  • 20
  • 31
  • 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? – Bernardo Ruz Mar 30 '21 at 21:29
  • Yamashiro Rion Thanks a lot my friend. – Kunal Kalwar Sep 01 '22 at 18:17
16

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

Rakesh
  • 1,205
  • 1
  • 14
  • 33
9

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?

Community
  • 1
  • 1
xxstevenxo
  • 661
  • 3
  • 12
  • 27
  • thanks! it says Task 'build' not found in root project 'MyProject' -- not sure what it means right now googling it. – daveomcd May 25 '13 at 21:01
  • try this link also http://stackoverflow.com/questions/14093024/trying-gradle-build-task-build-not-found-in-root-project – xxstevenxo May 25 '13 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 May 25 '13 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 May 25 '13 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 May 25 '13 at 21:33
  • Yeah I'm able to do that. I might just create a new project and move the code over (it's not much); however, I would have liked to solve it in case someone came across the same thing later. – daveomcd May 25 '13 at 21:35
  • No luck trying to figure this out? – xxstevenxo May 27 '13 at 07:05
  • Nope just created a new project and migrated it over. ;-/ I still have the old one in case someone has any ideas in hopes of getting this answered though. – daveomcd May 27 '13 at 10:42
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.

kyleai
  • 103
  • 1
  • 9
  • 1
    In my case I put directly `app`. Like `gradle -q dependencies app:dependencies --configuration compile` – AlexAndro Jan 11 '18 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

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
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.

bene25
  • 580
  • 6
  • 18