【问题标题】:Android Library - Why aren't my dependencies being downloaded? (Gradle/Maven)Android 库 - 为什么我的依赖项没有被下载? (Gradle/Maven)
【发布时间】:2023-03-19 16:45:01
【问题描述】:

我正在尝试弄清楚如何将 Android 库项目分发给一些 beta 用户,但是当我分发它并在示例项目中使用它时遇到了一些问题。我正在尝试分发 AAR 文件。

我的示例项目中的所有内容都可以正常编译,但当我调用一段使用 GSON 的代码时出现错误:java.lang.NoClassDefFoundError: com.google.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

谢谢!

【问题讨论】:

  • 您将依赖项添加到compile 配置中,但androidDependencies 任务(我不熟悉)仅打印了debugdebugTest 和@987654331 的依赖项@ 配置。尝试gradle dependencies,并确保为您声明依赖项的同一个项目执行它(显然这是一个多项目构建)。
  • @PeterNiederwieser 啊,你说得对,当我在该项目上运行 gradle 依赖项时,它确实会显示出来......但是知道为什么我会得到 java.lang.NoClassDefFoundError: com.google。 gson.Gson 运行时出错?
  • proguard 可能删除了它?
  • Weird.. 我开始了一个新的示例项目并让它工作(我指定了 com.example.android:example-api:0.1-SNAPSHOTaar,但它最初没有工作...删除 aar 可以加载依赖项)。现在在我的原始示例项目中,当我尝试删除 aar 时,它会抱怨以下内容:Artifact 'com.example.android:example-api:0.3-SNAPSHOT:example-api.jar' not found。但我的新项目运行良好!

标签: android maven gradle


【解决方案1】:

我不确定具体原因,但创建的新项目可以正常工作:

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

但在使用旧项目时,我必须像这样引用它,以便下载 AAR 并提取引用。

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

【讨论】:

    猜你喜欢
    • 2020-04-30
    • 2016-09-30
    • 2013-12-09
    • 2016-08-18
    • 2020-07-04
    • 2017-11-09
    • 2013-11-18
    • 1970-01-01
    相关资源
    最近更新 更多