【问题标题】:Android library not resolve dependencies after publish发布后Android库无法解析依赖项
【发布时间】:2019-05-21 14:38:42
【问题描述】:

我创建了使用 android-async 依赖项的 android 库。这个库项目显示一切都很好。但是,当我将此库发布到本地 nexus 中,然后尝试在其他项目中使用它时,然后在该异步库正在使用的类中使用显示错误,如下所示:

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/loopj/android/http/SyncHttpClient;
                                                                    at com.myproject.installer.InstallerService.onHandleIntent(InstallerService.java:46)

这是一个库的gradle文件:

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.2'
    }
}

repositories {
    jcenter()
    mavenLocal()
    mavenCentral()
    maven {
        url "http://nexus.local:8081/nexus/content/groups/public"
    }
    maven {
        url 'https://maven.google.com'
    }
}

apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 17
        versionCode 5
        versionName "0.1.4.1"
    }

    lintOptions {
        abortOnError false
    }
}

dependencies {
    compile 'com.loopj.android:android-async-http:1.4.9'
}

apply plugin: 'maven-publish'

publishing {
    publications {
        myPublication(MavenPublication) {
            artifacts {
                groupId 'com.myproject.android'
                artifactId 'installer-lib'
                version project.android.defaultConfig.versionName
                artifact 'build/outputs/aar/app-release.aar'
            }
            //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)
                }
            }
        }
    }
    repositories {
        maven {
            url 'http://nexus.local:8081/nexus/content/repositories/thirdparty'
            credentials {
                username = 'admin'
                password = getNexusPassword()
            }
        }
    }
}

def getNexusPassword() {
    Properties props = new Properties();
    props.load(project.file('local.properties').newDataInputStream());
    return props.getProperty('nexuspassword')
}

所以为了构建和发布这个库项目,我使用类似的命令

gradle clean build and gradle publish

添加附加信息: 我检查了这个库在 nexus 上是否显示它包含一个依赖项。这是一个 pom.xml:

<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.myproject.android</groupId>
<artifactId>installer-lib</artifactId>
<version>0.1.4</version>
<packaging>aar</packaging>
<dependencies>
<dependency>
<groupId>com.loopj.android</groupId>
<artifactId>android-async-http</artifactId>
<version>1.4.9</version>
</dependency>
</dependencies>
</project>

有人知道为什么我收到 NoClassDefFoundError 依赖项的错误消息吗?

【问题讨论】:

    标签: android android-gradle-plugin maven-plugin noclassdeffounderror android-library


    【解决方案1】:

    我有一个和你类似的问题,described here

    但是,就我而言,我可以在我的应用程序中使用我的库的依赖项。我认为与您的不同之处在于我的 pom 文件包含每个依赖项的 &lt;scope&gt;compile&lt;/scope&gt; 字段。

    我所做的只是使用具有以下配置的 Maven 插件:

    apply plugin: 'maven'
    
    uploadArchives {
        repositories {
            mavenDeployer {
                repository(url: "$url") {
                    authentication(userName: "$user", password: "$password")
                }
    
                pom.groupId = "com.test.common"
                pom.artifactId = "CommonLib"
                pom.version = VERSION_NAME + "." + VERSION_CODE
            }
        }
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-17
      • 2017-05-06
      相关资源
      最近更新 更多