109

I've created a new Android project using the default wizard in Android Studio. Compiled, and deployed the app to my device. All is well.

Now I want to import an external library that is available on Maven. (http://square.github.io/picasso/). I went to module properties, and added a Maven library. It shows up correctly in the list of dependencies. In addition, it shows up in the editor and I can correctly use it in code.

However, at compile time, I get a Gradle error: unable to find class

Any ideas?

munkay
  • 1,883
  • 2
  • 16
  • 15

6 Answers6

87

I am using the springframework android artifact as an example

open build.gradle

Then add the following at the same level as apply plugin: 'android'

apply plugin: 'android'

repositories {
    mavenCentral()
}

dependencies {
   compile group: 'org.springframework.android', name: 'spring-android-rest-template', version: '1.0.1.RELEASE'
}

you can also use this notation for maven artifacts

compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'

Your IDE should show the jar and its dependencies under 'External Libraries' if it doesn't show up try to restart the IDE (this happened to me quite a bit)

here is the example that you provided that works

buildscript { 
    repositories { 
        maven { 
            url 'repo1.maven.org/maven2'; 
        } 
    } 
    dependencies { 
        classpath 'com.android.tools.build:gradle:0.4' 
    } 
} 
apply plugin: 'android'

repositories {
    mavenCentral()
}

dependencies { 
    compile files('libs/android-support-v4.jar') 
    compile group:'com.squareup.picasso', name:'picasso', version:'1.0.1' 
} 
android { 
    compileSdkVersion 17 
    buildToolsVersion "17.0.0" 
    defaultConfig { 
        minSdkVersion 14 
        targetSdkVersion 17 
    } 
} 
user1568967
  • 1,816
  • 2
  • 16
  • 18
  • 1
    I tried that, here's my build.gradle file: ``` buildscript { repositories { maven { url 'http://repo1.maven.org/maven2' } } dependencies { classpath 'com.android.tools.build:gradle:0.4' } } apply plugin: 'android' dependencies { compile files('libs/android-support-v4.jar') compile group:'com.squareup.picasso', name:'picasso', version:'1.0.1' } android { compileSdkVersion 17 buildToolsVersion "17.0.0" defaultConfig { minSdkVersion 14 targetSdkVersion 17 } } ``` Error: Could not find com.squareup.picasso:picasso:1.0.1. – munkay May 16 '13 at 19:38
  • 1
    @munkay I updated the answer with a corrected build.gradle that matches your comment – user1568967 May 16 '13 at 23:10
  • 18
    Key part is restarting the IDE. This version of IntelliJ is not picking up changes. – Jimmy2Times May 19 '13 at 00:13
  • Yes, after restart everything is ok. – firen Jun 05 '13 at 15:43
  • Why doesn't Android Studio do this by default?!?! Bug? – arinte Jun 18 '13 at 17:22
  • I am having the same trouble as @munkay restarting didn't help. I copied and pasted the example build.gradle inside the sub-module (rather than the main module which was empty as the answer suggested it has to be the same level as apply plugin :android) – Archimedes Trajano Jul 05 '13 at 05:57
  • I get the following message after restart Gradle 'MyQrCodeScannerProject' project refresh failed: Could not fetch model of type 'IdeaProject' using Gradle distribution 'http://services.gradle.org/distributions/gradle-1.6-bin.zip'. A problem occurred configuring project ':MyQrCodeScanner'. Could not resolve all dependencies for configuration ':MyQrCodeScanner:classpath'. Could not find com.android.tools.build:gradle:0.4. Required by: MyQrCodeScannerProject:MyQrCodeScanner:unspecified – Archimedes Trajano Jul 05 '13 at 05:58
  • A re-install of Android studio didn't help either. – Archimedes Trajano Jul 05 '13 at 07:57
  • Does your build work using the command line? If it does then I suggest you delete all the idea files for your project (*.iml, .idea folder) then try again in android studio. If your command line fails, please provide your build.gradle – user1568967 Jul 27 '13 at 15:35
  • Restarting the Android Studio was a missing piece for me. Only after restarting the IDE and clicking on the external libraries it loaded what was missing. – akohout Nov 20 '13 at 10:16
  • excuse me, i want to use "NineOldAndroids Library", but i have problem in Depency. i don't know how to do it??? i explain my problem in this link, http://stackoverflow.com/questions/28655344/nineoldandroids-library-for-drag-drop-in-android . please,please, please help me. – Mina Dahesh Feb 22 '15 at 08:03
  • @user3671748 your stackoverflow question was aswered – user1568967 Feb 23 '15 at 13:37
  • 1
    "Key part is restarting the IDE". omg. This is 2016... but yeah, thanks Jimmy2Times. – Guy Moreillon Jan 08 '16 at 16:56
  • I want to add the library given in this link https://github.com/Almeros/android-gesture-detectors kindly guide me. I've trying since last 4 hours but in vain – Husnain Iqbal Apr 22 '16 at 13:12
75

As of version 0.8.9, Android Studio supports the Maven Central Repository by default. So to add an external maven dependency all you need to do is edit the module's build.gradle file and insert a line into the dependencies section like this:

dependencies {

    // Remote binary dependency
    compile 'net.schmizz:sshj:0.10.0'

}

You will see a message appear like 'Sync now...' - click it and wait for the maven repo to be downloaded along with all of its dependencies. There will be some messages in the status bar at the bottom telling you what's happening regarding the download. After it finishes this, the imported JAR file along with its dependencies will be listed in the External Repositories tree in the Project Browser window, as shown below.

enter image description here

Some further explanations here: http://developer.android.com/sdk/installing/studio-build.html

dodgy_coder
  • 12,407
  • 10
  • 54
  • 67
  • 7
    Does not work without: repositories { mavenCentral() } – Johannes Dec 02 '15 at 11:54
  • 1
    Sometimes we cannot simply use in dependencies, adding externally will workout, sample here ... repositories { maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' } } ... compile 'com.google.code.ksoap2-android:ksoap2-android:3.6.0' – Nandha Jun 15 '17 at 08:50
9

Android Studio 3

The answers that talk about Maven Central are dated since Android Studio uses JCenter as the default repository center now. Your project's build.gradle file should have something like this:

repositories {
    google()
    jcenter()
}

So as long as the developer has their Maven repository there (which Picasso does), then all you would have to do is add a single line to the dependencies section of your app's build.gradle file.

dependencies {
    // ...
    implementation 'com.squareup.picasso:picasso:2.5.2'
}
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
8

In Android Studio Bumblebee, maven declarations in settings.gradle file (instead of build.gradle file like previous AS version)

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven {
           ....
        }
    }
}
Linh
  • 57,942
  • 23
  • 262
  • 279
5
  1. Uncheck "Offline work" in File>Settings>Gradle>Global Gradle Settings
  2. Resync the project, for example by restarting the Android Studio
  3. Once synced, you can check the option again to work offline.
Rinkesh
  • 3,150
  • 28
  • 32
0

Try itext. Add dependency to your build.gradle for latest as of this post

Note: special version for android, trailing "g":

dependencies {
    compile 'com.itextpdf:itextg:5.5.9'
}
CAMOBAP
  • 5,523
  • 8
  • 58
  • 93
tom
  • 2,190
  • 1
  • 23
  • 27