Android library not resolve dependencies after publish

不想你离开。 提交于 2020-01-16 18:52:27

问题


I have created android library which use android-async dependency. This library project showing everything is fine. But when i publish this library into local nexus and then try to use it in some other project then in a class where this async library is using show error like this:

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

Here is a gradle file of library:

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')
}

so for building and publish of this library project i use command like

gradle clean build and gradle publish

adding additional info: I have check that on nexus this library is showing that it included a dependency. Here is a 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>

Does someone have idea why i am getting error message that NoClassDefFoundError for the dependency ?


回答1:


I have a problem similar as yours, described here.

But, in my case, I'm able to use the dependencies of my library in my app. The difference with you, I think, is that my pom file contains a <scope>compile</scope> field for each dependencies.

All I have done is using the Maven plugin with the following configuration :

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

I hope this will help you.



来源:https://stackoverflow.com/questions/56240716/android-library-not-resolve-dependencies-after-publish

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!