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
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.
Related
I wrote a library-project cameraBarcodeScanner that is built into an aar file. This library has the following dependencies defined in its build.gradle:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.google.zxing:core:3.2.1'
compile 'com.journeyapps:zxing-android-embedded:3.2.0#aar'
}
I'm using the library in a test-application like so:
dependencies {
compile(name: 'cameraBarcodeScanner-release', ext: 'aar')
}
Gradle finds the application and is able to build it. However, on runtime, android is not able to find the classes, that are located in zxing-android-embedded. It seems to me, that gradle is not downloading the dependencies of the library-project.
I also tried:
dependencies {
compile(name: 'cameraBarcodeScanner-release', ext: 'aar'){
transitive = true
}
But this didn't help either. Do I have to expose the library's dependencies in some way? How would you use a library and make gradle download its dependencies?
Thanks in advance!
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.
You should use a maven repository (you have to publish the library in a private or public maven repo), you will not have the same issue.
In this case, gradle downloads the dependencies using the pom file which will contains the dependencies list.
I'm writing a build system for a framework which can be extended via plugins by users (developers). My problem is that I can't include all aar files with a mask. For example I can include all jar files like this:
compile fileTree(dir: 'libs', include: '*.jar')
But if I include all aar files like this:
compile fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
I get an error:
Only Jar-type local dependencies are supported. Cannot handle: /path/to/file.aar
However it is possible to include each file separately like this:
compile (name:'myfile', ext:'aar')
As a workaround, during the build, I will need to list the files in 'libs' directory, insert all the libraries manually into my build.gradle and keep it in sync (or rewrite each time). it's a hackish way of doing it and thus I wonder if there is a better way of doing it. Maybe there is a gradle script which can do it automatically or some gradle beta which supports this:
compile fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
First add flatDir repository:
repositories {
flatDir {
dirs 'libs'
}
}
Then after dependencies section add:
fileTree(dir: 'libs', include: '**/*.aar')
.each { File file ->
dependencies.add("compile", [
name: file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name },
ext: 'aar'
])
}
You can put your aar files in a folder.
You have to define this folder inside the repositories block. Of course you can use the libs folder
Something like:
repositories {
mavenCentral()
flatDir {
dirs 'myAarFolder'
}
}
Then you can use in your dependencies something like:
dependencies {
compile(name:'nameOfYourAARFile', ext:'aar')
}
As I know there is no way to incluse all aars with a wildcard as *.
I know this answer is late to the game, but for others coming here, I have managed it without using the repository.flatDir.
Just add the fileTree() AFTER the dependencies section
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
fileTree(dir: 'libs', include: '*.aar')
.each { File file ->
dependencies.add("compile", files( 'libs/'+file.name ))
}
Gradle build error - more than one library with package name android.support.v7.appcompat. This project has Chrome Cast in it, so is using android-support-v7-appcompat and CastCompanionLibary-android-master. Project structure is the following:
BaseGameUtils
CastCompanionLibrary-android-master
VideoBrowserActivity
android-support-v7-appcompat
android-support-v7-mediarouter
google-play-services_lib
Really tough error because the compiler doesn't give much information other than 'more than one library with package name 'android.support.v7.appcompat'. Understand why you don't want to be using different versions of the same library, but why doesn't the compiler reveal what versions are being used, and where they are being called from?
Trying to add leaderboard similar to type-a-number challenge (github example project)
There are a total of (7) build.gradle files. One for the top level and one for each of the packages listed above. Top level gradle file:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
BaseGameUtils:
dependencies {
compile 'com.android.support:appcompat-v7:20.0.+'
compile 'com.android.support:support-v4:20.0.+'
compile 'com.google.android.gms:play-services-games:6.5+'
compile 'com.google.android.gms:play-services-plus:6.5+'
compile 'com.google.android.gms:play-services-appstate:6.5+'
}
CastCompanionLibrary-android-master
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.android.support:appcompat-v7:20.0.+'
compile project(':android-support-v7-mediarouter')
compile 'com.google.android.gms:play-services-cast:6.5+'
compile project(':BaseGameUtils')
}
VideoBrowserActivity
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':CastCompanionLibrary-android-master')
}
android-support-v7-appcompat
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android-support-v7-mediarouter
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':android-support-v7-appcompat')
}
google-play-services_lib
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
Any help is appreciated on this.
Just go through you project's libs folder not from the Android Studio but from you windows explorer/finder (for windows)/(for mac). And search for android.support.v7.appcompat. Any evidence found related to Apcompat (whether it is v4 or v7) is recommended to be deleted.
Hopefully your problem will be solved.
In your libs folder dont miss any sub directory that has libs folder.
Follow the Steps
1) Remove This repository:
implementation 'com.android.support:appcompat-v7:28.0.0'
2) Add This Repository
implementation 'com.android.support:support-v4:21.0.3'
compile group: 'com.android.support', name: 'appcompat-v7', version: '26.0.1'
3) That's It You Are Ready To Go
I have a Gradle project, composed of two subprojects: UI and backend library.
The library on it's own relies upon another .aar library.
Extract from library's build.gradle:
dependencies {
compile(name: 'ActionBarSherlock', ext: 'aar')
}
Extract from UI's build.gradle:
dependencies {
compile project(":my_library")
compile fileTree(dir: 'libs', include: ['*.jar']) // all jars
}
When I try to build the project, build fails with following error:
> Could not resolve all dependencies for configuration ':UI:_debugCompile'.
> Could not find :ActionBarSherlock:.
Required by:
Project:UI:unspecified > Project:my_library:unspecified
I checked the .aar output for my_library and it does not contain ActionBarSherlock.arr in libs folder. How do I tell Gradle to include .arr files into library output?
transitive = true
seems to be missing. For example (in the build.gradle UI):
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile ('com.your.company.backendlib') {
transitive = true
}
}
I was trying to integrate my project in android studio. but i am little confused when adding dependencies. I don't know which one is works good.I have tried Compile fileTree and compile files.Its not working for me . I found some methods.can any one tell me which one is appropriate for adding library (jar file only like admob).
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.android.support:appcompat-v7:+'
compile project(":libraries:libraryname")
compile files('libs/libraryname.jar')
can any one tell me which one is appropriate for adding library (jar file only like admob).
If the library is available as an artifact in a Maven or Ivy repository, like Maven Central, add the repository to your repositories block (e.g., mavenCentral()) and then use compile 'com.android.support:appcompat-v7:+', replacing the quoted string with the one that identifies the artifact that you want.
If the library is not available as an artifact, put the JAR in the appropriate directory (e.g., libs/ at the project level) and use compile fileTree(dir: 'libs', include: '*.jar').
compile project(":libraries:libraryname") is for sub-projects, which you probably do not have.
compile files('libs/libraryname.jar') works, but compile fileTree(dir: 'libs', include: '*.jar') is more flexible, as you do not have to change your build.gradle file for every JAR.