Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing my android library in an app isn't recognized as a library

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.

like image 973
jonalmeida Avatar asked Aug 30 '14 18:08

jonalmeida


People also ask

How do I import library to Android?

Click on “Import Existing Project“. Step 2: Select the desired library and the desired module. Then click finish. Android Studio will import the library into your project and will sync Gradle files.


1 Answers

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.

like image 178
Scott Barta Avatar answered Nov 13 '22 06:11

Scott Barta