How do I add Guava to my Android Studio project?

一世执手 提交于 2019-11-28 21:20:15

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.

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
}
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 :)

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

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

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.

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

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

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

IF YOU DONT SEE THE BLUE ARROW

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

From that screen you can add the library

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!