Best way to incorporate Volley (or other library) into Android Studio project
Asked Answered
R

9

82

I've seen different advice on the best way to do this This question covers creating a jar. Elsewhere, I've seen advice to simply copy the volley source into your own project. This section on libraries at android.com would seem the most authoritative. However, after compiling volley, I don't have an aal library, whereas that section says I should have.

So my question is this: I have an existing Android Studio project with a standard layout, and a git repository; what should I do to add volley? Where should I download it to? How should I add it to Android Studio? Which Gradle files, if any, do I need to modify.

Hopefully, for those of you have done this a few times, this should be bread-and-butter stuff, but I haven't been able to find a straightforward description.

--

Updating, per Scott Barta's suggestion.

The gradle.build file in the volley repository has this line.

apply plugin: 'android-library'

According to the documentation: "Library projects do not generate an APK, they generate a .aar package (which stands for Android archive)." However, when I build the volley project, no .aar is created.

My feeling is that as Volley is a library project, created by the Android team, it is most probably intended to be generated and used as .aar package. Any advice on whether it would be preferable to generate a .aar, and how to do that, would be appreciated.

Refract answered 11/1, 2014 at 17:28 Comment(5)
possible duplicate of Android Studio + VolleyDeference
That should get you most of the way there. If you're still stuck and having problems, refine your question or ask a new one if its substantially different.Deference
If you want to use Volley as a dependency module, you can refer link.Cayes
Note that @ScottBarta's link has been marked as a duplicate of volley android networking library.Readjust
There is very detailed explanation for achieving this in the following link : gitsubmoduleasandroidtudiomodule.blogspot.inPyle
L
125

LATEST UPDATE:

Use the official version from jCenter instead.

dependencies {
    compile 'com.android.volley:volley:1.0.0'
}

The dependencies below points to deprecated volley that is no longer maintained.

ORIGINAL ANSWER

You can use this in dependency section of your build.gradle file to use volley

  dependencies {
      compile 'com.mcxiaoke.volley:library-aar:1.0.0'
  }

UPDATED:

Its not official but a mirror copy of official Volley. It is regularly synced and updated with official Volley Repository so you can go ahead to use it without any worry.

https://github.com/mcxiaoke/android-volley

Lizzettelizzie answered 11/1, 2014 at 20:48 Comment(7)
Import to note that is not the official repo for Volley. That's just something someone uploaded to maven central. (AFAIK)Eldwin
ya I know that man but there is not difference just a mirror, exactly same and synced with official volley repository1.0.1 (updated at 2014.02.13) . That is the beauty of open source so don't worry you can use it. github.com/mcxiaoke/android-volleyLizzettelizzie
Not trying to get all paranoid on everyone but the maintainer of that repository could quite easily slip in some shady stuff... Proceed with caution if you're not personally auditing the repository.Comeon
See my comment below to include volley from google's repo as a submodule in your project.Facultative
Just FYI, as of 2015-09-05, this repo incorporates minor fixes from third party in addition to the official repo (it is in fact a fork instead of a mirror now). However I still decided to use this fork.Algin
What is the difference between this two links so ? Thank you. http://mvnrepository.com/artifact/com.mcxiaoke.volley/library http://mvnrepository.com/artifact/com.mcxiaoke.volley/library-aarSensitometer
Please see my answer below, there is now an official Google-published Volley target (com.android.volley:volley:1.0.0)Biysk
F
137

As pointed out by others as well, Volley is officially available on Github:

Add this line to your gradle dependencies for volley:

compile 'com.android.volley:volley:1.0.0'


To install volley from source read below:

I like to keep the official volley repository in my app. That way I get it from the official source and can get updates without depending on anyone else and mitigating concerns expressed by other people.

Added volley as a submodule alongside app.

git submodule add -b master https://github.com/google/volley.git volley

In my settings.gradle, added the following line to add volley as a module.

include ':volley'

In my app/build.gradle, I added a compile dependency for the volley project

compile project(':volley')

That's all! Volley can now be used in my project.

Everytime I want to sync the volley module with Google's repo, i run this.

git submodule foreach git pull
Facultative answered 22/6, 2015 at 23:40 Comment(7)
Why is this not the preferred solution? As I see it you get the official version, easy updates, and a semantic tie between your project and its dependencies. Is there any good reason to choose the other methods, or is it just that the other methods are standard procedure in the Android community?Gorblimey
I think this is the preferred solution but not documented properly by google. Also, apart from the other security concerns mentioned above, getting it from the official source puts you in control to update the library (as soon or as late as you like).Facultative
great, thanks. only thing I needed to add was the location of the volley library in settings.gradle: project(':volley').projectDir = new File('libraries/volley') because I placed it in a libraries folderCavallaro
It should not be necessary to ask version control to do a package manager's job.Grolier
Added an example in github which might help others to make this setup. github.com/arpitratan/AndroidGitSubmoduleAsModule/commit/…Pyle
This is one of the best way to include any 3rd party dependency, with source code. Lot of times we need to tweak source code and this comes handy. Thanks a lot.Linsk
The git repository has been changed: https://github.com/google/volley.gitCittern
L
125

LATEST UPDATE:

Use the official version from jCenter instead.

dependencies {
    compile 'com.android.volley:volley:1.0.0'
}

The dependencies below points to deprecated volley that is no longer maintained.

ORIGINAL ANSWER

You can use this in dependency section of your build.gradle file to use volley

  dependencies {
      compile 'com.mcxiaoke.volley:library-aar:1.0.0'
  }

UPDATED:

Its not official but a mirror copy of official Volley. It is regularly synced and updated with official Volley Repository so you can go ahead to use it without any worry.

https://github.com/mcxiaoke/android-volley

Lizzettelizzie answered 11/1, 2014 at 20:48 Comment(7)
Import to note that is not the official repo for Volley. That's just something someone uploaded to maven central. (AFAIK)Eldwin
ya I know that man but there is not difference just a mirror, exactly same and synced with official volley repository1.0.1 (updated at 2014.02.13) . That is the beauty of open source so don't worry you can use it. github.com/mcxiaoke/android-volleyLizzettelizzie
Not trying to get all paranoid on everyone but the maintainer of that repository could quite easily slip in some shady stuff... Proceed with caution if you're not personally auditing the repository.Comeon
See my comment below to include volley from google's repo as a submodule in your project.Facultative
Just FYI, as of 2015-09-05, this repo incorporates minor fixes from third party in addition to the official repo (it is in fact a fork instead of a mirror now). However I still decided to use this fork.Algin
What is the difference between this two links so ? Thank you. http://mvnrepository.com/artifact/com.mcxiaoke.volley/library http://mvnrepository.com/artifact/com.mcxiaoke.volley/library-aarSensitometer
Please see my answer below, there is now an official Google-published Volley target (com.android.volley:volley:1.0.0)Biysk
V
26

Nowadays

dependencies {
    compile 'com.android.volley:volley:1.0.0'
}   

A lot of different ways to do it back in the day (original answer)

  • Use the source files from git (a rather manual/general way described here)

    1. Download / install the git client (if you don't have it on your system yet): http://git-scm.com/downloads (or via git clone https://github.com/git/git ... sry bad one, but couldn't resist ^^)
    2. Execute git clone https://android.googlesource.com/platform/frameworks/volley
    3. Copy the com folder from within [path_where_you_typed_git_clone]/volley/src to your projects app/src/main/java folder (Integrate it instead, if you already have a com folder there!! ;-))

    The files show up immediately in Android Studio. For Eclipse you will have to right-click on the src folder and press refresh (or F5) first.

  • Use gradle via the "unofficial" maven mirror

    1. In your project's src/build.gradle file add following volley dependency:

      dependencies {
          compile fileTree(dir: 'libs', include: ['*.jar'])
          // ...
      
          compile 'com.mcxiaoke.volley:library:1.+'
      }
      
    2. Click on Try Again which should right away appear on the top of the file, or just Build it if not

    The main "advantage" here is, that this will keep the version up to date for you, whereas in the other two cases you would have to manually update volley.

    On the "downside" it is not officially from google, but a third party weekly mirror.

    But both of these points, are really relative to what you would need/want. Also if you don't want updates, just put the desired version there instead e.g. compile 'com.mcxiaoke.volley:library:1.0.7'.

Vine answered 27/10, 2014 at 12:3 Comment(7)
In Android Studio 1.0.2, you don't need to right-click on the jar to add it. The default Gradle rules add it automatically. Also, if you're a new Studio user, you might be surprised that the jar doesn't show up in the Project View. It's filtered out by the "Android" setting in the spinner just above the view. Change it to "Project" to see that you've actually accomplished something.Readjust
Good to know, thx! Removed it from the answer! But as remark for beta users: If you choose the first option (adding it as library), you will also need to right-click on volley.jar within the libs folder in Android Studio, and choose Add As Library... to have it available in your project.Vine
@Levit, Regarding the second bullet point - " Use the source files from git (a rather manual/general way described here)" Im using android studio I got the libraries added to my project and edited the settings.gradle and dependencies. It is working fine but I get a message saying unregistered Vcs root detected, is under git, but is not registered in settings, could you explain this pleaseSixpence
@johncarter: With the second bullet point / way, you should be fine just by placing it in the right folder, without editing your settings and dependencies. But there is also a more or (rather) less comprehensive guide concerning this way on AndroidDevelopers: developer.android.com/training/volleyVine
Thanks for the great answer. Volley.jar method worked for me out of the box!Palembang
Tried 2nd bullet. I had to useLibrary 'org.apache.http.legacy' to make it to resolve.Dodecanese
The 2nd one is actually what is suggested by google: developer.android.com/training/volley/index.htmlVine
B
11

As of today, there is an official Android-hosted copy of Volley available on JCenter:

compile 'com.android.volley:volley:1.0.0'

This was compiled from the AOSP volley source code.

Biysk answered 12/2, 2016 at 19:30 Comment(1)
Thanks for posting! This should be marked as the correct answer.Burley
N
5

UPDATE:

compile 'com.android.volley:volley:1.0.0'

OLD ANSWER: You need the next in your build.gradle of your app module:

dependencies {
        compile 'com.mcxiaoke.volley:library:1.0.19'
        (Rest of your dependencies)

    }

This is not the official repo but is a highly trusted one.

Norrisnorrv answered 22/7, 2014 at 15:58 Comment(1)
Beware. The volley library hosted here is outdated and not officially hosted. See my post at the bottom.Facultative
S
1

For incorporate volley in android studio,

  1. paste the following command in terminal (
    git clone https://android.googlesource.com/platform/frameworks/volley ) and run it.

    Refer android developer tutorial for this.

    It will create a folder name volley in the src directory.
  2. Then go to android studio and right click on the project.
  3. choose New -> Module from the list.
  4. Then click on import existing Project from the below list.
  5. you will see a text input area namely source directory, browse the folder you downloaded (volley) and then click on finish.
  6. you will see a folder volley in your project view.
  7. the switch to android view and open the build:gradle(Module:app) file and append the following line in the dependency area:

    compile 'com.mcxiaoke.volley:library-aar:1.0.0'

  8. Now synchronise your project and also build your project.

Swint answered 23/12, 2015 at 7:33 Comment(0)
A
0

I have set up Volley as a separate Project. That way its not tied to any project and exist independently.

I also have a Nexus server (Internal repo) setup so I can access volley as
compile 'com.mycompany.volley:volley:1.0.4' in any project I need.

Any time I update Volley project, I just need to change the version number in other projects.

I feel very comfortable with this approach.

Araarab answered 17/9, 2015 at 20:5 Comment(0)
M
0

add

compile 'com.mcxiaoke.volley:library:1.0.19'
        compile project('volley')

in the dependencies, under build.gradle file of your app

DO NOT DISTURB THE build.gradle FILE OF YOUR LIBRARY. IT'S YOUR APP'S GRADLE FILE ONLY YOU NEED TO ALTER

Margenemargent answered 3/7, 2016 at 6:21 Comment(0)
G
0

This solution is for Kotlin DSL (build.gradle.kts):

dependencies {
    implementation("com.android.volley:volley:1.2.1")
    // ...
}
Glassman answered 18/2, 2022 at 11:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.