122

Im trying to use ListUtils but when I ran the app I got this error:

 Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.commons.collections.ListUtils" on path: DexPathList[[zip file "/data/app/com.meridianaspect.wiw-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]

So I guess I have to import that library via gradle somehow, but I dont know how to do that?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180
  • HttpClient was already part of Android but obsolete from API 23+: https://stackoverflow.com/a/32157466/1085264 – straya Jan 03 '20 at 02:14

2 Answers2

492

Pick the ones you need and add them to build.gradle's dependencies block:

I recommend checking the link to make sure you are getting the latest version. Feel free to suggest edits.

collections: (link)

implementation 'org.apache.commons:commons-collections4:4.4'

lang: (link)

implementation 'org.apache.commons:commons-lang3:3.9'

compress: (link)

implementation 'org.apache.commons:commons-compress:1.19'

net: (link)

implementation 'commons-net:commons-net:3.6'

io: (link)

implementation 'commons-io:commons-io:2.6'

exec (link)

implementation 'org.apache.commons:commons-exec:1.3'

codec (link)

implementation 'commons-codec:commons-codec:1.13'

http (link)

implementation 'org.apache.httpcomponents:httpclient:4.5.10'

validator (link)

implementation 'commons-validator:commons-validator:1.6'

math (link)

implementation 'org.apache.commons:commons-math3:3.6.1'

See all of the Apache libraries here

Simson
  • 3,373
  • 2
  • 24
  • 38
Chad Bingham
  • 32,650
  • 19
  • 86
  • 115
8

Place the jar file in the libs folder in the root of your module. Then File -> Project Settings. In the left side choose the module where you want to add this lib, in the right side choose tab Dependencies. In the bottom press Plus sign and click File dependency. Then choose your jar and sync project

Oleg Osipenko
  • 2,409
  • 1
  • 20
  • 28
  • 7
    Is there a way to do this just by placing some text in Gradle and having to deal with jar files and modules? – Kaloyan Roussev May 15 '15 at 12:14
  • @J.K. you mean simply drop jars and not specifying each jar? – Oleg Osipenko May 15 '15 at 12:16
  • 3
    No, I mean not dropping any jars, just telling gradle what repo to get them from – Kaloyan Roussev May 15 '15 at 12:21
  • like this, for example: compile 'com.bartoszlipinski.recyclerviewheader:library:1.2.0' – Kaloyan Roussev May 15 '15 at 12:21
  • 5
    Try 'compile 'org.apache.commons:commons-collections4:4.0' – Oleg Osipenko May 15 '15 at 12:25
  • 1
    on pressing plus sign another option is Library dependency and there is search - type the name and search for the library you interested in – Oleg Osipenko May 15 '15 at 12:27
  • gradle based projects should not be adding dependencies by editing the Project Settings, they should be adding them as dependencies in the build.gradle – Novaterata Apr 27 '21 at 12:23
  • @Novaterata any source for such declaration? – Oleg Osipenko Apr 28 '21 at 13:48
  • In the first place, It's apache commons, they should be using a maven dependency, not the jar file. Here is how to do it with the GUI, but it's easy enough to just add it to your build.gradle https://www.jetbrains.com/help/idea/work-with-gradle-dependency-diagram.html#gradle_generate – Novaterata Apr 28 '21 at 13:54
  • @Novaterata even in case of adding not jar, but maven dependency, what's the problem of using UI? It ends up in the same `build.gradle` file And I haven't heard, that using UI is deprecated, please send me the link for that announcement – Oleg Osipenko Apr 28 '21 at 14:03
  • @OlegOsipenko I must be misunderstanding, whenever I had done something like this in the past it did not update the build.gradle, but just the IntelliJ project file. Apologies if that's no longer the case. – Novaterata Apr 28 '21 at 14:23
  • @Novaterata not sure about IntelliJ. But in the question it was said about Android Studio. Although it's built on top of IntelliJ Idea, it still differs. Maybe that is one of differences. But not sure. In AS it's definitely doesn't matter how you do it, via UI or by hand – Oleg Osipenko Apr 28 '21 at 15:06