5

Hi I have a Android Library Project which produces an AAR.

All is good but when I use the AAR in another project I get this error:

java.lang.NoClassDefFoundError: com.squareup.picasso.Picasso

The AAR makes use of picasso, is it possible to export the dependencies of the AAR as well when generating one?

redDragonzz
  • 1,543
  • 2
  • 15
  • 33
  • How are you using the AAR in the other project? – CommonsWare Apr 13 '14 at 12:47
  • Like this: `repositories { flatDir { dirs 'libs' } mavenCentral() }` and then including them using the usual groupId format – redDragonzz Apr 13 '14 at 16:29
  • This is supposed to be the expected behavior I suppose: https://groups.google.com/forum/#!topic/adt-dev/0ZAP8AVUZVw ! I think i must download the picasso jar and then gradle will include it automatically, otherwise it doesnt. Will give it a try and post an answer – redDragonzz Apr 13 '14 at 16:45
  • I have never used `flatDir`. I publish my AARs to a local Maven repo, and it resolves dependencies from there, with no issue. – CommonsWare Apr 13 '14 at 16:51
  • using jars instead of maven dependencies works. I guess the idea is you can't mix the two `flatDir` and pure remote dependencies. How can one go about setting up a local maven repo. – redDragonzz Apr 13 '14 at 16:58

1 Answers1

6

How can one go about setting up a local maven repo.

WARNING: the following recipe works, but probably could use improvement, as I am far from a Maven expert. I hammered out this approach last year for use with my CWAC libraries.

Step #1: Add classpath 'com.github.dcendents:android-maven-plugin:1.0' to your buildscript dependencies block in your library project's build.gradle file. Also add version and group statements to provide that information about your AAR.

Step #2: Use gradle install to compile the AAR and install it in the default local Maven repository.

Step #3: Ordinarily, you would add mavenLocal() to the dependencies block of your application project to pick up the AAR via its artifact ID. That may be working again, though it was broken for a bit. Instead, use maven { url "${System.env.HOME}/.m2/repository" } as a short-term workaround.

So, for example, your library project build.gradle file might contain:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
        classpath 'com.github.dcendents:android-maven-plugin:1.0'
    }
}

apply plugin: 'android-library'
apply plugin: 'android-maven'

version '0.4.0'
group 'some.likely.group.name.goes.here'

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.squareup.picasso:picasso:2.2.0'
    compile fileTree(dir: 'libs', include: '*.jar')
}

android {
  // as normal
}

You would use gradle install to publish the JAR to your local Maven repo. Your application project would then have the following stuff in its build.gradle:

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

apply plugin: 'android'

repositories {
    mavenCentral()
    maven { url "${System.env.HOME}/.m2/repository" } // mavenLocal()
}

dependencies {
    compile 'some.likely.group.name.goes.here:name-of-library:0.4.0'
}

android {
    // as normal
}

where you replace:

  • some.likely.group.name.goes.here with something

  • 0.4.0 with a version number in X.Y.Z format

  • name-of-library will be the directory name that contains the Android library project (e.g., presentation or foo)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • One important thing to note for anyone still using this method like I am: this has since been updated for newer gradle versions. Please refer to the new GitHub here: https://github.com/dcendents/android-maven-gradle-plugin and make sure gradle is at least 2.4 by changing `gradle-wrapper.properties` using instructions here: http://tools.android.com/tech-docs/new-build-system/migrating-to-1-0-0 – anddev84 Sep 28 '15 at 21:55
  • Please how to use `gradle install` ? I installed gradle for java on windows and tried `gradle install`, I got this error `Task 'install' not found in root project 'bin'.` – William Kinaan Nov 28 '16 at 16:00
  • @WilliamKinaan: This answer is 2.5 years old. I recommend finding a newer solution. – CommonsWare Nov 28 '16 at 16:01
  • @CommonsWare It makes sense, but I've been reading about Gradle and I just want to know, *just for personal use* how to apply gradle install – William Kinaan Nov 28 '16 at 16:02
  • @WilliamKinaan: Either you are missing the cited plugin, or the plugin changed in the past 2.5 years. I am no longer using that plugin, as the standard `maven` plugin and its `uploadArchives` task works fine with modern versions of Android Studio/Android Plugin for Gradle. – CommonsWare Nov 28 '16 at 16:07