0

I'm working with my first project in Android Studio 2.2. When I try to Sync the project with the Gradle, it shows an error error: package org.hamcrest does not exist

Therefore I had downloaded the hamcrest.jar library and added manually it under myfirstproject\app\libs

But, I got the following error when I add hamcrest.jar to my project.

Could not find method compile() for arguments(file collection) on object of type org.gradle.api.internal.artifacts.DSL.dependencies.DefaultDependencyHandler 
  • This is my build.gradle file

    -

enter image description here

  • Have I missed anything during the installation of my Android Studio?

  • Or should I need to add any libraries to the project?

  • If needed how it is to be done?

Michel
  • 1,085
  • 13
  • 24
  • Did you made sure the library is under apps/libs? – liorsolomon Feb 26 '17 at 16:40
  • also check http://stackoverflow.com/questions/16608135/android-studio-add-jar-as-library and http://stackoverflow.com/questions/16779959/android-studio-importing-external-library-jar – liorsolomon Feb 26 '17 at 16:41

2 Answers2

3

You added that to the wrong gradle file. Read the comment in that file that says not to add app dependencies there

Open app/build.gradle and you should notice that you already have

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

}

So, which means you don't have to add anything else to compile any jar files.

You really should not use a Jar file, though.

Add this

testCompile 'org.hamcrest:hamcrest-library:1.3'
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • But again occurred some errors..like "failed to resolve:javax.inject:javax.inject:1 – Michel Feb 26 '17 at 09:33
  • I cant make my project – Michel Feb 26 '17 at 10:52
  • `javax.inject:javax.inject` is not anything in my answer. Please show both your Gradle files in your question as text, not images – OneCricketeer Feb 26 '17 at 15:31
  • 'Failed to resolve:junit:junit:4.12' what does it mean? Pls answer – Michel Feb 26 '17 at 17:09
  • 1
    I uninstalled my android studio and deleted all belonged files.Then downloaded a new android studio and installed.During my installation I had connected to a WiFi to download the required packages on time.Now my Gradle sync is completed successfully. – Michel Mar 06 '17 at 02:11
2

Hamcrest library is something that is automatically configured with Android Studio during the installation.

Here the error is due to the incomplete installation of Android Studio. That is, most of the important external packages haven't get downloaded during the installation (including hamcrest.jar).This happened may be due to the loss of network connection at the time of installation.


So the best solution is

  1. Uninstall the Android Studio,
  2. Delete all the belonged files (AndroidStudioProjects,.AndroidStudio2.2 and .android folders ). Don't forget to move your current project to any other folder.
  3. Once again install the Android Studio by making sure that you have a good network connection.
  4. Open your last project from where it was moved
  5. Sync the project with Gradle.

See that everything works fine!

Michel
  • 1,085
  • 13
  • 24
  • Excellent point. Happened to me because I already had a project and AS instead of going through the wizard just opened the project. When I almost accidentally, out of frustration closed the project the wizard resumed and installed missing components – Bostone Aug 09 '20 at 02:50