5

I'm trying to build an android library that uses a java library 'core' (myLibrary-core). I've had problems get my dependencies to show up in my generated pom file, but I (think I) fixed it using the answer in this question: Gradle not including dependencies in published pom.xml

I'm now trying to solve why importing my library into a separate application gives this error. This is the error I get when I import the library into a project as a gradle dependency:

Error:Module version com.example:myLibrary:0.1.0 depends on libraries but is not a library itself

This is my build.gradle:

apply plugin: 'android-sdk-manager'
apply plugin: 'com.android.library'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
        classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.12.+'
    }
}

apply plugin: 'maven-publish'

group = 'com.example'
version = '0.1.0'

android {
    compileSdkVersion 19
    buildToolsVersion '19.1.0'

    defaultConfig {
        applicationId "com.example.android"
        minSdkVersion 11
        targetSdkVersion 19
        versionCode 1
        versionName "0.1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/DEPENDENCIES'
    }
}

dependencies {
//    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:20.0.0'
    compile 'com.example:myLibrary-core:0.1.2'
    compile 'com.android.support:support-v13:20.0.0'
    compile 'com.google.android.gms:play-services:+'
}

// build JAR file
task androidReleaseJar(type: Jar, dependsOn: assembleRelease) {
    from "$buildDir/intermediates/classes/release/"
}

publishing {
    publications {
        maven(MavenPublication) {
            artifact androidReleaseJar {
                classifier "sources"
            }

            //The publication doesn't know about our dependencies, so we have to manually add them to the pom
            pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')

                //Iterate over the compile dependencies (we don't want the test ones), adding a <dependency> node for each
                configurations.compile.allDependencies.each {
                    def dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', it.group)
                    dependencyNode.appendNode('artifactId', it.name)
                    dependencyNode.appendNode('version', it.version)
                }
            }
        }
    }
}

}

This generates the pom file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>myLibrary</artifactId>
  <version>0.1.0</version>
  <packaging>pom</packaging>
  <dependencies>
    <dependency>
      <groupId>com.android.support</groupId>
      <artifactId>appcompat-v7</artifactId>
      <version>20.0.0</version>
    </dependency>
    <dependency>
      <groupId>com.example</groupId>
      <artifactId>myLibrary-core</artifactId>
      <version>0.1.2</version>
    </dependency>
    <dependency>
      <groupId>com.android.support</groupId>
      <artifactId>support-v13</artifactId>
      <version>20.0.0</version>
    </dependency>
    <dependency>
      <groupId>com.google.android.gms</groupId>
      <artifactId>play-services</artifactId>
      <version>+</version>
    </dependency>
  </dependencies>
</project>

The odd thing is if I remove play-services, support-v13 and appcompat-v7, the error goes away. I can't think of anything else to try so I'm happy to try any suggestion made.

Community
  • 1
  • 1
jonalmeida
  • 1,206
  • 1
  • 9
  • 24
  • I know how to do this with eclipse adt. alt-enter on each project. Library click islibary and the other project you click the library project. – danny117 Sep 16 '14 at 04:50
  • @danny117 This doesn't seem related to my situation though. Is the pom file properly generated AND you can can import your library project in an application via gradle? – jonalmeida Sep 16 '14 at 06:21
  • I actually only know the one method for eclipse. When android studio is out of beta I'll look at it. Take a look here setting up the gradle with Android Studio for a library project. http://developer.android.com/sdk/installing/studio-build.html – danny117 Sep 16 '14 at 18:11
  • I don't think you've understood my question.. – jonalmeida Sep 16 '14 at 18:13
  • I know I'm actually very weak with gradle maven builds. I'm just trying to help. Good luck with the Library import. ehh what was the IDE or maybe your building on a automated nightly build server. – danny117 Sep 16 '14 at 18:24

1 Answers1

7

The Android plugin can't consume POM dependencies; it can only deal with JAR or AAR libraries. That's what this means: com.example:myLibrary depends on libraries (appcompat-v7, myLibrary-core, support-v13, and play-services) but is not a library itself (the Android Gradle plugin doesn't deal with <packaging>pom</packaging>).

I know it's confusing because Android-Gradle digests the module's .pom file to resolve its dependencies and such, but it can't deal with libraries that use that as their packaging.

If you want to create a module that can be consumed by the Android-Gradle plugin, it will need to be packaged as JAR or AAR.

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • Thanks Scott! I'll have to rethink how I package my library in that case. – jonalmeida Sep 17 '14 at 17:08
  • That's a massive limitation right? I mean, probably 90% of libraries will have a dependency on another library. Or am I missing something? – Alex Dean Sep 17 '14 at 17:46
  • If you have an Android library in the Gradle build system (not a plain Java library, but one which uses Android APIs and contains Android resources and build artfiacts), then it can only rely on other libraries packaged as JAR or AAR. That doesn't seem to be a severe limitation; JAR is pretty standard, and AAR is the way Android libraries are packaged from Gradle builds. It would be nice if it could support apklib as well, but that doesn't seem likely. – Scott Barta Sep 17 '14 at 17:51
  • 1
    I believe what @AlexDean means is that it seems limited compared to how a maven works, which lets you define dependencies and have them pulled down from the repository before you build it, rather than package every library you use. – jonalmeida Sep 18 '14 at 22:23