Add commons-io dependency to gradle project in Android Studio
Asked Answered
C

4

57

Very simple question - how to add commons-io dependency to gradle Android project?

I tried the following

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar')
    compile group: 'commons-io', name: 'commons-io', version: '2.0.1'
}

but it does not work The error is

Gradle: A problem occurred configuring project ':LearnIt'.

Failed to notify project evaluation listener. Could not resolve all dependencies for configuration ':LearnIt:_DebugCompile'. > Could not find commons-io:commons-io:2.0.1. Required by: learnit:LearnIt:unspecified

Cothurnus answered 27/7, 2013 at 7:49 Comment(1)
In case anyone needs all apache commons libs - mvnrepository.com/artifact/org.apache.commonsStenson
P
51

you need to declare a repository where you want to resolve the commons-io library from (e.g. MavenCentral):

buildscript {
    repositories {
        mavenCentral()
    }

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

apply plugin: 'android'

repositories{
    mavenCentral()
}

dependencies {
    compile files('libs/android-support-v4.jar')

    compile group: 'commons-io', name: 'commons-io', version: '2.0.1'
}    
Privilege answered 27/7, 2013 at 11:6 Comment(5)
see buildscript { repositories { mavenCentral() }Cothurnus
You have it configured inside buildScript, but not inside the build file itself. buildScript is used to declare additional dependencies needed by the custom tasks themselves, and not dependencies needed by the project being built. Also note that Rene is a gradle committer. He knows what he's talking about. See gradle.org/docs/current/userguide/…Rosalynrosalynd
ok thank you! Now it is clear, but was a little bit confusing at the beginningCothurnus
I am keep getting this error : Unable to load class 'org.apache.commons.io.FileUtils'. Android Studio 3.2.1Salmanazar
Something like this would work these days as well: implementation "commons-io:commons-io:2.4"Pitchfork
D
80

As of now (May 2014) if you use the default generated project it is actually amazingly simple (though difficult to find instructions!

Open the second level build.gradle, and add the following line to the dependencies {:

compile "commons-io:commons-io:+"

That will get the latest version of commons-io. My complete file looks like this:

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 18
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile "commons-io:commons-io:+"
}
Duad answered 22/5, 2014 at 15:15 Comment(5)
That works great - very useful to have IOUtils as part of the project.Eileen
Thanks for this but do not use + operator in dependencies. that is bad for version differences.Carpentaria
It has pros and cons.Duad
@OmerKarakose is right. It is better to stick to one version and not pull the latest always.Fennel
@Duad From where did u find it?Bearskin
P
51

you need to declare a repository where you want to resolve the commons-io library from (e.g. MavenCentral):

buildscript {
    repositories {
        mavenCentral()
    }

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

apply plugin: 'android'

repositories{
    mavenCentral()
}

dependencies {
    compile files('libs/android-support-v4.jar')

    compile group: 'commons-io', name: 'commons-io', version: '2.0.1'
}    
Privilege answered 27/7, 2013 at 11:6 Comment(5)
see buildscript { repositories { mavenCentral() }Cothurnus
You have it configured inside buildScript, but not inside the build file itself. buildScript is used to declare additional dependencies needed by the custom tasks themselves, and not dependencies needed by the project being built. Also note that Rene is a gradle committer. He knows what he's talking about. See gradle.org/docs/current/userguide/…Rosalynrosalynd
ok thank you! Now it is clear, but was a little bit confusing at the beginningCothurnus
I am keep getting this error : Unable to load class 'org.apache.commons.io.FileUtils'. Android Studio 3.2.1Salmanazar
Something like this would work these days as well: implementation "commons-io:commons-io:2.4"Pitchfork
D
48

Use gradlePlease to get the dependency.

Add the following to your app/build.gradle file:

dependencies {
    compile 'org.apache.commons:commons-io:1.3.2'
}

//UPDATED

implementation group: 'commons-io', name: 'commons-io', version: '2.6'
Deliquescence answered 20/11, 2015 at 6:28 Comment(3)
for a jar download -> commons.apache.org/proper/commons-io/download_io.cgiPerfectly
This did for me: compile 'commons-io:commons-io:2.4' - for jcentre() repo.Castellano
@Deliquescence From where did u find it?Bearskin
W
9

Update 2020 using gradle

// Home Page : https://commons.apache.org/

// IO - https://commons.apache.org/proper/commons-io/
implementation group: 'commons-io', name: 'commons-io', version: '2.7'

// String / Text 
implementation group: 'org.apache.commons', name: 'commons-text', version: '1.8'
Wimer answered 29/6, 2020 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.