android - Android库-为什么不下载我的依赖项? (等级/Maven)

标签 android maven gradle

我试图弄清楚如何将Android Library项目分发给某些Beta用户,但是在分发并将其用于示例项目时遇到了一些问题。我正在尝试分发AAR文件。

我的示例项目中的所有内容都可以正常编译,但出现错误:java.lang.NoClassDefFoundError:com.google.gson.Gson,当我调用使用GSON的代码时。

我的库的build.gradle文件如下所示:

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

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: exampleMavenUrl ) {
                authentication(userName: exampleMavenUser, password: exampleMavenPassword)
            }
            pom.groupId = 'com.example.android'
            pom.artifactId = 'example-api'
            pom.version = '0.1-SNAPSHOT'
            pom.packaging = 'aar'
        }
    }
}


android {
    compileSdkVersion 19
    buildToolsVersion "19.1.0"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
        versionCode 1
        versionName "0.1"
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.squareup.okhttp:okhttp:1.3.0'

    compile 'org.springframework.android:spring-android-core:1.0.1.RELEASE'
    compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
}

当我运行时:gradlew clean uploadArchives成功将其上传到我的快照关系存储库。

在我的示例项目的build.gradle中引用它:
apply plugin: 'android'

repositories {
    mavenCentral()
    maven(){
        url "http://repos.example.com/nexus/content/repositories/android-snapshot"
    }
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.1.0"

    defaultConfig {
        packageName "com.example.sample.app"
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:19.+'
    compile('com.example.android:example-api:0.1-SNAPSHOT')
}

这是gradle生成并上传的pom文件,其内容类似于:

http://repos.example.com/nexus/content/repositories/android-snapshot/com/example/android/example-api/0.1-SNAPSHOT/example-api-0.1-20140602.145158-1.pom

它位于aar文件的旁边。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.android</groupId>
    <artifactId>example-api</artifactId>
    <version>0.1-SNAPSHOT</version>
    <packaging>aar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework.android</groupId>
            <artifactId>spring-android-rest-template</artifactId>
            <version>1.0.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.4</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp</groupId>
            <artifactId>okhttp</artifactId>
            <version>1.3.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.android</groupId>
            <artifactId>spring-android-core</artifactId>
            <version>1.0.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

当我运行gradlew androidDependencies时,它仅列出我在本地库文件夹中包含的内容。
:app:androidDependencies
debug
\--- com.example.android:example-api:0.1-SNAPSHOT
     +--- LOCAL: android-logging.jar
     +--- LOCAL: Context-Core.jar
     \--- LOCAL: Context-Location.jar

debugTest
No dependencies

release
\--- com.example.android:example-api:0.1-SNAPSHOT
     +--- LOCAL: android-logging.jar
     +--- LOCAL: Context-Core.jar
     \--- LOCAL: Context-Location.jar

我对发布库非常陌生,因此很有可能我在这里做了完全错误的事情。任何指针都会有所帮助。

编辑:这是gradle依赖项的输出(的一部分),它显示了列出了远程依赖项,但是我仍然遇到运行时错误!
compile - Classpath for compiling the main sources.
+--- com.android.support:support-v4:19.+ -> 19.1.0
\--- com.example.android:example-api:0.1-SNAPSHOT
     +--- org.springframework.android:spring-android-rest-template:1.0.1.RELEASE
     |    \--- org.springframework.android:spring-android-core:1.0.1.RELEASE
     +--- com.google.code.gson:gson:2.2.4
     +--- com.squareup.okhttp:okhttp:1.3.0
     |    \--- com.squareup.okhttp:okhttp-protocols:1.3.0
     \--- org.springframework.android:spring-android-core:1.0.1.RELEASE

谢谢!

最佳答案

我不确定为什么,但是创建的新项目可以像这样正常工作:

compile('com.example.android:example-api:0.1-SNAPSHOT')

但是,当使用旧项目时,我必须像这样引用它,以便下载AAR并提取引用。
compile('com.example.android:example-api:0.1-SNAPSHOT'){
    transitive = true
}

关于android - Android库-为什么不下载我的依赖项? (等级/Maven),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23998872/

相关文章:

android - uploadArchives 构建调试和发布

android - 如何在操作栏中添加填充和增加抽屉导航图标的宽度?

Spring Boot 2 和 Kotlin(使用 Maven)

Gradle,不同版本的直接和传递依赖

java - 在评估阶段使用 Gradle 插件配置属性

android - 在没有混淆的情况下使用 Proguard 和 Android

android - 如何从 "disk"读取 NinePatch 图像并将其转换为 NinePatchDrawable?

maven - 无法识别的标签 'repositores'

java - Maven 的 pom.xml 中的 pluginManagement 是什么?

java - Struts Hibernate 与注释集成 (java.lang.NoSuchMethodException)