16

I am trying to build an Android project with Gradle and the Android Gradle plugin. I would like to depend on library projects found in external (maven) repositories, e.g. ActionBarSherlock.

This seems possible according to the official site:

Using a library is done one of the following way:

Multi-project setup. Read here: http://www.gradle.org/docs/current/userguide/multi_project_builds.html

Dependencies through a repo such as maven or ivy.

The current contents of my build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.2'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.actionbarsherlock:library:4.2.0'
}

android {
    target = 'android-16'
    sourceSets {
        main {
            manifest {
                srcFile 'AndroidManifest.xml'
            }
            java {
                srcDir 'src'
            }
            res {
                srcDir 'res'
            }
            assets {
                srcDir 'assets'
            }
            resources {
                srcDir 'src'
            }
        }
    }
}

I am using Gradle 1.2. When I try to build with gradle assemble, I get the following error:

Error: duplicate files during packaging of APK /[path to project]/build/apk/[project name]-debug-unaligned.apk
    Path in archive: AndroidManifest.xml
    Origin 1: /[path to project]/build/libs/[apk name]-debug.ap_
    Origin 2: /[home directory]/.gradle/caches/artifacts-14/filestore/com.actionbarsherlock/actionbarsherlock/4.2.0/apklib/dd63451a922558005d8c10be1085b488ed731d19/actionbarsherlock-4.2.0.apklib
:packageDebug FAILED

It seems like it is trying to include the AndroidManifest.xml from both the library project and my project. If I remove the manifest specification in sourceSets, I still get the same error.

The site mentions using apply plugin: 'android-library' for library projects; I am guessing this is only when building the actual library (with a multi-project setup) since doing so does not produce an APK.

How can I get external Android library project dependencies to work?

antonyt
  • 21,863
  • 9
  • 71
  • 70

3 Answers3

20

While it is possible to get dependencies through a Maven or Ivy repository, those are not packaged in a standard Jar archive. There is a new format designed specifically for this build system. The type is 'aar'

Current Android Libraries available on Maven Central were created using the Maven plugin which use a different archive format ('apklib') so they are not compatible.

If you look at com.actionbarsherlock:actionbacksherlock (which is the new location of that library), you'll see its format is apklib. http://search.maven.org/#artifactdetails%7Ccom.actionbarsherlock%7Cactionbarsherlock%7C4.2.0%7Capklib

Looking at the source code, we check for 'aar' packaging and revert to processing all others as 'jar' which is obviously a problem here. We should detect apklib and provide a better error message.

Xavier Ducrohet
  • 28,383
  • 5
  • 88
  • 64
  • Is the new aar format a compiled distribution like jar, or another raw source zip like apklib? – yorkw Mar 13 '13 at 03:31
  • With version 0.3 of the plugin, it seems like building an Android library project produces an AAR (archive with compiled classes and the resources). Until it is more widespread, is there a way to get the Android Gradle plugin to work with the existing apklib packaged libraries found on Maven Central (etc.)? EDIT: This would be particular useful considering the current message on the site: "Binary packaging of library is still going through changes. Do not upload libraries to maven central (or other similar repos) just yet." – antonyt Mar 13 '13 at 03:56
  • 17
    aar is similar to apklib in that it is a zip file containing the jar + other files. Supporting apklib is not really planned but I could look into it. – Xavier Ducrohet Mar 13 '13 at 04:51
  • 1
    @XavierDucrohet is there any chance in Android Studio to use that akplib files? If that matters I'm using gradle 1.10 in AS 0.4.4 – rekire Feb 14 '14 at 08:45
  • 1
    @rekire not at the moment. – Xavier Ducrohet Feb 14 '14 at 16:55
14

You can integrate the ActionBarSherlock as an aar now.

Just add this to your dependency definition in your build.gradle file:

dependencies {
    compile 'com.android.support:support-v4:18.0.+'
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'

Jake Wharton published an example project that shows how to use ActionBarSherlock with Gradle

Janusz
  • 187,060
  • 113
  • 301
  • 369
-1

You are using an older version of the android gradle plugin.
In the latest version 0.3 a lot has been improved in this area.

I would advise you to upstep to version 0.3 of this plugin. Be also aware that you then have to use a more recent version of gradle (1.3 or 1.4).

More information about how external libraries can be used in with this plugin.

Steven
  • 2,050
  • 23
  • 20