Android App with access to some library dependencies using Gradle - android

I'm creating an Android App. All the business logic is in a library that I'm adding to my app project importing its AAR file.
But this library has some compiled project dependencies I'd need to use from the app, but don't know how.
LIBRARY PROJECT
apply plugin: 'com.android.library'
dependencies {
compile 'com.android.support:appcompat-v7:22.1.1'
compile(name: 'lib1', ext: 'jar')
compile project(':lib2')
I'm exporting this library as mobile-release.aar.
APP PROJECT
dependencies {
compile(name:'mobile-release', ext:'aar')
How can I access from my app project to those lib1, lib2 from mobile-release?
I tried adding
compile(:mobile-release:lib1)
without success. Thanks!

the short solution is to include your library aar file as module dependency in the application project, and then add the library dependencies
//in app projet
dependencies {
//....
compile project(':mobile-release') {
dependencies {
compile(name: 'lib1', ext: 'jar')
compile project(':lib2')
}
}
}
there are other solutions like publish you library to a local/remote repository, or create a function in the library gradle file that takes the jar dependencies and add them to the generated aar

Related

How(is it possible) to add a dependency to my android library(aar)? [duplicate]

We're making some library, basicly for our API, that we would make life easier for our external developers.
So we created new library project and put Retrofit and some other libraries as dependencies.
dependencies {
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
}
Now when we build it, it produces aar file.
But now when we put the aar file to libs directory and set it as dependency, we still have to put the same dependency in user's build.gradle file, which sucks. It should be taken from the library, right?
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile(name: 'ourlibrary', ext: 'aar') {
transitive = true;
}
}
How to make transitive = true work?
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.
In your case adding transitive=true doesn't resolve your issue for the reason described above.
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.

How to pack an .aar that has an external dependency? [duplicate]

We're making some library, basicly for our API, that we would make life easier for our external developers.
So we created new library project and put Retrofit and some other libraries as dependencies.
dependencies {
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
}
Now when we build it, it produces aar file.
But now when we put the aar file to libs directory and set it as dependency, we still have to put the same dependency in user's build.gradle file, which sucks. It should be taken from the library, right?
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile(name: 'ourlibrary', ext: 'aar') {
transitive = true;
}
}
How to make transitive = true work?
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.
In your case adding transitive=true doesn't resolve your issue for the reason described above.
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.

Gradle: exclude dependencies from inside an aar [duplicate]

We're making some library, basicly for our API, that we would make life easier for our external developers.
So we created new library project and put Retrofit and some other libraries as dependencies.
dependencies {
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
}
Now when we build it, it produces aar file.
But now when we put the aar file to libs directory and set it as dependency, we still have to put the same dependency in user's build.gradle file, which sucks. It should be taken from the library, right?
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile(name: 'ourlibrary', ext: 'aar') {
transitive = true;
}
}
How to make transitive = true work?
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.
In your case adding transitive=true doesn't resolve your issue for the reason described above.
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.

How to declare the dependencies of a library project when those dependencies should be fulfilled by the consuming application?

I'm developing an android library that depends on some third party aars and jars.
Currently, these dependencies are declared in the library module's gradle buildscript like so:
repositories {
flatDir{
dirs 'libs', 'android-libs'
}
}
dependencies{
compile(name: 'threetenabp-1.0.5', ext: 'aar')
compile fileTree(include: ['*.jar'], dir: 'libs')
}
However, this results in the dependencies' classes being built into the aar, potentially causing conflicts when the library is used in an application.
How can I reference these dependencies from my library without actually packaging them into the library?
I have tried changing the declarations from "compile" to "provided" and then compiling the files into the application, but when I do this my library fails to build.
After some reading at https://docs.gradle.org/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies I eventually figured out that using compile fileTree will package the dependencies into the output library. To avoid this, declare each dependency individually, using the following syntax:
dependencies {
compile: name: 'filename-without-extension'
}
And the dependencies will no longer be packaged into the output.
The project making use of the output aar will still need to include the flat-dir repository that holds the jar files, like so:
repositories {
flatDir{
dirs 'libs'
}
}

Can't add External Library in Android Studio?

I Added a External Library to my project by following
this method. and tried this method too this method too.
Gradle build got Finished and i got this line added to my build.gradle
compile 'com.github.castorflex.smoothprogressbar:library-circular:1.0.1'
Now i am trying to import this library in my class. But i can't do this. I took a look at the files and they are located in the build directory under the exploded-aar. so i added #aar to the compile line. Still there are no changes.
How can i import this library to my class or where i am going wrong?
Thanks in Advance
Well, you don't need to download anything or point your dependency to a file.
This dependency, as most of the dependencies you use, can automatically fetched from JCenter, biggest repository for Java and Android components.
All you need to do is:
Add JCenter as your dependencies repository.
Declare that you need the library-circular dependency. This can be found and copy/pasted from the file icon in particular package/version in JCenter.
Resync your Android Studio project with your Gradle build file. This is the 'sync' button in Gradle pane in Android Studio.
Here's what your build.gradle should include:
repositories {
jcenter()
}
dependencies {
compile(group: 'com.github.castorflex.smoothprogressbar', name: 'library-circular', version: '1.0.1', ext: 'aar')
}
in build. gradle put following code
dependencies {
compile fileTree (dir: 'libs', include: ['*.jar'])
}
Did you add the dependencies?
You can do so in the following way -
dependencies {
compile files('insert ParentFolder/external_library_name with the extension here')
}
Add it to build.gradle based in the app folder
1.Put the jar file into the libs folder.
2.Right click it and hit 'Add as library'.
3.Ensure that compile files('libs/abcdef.jar') is in your build.gradle file and Finally add the dependencies.
Eg.
dependencies {
compile fileTree(dir: '../jar', include: '*.jar')
compile project(':pull-to-refresh')
compile files('libraries/abcdef.jar')
}
Hope this helps :)

Categories

Resources