1

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

Community
  • 1
  • 1
Sai Phani
  • 63
  • 1
  • 8

1 Answers1

5

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.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • as I mentioned previously `compile files('notificationlog-0.1.0.jar') `, I was able to use this nested library in my project. I downloaded OkHttp and included as a jar, but still I was unable to access OkHttp – Sai Phani Feb 10 '16 at 10:07
  • @SaiPhani I dont' understand in which module are you adding the jar files. However pay attention to the answer. Transitive = true doesn't make sense in an aar-file dependency. You have to add the jars or the dependencies in the project in which you are using the aar file. Pay also attention that okhttp can have some dependencies (like okio) – Gabriele Mariotti Feb 10 '16 at 17:44
  • 1
    @GabrieleMariotti so there's no way one can include transitive dependencies of a locally referenced aar without explicitly including the aar's dependencies in the project? – aasu Mar 21 '16 at 18:29
  • 1
    I am currently somewhat new to this build system still, but if I own source libraries to 5 internal projects (one for camera, one for image settings, etc.) and I want to create an SDK to be used for that which uses these 5 internal projects, I can't create a single archive of some sort that can be shared in a single package? – Jay Snayder Jan 24 '17 at 18:41