7

The past two days i have tried to find a solution to this problem but with no luck.

I am trying to include GSON lib. Into my android project.

Here is a picture of my folder structure:

Folder

Now in my build.gradle i have the following:

    // Top-level build file where you can add configuration options common to all sub-projects/modules.

dependencies {
    compile files('libs/gson-2.2.4.jar')
}

i have also right clicked gson-2.2.4.jar and added it as a lib.

When i compile i get the following error:

    Gradle: A problem occurred evaluating root project 'notebox-android'.
> Could not find method compile() for arguments [directory 'libs'] on root project 'notebox-android'.

Can anyone help me with this?

please tell me if you need more information!

Marc Rasmussen
  • 19,771
  • 79
  • 203
  • 364

2 Answers2

10

The answer to this question might help you to import your gson library correctly and avoid any compile/link issues: Android Studio - Importing external Library/Jar. Another related link: Android Studio: Add jar as library? with accepted answer. Both answers say the same thing. gradlew clean after you add/import the jar file and edit the build.gradle file.

HTH.

Edit: There are two build.gradle files. You should be editing the build.gradle that's under your project folder and not the one under your project-root folder.

Community
  • 1
  • 1
VJ Vélan Solutions
  • 6,434
  • 5
  • 49
  • 63
2

I had this same problem when I added the v7 appcompat repository.

Initially I thought that Android Studio 0.5.4 had not updated the build.gradle file, but per Marc's warning above I found that I was editing the wrong build.gradle file: I edited the one in the project folder, and not the one in the app folder. Theone in the app folder is automatically updated with the compile() directive for the appcompat repository and support library when they are added with the SDK Manager.

So the problem turned out to be that I had neglected to adjust the minSdkVersion and targetSdkVersion settings in the build.gradle file in the app folder:

I had to change it from:

    defaultConfig {
    minSdkVersion 10
    targetSdkVersion 19
    versionCode 1
    versionName "1.0"
}

to:

    defaultConfig {
    minSdkVersion 7
    targetSdkVersion 17
    versionCode 1
    versionName "1.0"
}

To match the back compatibility with Android 2.1 (i.e. v7 appcompat).

Another tip (if it even qualifies as one):

I am adding v2.1 backwards compatible support for the action bar, and it turns out that in 0.5.4 (which I doubt is different to 0.5.3 with respect to this issue) you need to import ActionBarActivity into the main activity .java class file:

import android.support.v7.app.ActionBarActivity;

and not:

import android.support.v7.app.ActionBar; //Android Studio 0.5.4 IDE lists this as unused.

as the beginner project at http://developer.android.com/training/basics/actionbar/setting-up.html indicates.

The build worked when I made the above two adjustments.

Bruce Long
  • 713
  • 3
  • 9
  • 28