10

Android Studio cannot compile my test project, because it cannot resolve the resources. I tried some different settings for the res directory in the gradle.build, but nothing worked. Am I missing something simple here? It's a simple HelloWorld project with nothing individual in it.

I have the following project structure as it was created by the Android Studio wizard (newest release 0.2.5):

enter image description here

In my AndroidManifest.xml there is the usual app name reference: @string/app_name This string is defined in main/res/values/strings.xml (selected in the screenshot above).

The error I get from the IDE (Android Studio) is:

android-apt-compiler: [MyApplicationProject] C:\...\MyApplication\src\main\AndroidManifest.xml:6: error: 
Error: No resource found that matches the given name (at 'label' with value '@string/app_name').

The error I get from the command line when i run "gradle tasks" is:

A problem occurred configuring root project 'MyApplicationProject'.
 > Failed to notify project evaluation listener.
    > Main Manifest missing from C:\...\MyApplicationProject\src\main\AndroidManifest.xml
Zoe
  • 27,060
  • 21
  • 118
  • 148
muetzenflo
  • 5,653
  • 4
  • 41
  • 82
  • check this answer http://stackoverflow.com/questions/16682223/how-do-i-structure-project-test-directory-in-android-studio – robotoaster Aug 20 '13 at 20:06
  • Thanks, but was exactly do you want to show me? I already know this post, but it's focused on test settings. Are the test folders required so that my res folders are recognized? – muetzenflo Aug 20 '13 at 20:31
  • you don't have to specify them as they by default are named as my screenshot shows. All I do is to add testPackageName and testInstrumentationRunner to default config. Also I am not using IDE for running test but gradle command – robotoaster Aug 20 '13 at 20:38
  • okay, although I don't think it has anything to do with the instrumentTest folder, I now created them. I also tried to list all possible gradle tasks by command line. Please see my edited question. – muetzenflo Aug 20 '13 at 21:03
  • 1
    i have encountered this missing manifest before, gradle plugin should generate test manifest by itself. It is difficult for me to guess why but here is my repo with working project. test should fail on assertion, check readme https://github.com/swavkulinski/android-studio-instrumentation-test – robotoaster Aug 21 '13 at 00:06
  • Hi thanks for your repolink. I compared the two projects, but couldn't find much difference. Only that you have your main gradle.build in the source-folders and not in the project root. I think that there is a bug in the newest Android Studio release. I created a new project with version 0.2.0 and everything was configured correctly...if I find out more, I'll post it here, but until now I can use the project created by Android Studio 0.2.0. Thanks for your help! – muetzenflo Aug 21 '13 at 13:26
  • http://tools.android.com/tech-docs/new-build-system/ more examples on the bottom of the page in zip – robotoaster Aug 21 '13 at 13:38

7 Answers7

6

Try add the following under defaultConfig (within android {...})

sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        resources.srcDirs = ['src']
        aidl.srcDirs = ['src']
        renderscript.srcDirs = ['src']
        res.srcDirs = ['res']
        assets.srcDirs = ['assets']
    }
}
powder366
  • 4,351
  • 7
  • 47
  • 79
  • Thank, I also tried that back then. But it was a bug in the Android Studio release. Since version 0.5.6 it works fine. – muetzenflo Nov 22 '13 at 09:15
  • @muetzenflo If that is the answer you should write it up and mark it as the answer. – ThomasW Jan 21 '14 at 04:26
  • Could you tell me where I can learn all the properties sourceSets contains? On [gradle page](http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.SourceSet.html) I can only find `java` and `resources`. – CopperCash Jul 04 '14 at 08:47
4

If you are having this problem in current Android Studio version, or something to the same extent (E.g. I added images to my drawable folder, and they were not recognized) your problem may be what I encountered.

If you ran into this problem after duplicating a project you were working on, then this new project was likely not imported correctly.

I made the mistake of copying and pasting the project in the IDE, renaming it (editing manifest), then trying to work on it. This will not work, because the new project is still accessing the original's res folder.

When duplicating a project, so as not to ruin your original you should:

  1. Go to your Android Studio project folder (Right click on your current project in the IDE, and select "Show In Explorer".
  2. Copy + Paste this project in the same directory; renaming folder accordingly to new version name.
  3. In IDE go to File > Import Project > ... And locate the directory of your new project.
  4. (Proceed to rename project in IDE accordingly. Change the package name in AndroidManifest.xml)

This way the IDE creates a new project for it, and it access the correct res folder (not that of the original).

Though this may not be the solution to many of you reading this question; I came across this question many times prior to discovering the proper way to duplicate a project.

I hope this can save someone some time.

OscuroAA
  • 252
  • 2
  • 17
1

Although it's a very old post, @ThomasW suggested to make this the official answer: It was a bug in Version 0.2.5 Ever since I use newer versions of Android Studio I didn't have this problem anymore.

muetzenflo
  • 5,653
  • 4
  • 41
  • 82
  • Hmm that's strange. I use AS on a daily basis and never had this problem since after 0.2.5 But with the new 0.8.1 release there were numerous changes with the internal project management/import. If you find a better solution, just post it here and I will mark it as correct one. Maybe try to empty cache and restart AS (available from the File-menu) – muetzenflo Jul 07 '14 at 14:15
  • I found, what was wrong in my case - I had two views in RelativeLayout, the first was using relation property with second (by id) - AndroidStudio has no problem with resolving such view for preview (there was everything fine), but gradle didn't build it. I had to change order of that views and now it's working fine. – Krystian Jul 08 '14 at 08:55
1

What worked for me was to create a copy of each file that it wasn't picking up in my Drawable folder and delete the old one. For some reason it wasn't picking up any resources in my drawable file. Tried everything else I could find on Google.

Bravo
  • 107
  • 1
  • 3
0

I struggled with this for a while and always had issues creating new maven project in AndroidStudio What finally worked is the following (tested with Android Studio 1.0.1)

  • Right click on POM file -> Maven -> Remove project (here if it complains, make sure to remove dependencies on the project)
  • Right click again on pom -> Maven -> Import project
jsk
  • 1
0

For those who come with the same problem in year 2023, Invalidate Caches and Restart worked for me.

Viktor Brešan
  • 5,293
  • 5
  • 33
  • 36
0

I always encounter the same problem when updating, renaming or adding new resources (strings, drawables, raw files, colors, etc. Without knowing the reason why, the problem is always solved by simply renaming the app's project folder and re-opening the project.

  • The reason for that might be that AS does not recognize the path of the project anymore and Invalidates existing caches. So your approach might technically be exaclty what works for some people: Invalidate Cache and Restart – muetzenflo Apr 18 '23 at 09:18
  • I tried Invalidate Cache and Restart but it didn't work most of the time. I also tried deleting the .gradle, .idea, and build folder but didn't solve the problem. Renaming the project folder always works for me. – Arnulfo B. Bataller Apr 19 '23 at 13:46