27

First and foremost, I am aware of the existence of this question - How do I add a library project to Android Studio? - and unfortunately, it has not helped me.

My goal is rather simple. I want to write an Android app using the Android Studio IDE (0.2.11), and the Guava libraries in my code.

I do not know Gradle, I've only started using Android Studio and my Visual Studio/C# background has dumbed me down, for which I apologize (in that Mickey Mouse world, you typically just add a library reference and off you go).

I will document my steps with screenshots. I mostly followed advice given in this answer.

I created a libraries folder under my project folder.

enter image description here

I cloned Guava repository into it.

enter image description here

Files successfully appeared.

enter image description here

I went to Project Structure and selected Import Module.

enter image description here

enter image description here

I selected Create module from existing sources and agreed to all the default choices.

I updated my settings.gradle file to include ':libraries:guava', ':Test':

enter image description here

And my build.gradle file with compile project(":libraries:guava"):

enter image description here

But all I'm getting whenever I'm trying to rebuild the project is:

Error: Gradle: A problem occurred configuring project ':Test'.
> Failed to notify project evaluation listener.
> Configuration with name 'default' not found.

I did try putting a build.gradle as below in the guava folder:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android-library'

dependencies {
    compile 'com.android.support:support-v4:13.0.+'
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }
}

(as the aforementioned answer says).

I have googled up and down to find the "correct" build.gradle for Guava.

https://code.google.com/p/guava-libraries/wiki/UseGuavaInYourBuild - didn't help me, either.

I did try countless things which I will not describe here as they were rather haphazard - I tried adding a module dependency, I tried turning Use auto-import on in Gradle settings, etc.

I know it's not a way of solving issues and I promise I will diligently read Gradle's User Guide from 1 through 5.4.2 to 7.3, but I can't believe this is really prerequisite to achieve something as unremarkable as merely adding a library to a project? Why is there no default build.gradle file from which one could start to fiddle with all sorts of things if necessary?

And the real question - how do I create an app (in Android Studio) that builds, actually runs on an Android device and on the top of that allows me to use Guava so I could sort a map by values without writing 50 lines of code? :)

Sorry about the chatty tone of my question, I know the drill around here, it's just my way of venting my frustration off.

Judging by how many votes were casted for questions and answers that tackled similar issues, I'm sure I'm not the only one who would benefit from some more instructions. I would start a bounty on it straight away, but the rules forbid me.

Community
  • 1
  • 1
Konrad Morawski
  • 8,307
  • 7
  • 53
  • 91
  • BTW I'll try your suggestions and answer any questions tomorrow, since it's already nearly midnight here :) – Konrad Morawski Oct 07 '13 at 21:13
  • 1
    I used the thread you referenced at the beginning to add guava without any issues. BUT I tried now on the latest Android Studio and Project Structure no longer gives me access to add a library. Something changed. – Ryan Heitner Oct 12 '13 at 16:09

6 Answers6

48

If you just need to use a stable, released version of the Guava libraries, importing it is extremely easy.

Just go to the build.gradlefile of the module where you want to use the library (i.e GuavaTestProject/GuavaTest/build.gradle) and, right after

repositories {
    mavenCentral()
}

add a Maven dependency:

dependencies {
    compile group: 'com.google.guava', name: 'guava', version: '15.0'
}

Rebuild your project if needed and that's all (tested right now with a fresh project created with Android Studio 0.2.13).

If you really need to include the source code of the Guava library and compile it yourself as a module of your Gradle build that's an entirely different problem because Guava is build with Maven and so you need to move the Guava build system from Maven to Gradle, which I think is overwhelmingly complex for your goals.

If you just need to browse the source or view it while debugging, what I would do is:

  • Download Guava source code on a separate folder:

    git clone https://code.google.com/p/guava-libraries/
    git checkout v15.0
    
  • When Android Studio doesn't find the sources, click on "Attach sources" and point to this alternative location.

I think if you don't need to actually modify and compile Guava source code this is the easiest solution.

jlhuertas
  • 1,477
  • 10
  • 9
  • Thanks. I'm awarding the bounty to @RyanHeitner's comprehensive (and earlier) answer, but since you're correct as well I'm accepting yours as a way to give you points. You pointed out two important facts that I was missing: 1. `Guava is build with Maven and so you need to move the Guava build system from Maven to Gradle, which I think is overwhelmingly complex` (I'm fine with this explanation, I just lacked that knowledge before and tried to achieve my goal the hard way) 2. Since I can attach sources, I can debug my `jar` just as if I dealt with plain source. And this works indeed. Thank you – Konrad Morawski Oct 13 '13 at 19:32
  • 6
    That's ok, no problem about the bounty, I'm just trying to give back some of the knowledge others are giving to me, so I'm glad it worked! ;) – jlhuertas Oct 13 '13 at 21:33
11
dependencies {
  compile 'com.google.guava:guava:19.0'
}

Source: https://github.com/google/guava

EDIT: As of v22, there's specific guava version for Android.

dependencies {
  compile 'com.google.guava:guava:22.0-android'
}

* Thanks Sam :)

AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277
8

Thats a lot of info in your question. I am using Guava too. But I don't remember going through any of this trouble..

Guava is available on the mavencentral. So adding guava to your project should be fairly simple. AFAIK, you do not need to checkout, build and add a project dependency etc..

See gradle file for my app below.

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android'

repositories {
     mavenCentral()
}

// Dependencies
dependencies {
    compile fileTree(dir: 'libs', include: '*.jar') // jar files under the libs folder.
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar' // actionbarsherlock,
    compile 'com.android.support:support-v4:18.0.0' // android support lib
    compile 'com.google.code.gson:gson:2.2.4' // google GSON
    compile 'com.google.guava:guava:14.0.1' // guava 14.0.1
}
Varun
  • 33,833
  • 4
  • 49
  • 42
  • Thanks, I'll try this as well of course and let you know if it works. This by itself should shed some light on where the problem is. Still, I'd like to be able to checkout the full source code of the library I'm using, if I want to. Perhaps Guava itself isn't the best suited example, but any small open-source project, why not, it's good to be able to look the implementation up and debug it, if required – Konrad Morawski Oct 07 '13 at 21:30
  • Definitely. If you are looking at using the source as a library it should be very simple as well. The library project will use a `java` plugin in its `build.gradle`. – Varun Oct 07 '13 at 21:33
  • 1
    You should actually use 'com.google.guava:guava-jdk5:14.0.+' for Android. – Krylez Oct 07 '13 at 21:42
  • 1
    @Krylez only if you care about Android 2.2 and below, I think. Android 2.3 is based on jdk6. – Lesleh Oct 07 '13 at 21:56
  • @ Krylez Newer versions of android can work with java libraries compiled with java 1.6. As @Lesleh said, only if you care about Froyo and below. – Varun Oct 07 '13 at 22:04
  • @Varun - it works, I can confirm. Thanks. +1 for you, but I still don't know how to achieve this obtaining the source code (this is why I don't mark your answer) – Konrad Morawski Oct 09 '13 at 22:53
  • @KonradMorawski No issues. Working with source should not be that big of a deal. Let me try with a sample project. – Varun Oct 10 '13 at 00:59
  • @Krylez what is guava-jdk5 as opposed to regular guava? – Don Cheadle Apr 14 '15 at 18:42
6

do not use the full guava library, use the version specific for android,

dependencies {
    compile 'com.google.guava:guava:23.0-android'
}
Sam
  • 6,215
  • 9
  • 71
  • 90
2

In Android Studio 0.3.0 This is now working as it should on Mac. See the release notes for additional options in adding libraries http://tools.android.com/recent/androidstudio030released


I have been working on Windows now and there are two options Right click on a jar in libs and you have an add to library Or you can push F4 and get to open library settings so it seems you do not have to struggle as much on Windows as on a Mac


This is what I did. Please read to the end since it works inconsistently and if you do not see the blue arrow first try there is a resolution. First I created a libs folder. I do not think this is needed though but it is a habit.

enter image description here

First try go to File , Project Structure.

If you see a little blue arrow at the top left push it and you will go to the screen where you can add a library

enter image description here

If you manage to get to this screen below push the plus sign and add the library. enter image description here

You may also see a red line at the bottom saying Guava not used with a light bulb. This fill add the dependancy to the Gradle Build File enter image description here

IF YOU DONT SEE THE BLUE ARROW

GO instead to File, Other settings, Default project structure.

From that screen you can add the library enter image description here

Then go back to project structure and add it. The thing is that it will remain as a default for all your projects so you can add and remove it for each individual project via the project structure menu. I am not sure if this is a bug in Android Studio the fact that you can be blocked from adding it to the individual project without changing the default.

Ryan Heitner
  • 13,119
  • 6
  • 77
  • 119
  • So what exactly do you put in the `libs` folder? Just the .jar file? – Konrad Morawski Oct 13 '13 at 13:14
  • 1
    It does work, just the `compile fileTree(dir: 'libs', include: '*.jar')` line must be added to the gradle file (as Varun adviced). You are right about this pecularity of project settings in Android Studio, and this is an important observation. To me it's a bug, or at the very least it's counter-intuitive and user-unfriendly :( I'm awarding you the bounty, but I'll accept @jlhuertas answer as a way of giving him other 15 points, since his answer was helpful to me as well to an extent. Thank you – Konrad Morawski Oct 13 '13 at 19:28
2

For Android Studio 3.2.1:

Click the File/Project Structure menu (ctrl+alt+shift+S). Press the + button in the Dependencies tab from the app menu to add a Library dependency.

Look for com.google.guava:guava:(whatever number is the most recent one) and add it.

Add Guava to Android Studio project Add Guava to Android Studio project

Allfarid Morales García
  • 1,455
  • 2
  • 17
  • 27