Transitive Library dependencies are not found in the application

跟風遠走 提交于 2020-01-11 07:34:21

问题


Say I have a library module which contains some 3rd party libraries like OkHttp. When I include this library in my application I was unable to use these 3rd party libraries. I read the following articles Article 1, Article 2 and tried
1. compile project(':library-name')
{after importing the .aar file(of library) as a module into myproject}
2. I included the .aar file in libs folder and added following dependencies

build.gradle(project level)

allprojects {
repositories {
    flatDir {
        dirs 'libs'
    }
}
}

build.gradle(application level)

compile fileTree(dir: 'libs', include: ['*.jar'])
compile ('com.myapp.package:library-name:1.0.0@aar'){
transitive=true
}

3. similar to 2nd but in
build.gradle(application level)

compile fileTree(dir: 'libs', include: ['*.jar'])
compile (name:'library-name', ext:'aar'){
transitive=true
}

But, still I was unable to use the transitive libraries which are present in my library. I am getting following exception.

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/squareup/okhttp/MediaType;

Can someone help

EDIT:
The following is build.gradle of my library

compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'

compile 'org.apache.httpcomponents:httpcore:4.4.4'
compile 'org.apache.httpcomponents:httpclient:4.5.1'

compile 'com.squareup.okhttp:okhttp:2.6.0'
compile 'petrov.kristiyan.colorpicker:colorpicker-library:1.0.3'


testCompile 'junit:junit:4.12'

testCompile 'org.mockito:mockito-core:1.10.19'

testCompile 'org.hamcrest:hamcrest-library:1.1'

compile files('notificationlog-0.1.0.jar')

I was able to use the notificationlog in my application which was a dependency in my library, but I was unable to use okhttp and colorpicker


回答1:


The aar file doesn't contain the nested (or transitive) dependencies and doesn't have a pom file which describes the dependencies used by the library.

It means that, if you are importing a aar file using a flatDir repo you have to specify the dependencies also in your project.

It doesn't make sense:

compile (name:'library-name', ext:'aar'){
    transitive=true
}

because the aar is without a pom file which describes the dependencies, then gradle is unable to add any dependencies because it can't know them.



来源:https://stackoverflow.com/questions/35311507/transitive-library-dependencies-are-not-found-in-the-application

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