在应用程序中导入我的Android库不会被识别为库

时间:2014-08-30 18:27:09

标签: android maven gradle android-studio

我正在尝试构建一个使用Java库'核心' (myLibrary-core)。我有问题让我的依赖项出现在我生成的pom文件中,但我(我想)用这个问题中的答案修复了它:Gradle not including dependencies in published pom.xml

我现在正试图解决为什么将我的库导入单独的应用程序会出现此错误。这是我将库作为gradle依赖项导入项目时得到的错误:

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

这是我的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)
                }
            }
        }
    }
}

}

这会生成pom文件:

<?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>

奇怪的是,如果删除play-services,support-v13和appcompat-v7,错误就会消失。我无法想到任何其他事情,所以我很乐意尝试任何建议。

1 个答案:

答案 0 :(得分:7)

Android插件无法使用POM依赖项;它只能处理JAR或AAR库。这意味着什么:com.example:myLibrary取决于库(appcompat-v7,myLibrary-core,support-v13和play-services),但不是库本身(Android Gradle插件没有&#39;处理<packaging>pom</packaging>)。

我知道这让人感到困惑,因为Android-Gradle会消化模块的.pom文件以解决其依赖关系等问题,但它无法处理将其作为包装使用的库。< / p>

如果您想创建一个可以被Android-Gradle插件使用的模块,则需要将其打包为JAR或AAR。