Transitive Library dependencies are not found in t

2019-02-21 00:11发布

问题:

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.