首页 文章

Android库 - 为什么不下载我的依赖项? (摇篮/ Maven的)

提问于
浏览
0

我正在试图弄清楚如何将Android库项目分发给一些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它成功上传到我的快照nexus存储库 .

在我的示例项目的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

谢谢!

1 回答

  • 1

    我不确定为什么,但是创建的新项目没有问题:

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

    但是当使用旧项目时,我必须像这样引用它,以便下载AAR并引用引用 .

    compile('com.example.android:example-api:0.1-SNAPSHOT'){
        transitive = true
    }
    

相关问题