Gradle: download Dependencies of included aar-library - android

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.

Related

Adding a library with the notation ext: 'aar' or #aar doesn't import the transitive dependencies

I've white labeled an app by converting my app into a library and I have a configuration file which is passed to the library to change colours, add a new loading animation, change some text, etc. The library has all those dependencies listed above. When I import the library into the new app, since the new app is just a variation of the library, it won't compile unless I import these dependencies as well.
I added the custom library using:
compile(group: 'com.lib.libcore', name: 'libcore', version: '1.0.35', ext: 'aar', classifier: '')
in the build.grdle but it still forces me to include all the dependencies from the library.
I was wondering if anyone knows why I need to include all the dependencies from my custom library in my app.
So I have these dependencies:
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'
compile ('com.google.firebase:firebase-core:11.4.2')
compile ('com.google.firebase:firebase-messaging:11.4.2')
compile ('com.google.firebase:firebase-crash:11.4.2')
compile ('com.google.firebase:firebase-invites:11.4.2')
compile 'com.google.android.gms:play-services-location:11.4.2'
compile 'com.google.android.gms:play-services-gcm:11.4.2'
compile 'com.google.android.gms:play-services-maps:11.4.2'
compile 'com.google.android.gms:play-services-auth:11.4.2'
compile 'com.google.android.gms:play-services-places:11.4.2'
When I import the library using gradle, I need to include all these dependencies in the app gradle file as well. Does anyone know how I can remove it from the app gradle without the app failing?
It happens because you are using the notation ext: 'aar'
compile(group: 'com.lib.libcore', name: 'libcore', version: '1.0.35', ext: 'aar', classifier: '')
It is the same of:
compile('com.lib.libcore:libcore:1.0.35#aar')
You can check the official doc:
An artifact only notation creates a module dependency which downloads only the artifact file with the specified extension. Existing module descriptors are ignored.
It means that you want to download only the aar artifact, and no dependencies.
Just remove the notation ext: 'aar' or the #aar in your dependency.
If you want to use custom library in your project you have to add it as dependency in your build.gradle (Module:app)

local .aar file cannot be supported

I try to add a local .aar file to the project as it is described here, because I don't want to implement this routine each time when I change my library code.
So in my project build.gradle file I added:
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
Both my library and my app have two flavours - alphaTest and myRelease. I want each library to be added strictly to the corresponding flavour of the app.
And in build.gradle of my app I have the following dependencies block:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar', '**/*.aar'])
compile 'com.android.support:appcompat-v7:23.0.0'
alphaTestCompile files ('libs/myLib-alphaTest-release.aar')
myReleaseCompile files('libs/myLib-myRelease-release.aar')
}
Unfortunately as a result I always receive this kind of error:
Error:Project myApp: Only Jar-type local dependencies are supported. Cannot handle: C:\Users\...\myApp\libs\myLib-myRelease-release.aar
I tryed to use this format
alphaTestCompile ('my.package:libs/myLib-alphaTest-release:0.1#aar')
and also this
alphaTestCompile (name:'myLib-alphaTest-release', ext:'aar')
but in this case the gradle cannot event resolve the files.
How can I make .aar files also be compiled?
Below is what I config for using aar file:
//dependencies
compile fileTree(include: ['*.jar'], dir: 'libs') // dont change anything here
compile(name: 'yourLib', ext: 'aar') // yourLib.aar is put in libs folder
repositories {
flatDir {
dirs 'libs'
}
}

Transitive Library dependencies are not found in the application

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.

How to fix duplicate libraries - more than one library with package name 'android.support.v7.appcompat'

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

Gradle: .aar library not included into compiler's output

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

Categories

Resources