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.