At our company, we have started modularizing our android projects and each has several dependencies. We use JFrog artifactory to host our aar files. Here is the code:
Library A:
compile "com.google.firebase:firebase-crash:$googlePlayServices"
compile "com.google.firebase:firebase-core:$googlePlayServices"
compile "com.squareup.retrofit2:retrofit:$retrofit"
The following does not work. I have also tried removing "#aar" but still nothing.
Main Projects:
compile ('com.sample.librarya:librarya:0.0.1#aar'){
transitive = true
}
and hence I have to add retrofit dependencies to the main app again.
I have done a lot of research and read a lot of SO questions but none of them help hence this question. I also have all dependencies on LibraryA listed in its pom.xml file.
Add both following dependencies:
compile ('com.sample.librarya:librarya:0.0.1#pom')
compile ('com.sample.librarya:librarya:0.0.1#aar')
The first will download the pom and add all it's transitive dependencies on classpath. The second will download the aar.
Related
I'm using Android Studio 3.0.1 and I'm trying to add an online dependency and while Gradle initially syncs without a problem it doesn't show my dependency in External Libraries and my code that references the dependency doesn't work.
Here's a snippet of what my build.gradle file looks like:
repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/groups/public/' }
}
dependencies {
compile group: 'com.fortysevendeg.android', name: 'swipelistview', version: '1.0-SNAPSHOT'
}
I'm pretty new to android development (took over an existing project from a dev who quit without leaving any documentation) so I'm not sure if this is a mistake with how to add a project dependency or if there is a problem with the dependency that I'm trying to add. Any help would be greatly appreciated!
I was able to get this to work by changing the dependency declaration to:
compile group: 'com.fortysevendeg.android', name: 'swipelistview', version: '1.0-SNAPSHOT', classifier: 'jar-with-dependencies'
The library artifacts up on the repository include an apklib and a JAR with a special classifier. The apklib format is not supported by Android Studio, and unfortunately the classifier on the JAR means that it's not accessible simply using the group-name-version format when declaring dependencies.
Your build.gradle file seems fine. If you want to keep the library specified as an external library, you can try and define the dependency using the alternative notation, replace:
compile group: 'com.fortysevendeg.android', name: 'swipelistview', version: '1.0-SNAPSHOT'
with:
compile 'com.fortysevendeg.android:swipelistview:1.0-SNAPSHOT'
The alternative approach is to download the jar file yourself and use it as a local dependency. If you navigate to the maven repository you can inspect the package which is included as a dependency and download the jar directly. Place the jar file in the libs folder of your project and add the following to your build.gradle file:
compile fileTree(dir: 'libs', include: ['*.jar'])
For further details on how to configure the dependencies of your gradle project, check out the Android Studio documentation here.
Based on the information you have provided, this should fix your issues. If this does not solve the error then there may be other issues with the project.
Your dependencies should not placed in the top-level build.gradle file where the repositories are defined. There is even a comment in that file that says so, by default.
You app dependencies should be the module's build.gradle along with the others like android-support
Additionally, that library is very old, and is a SNAPSHOT build, meaning it isn't meant to be generally used in a release environment. You should find an alternative... And there are plenty of other ListView swiping ones
I am building an SDK for Android that should be exported as an .aar file.
The SDK has a list of dependencies defined in its build.gradle file, for example:
compile 'com.makeramen:roundedimageview:2.2.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.google.android.gms:play-services-ads:9.0.2'
My question is: how do i include these libraries in the .aar, so that it is self-contained and does not require the developer using it to add these libraries when using my SDK?
I have looked everywhere for this, but most answers don't seem to address this issue or don't work.
How do i include these libraries in the .aar
Not at all.
An .aar-file is not intended to contain it's own dependencies
You should use a dependency management system (like maven or ivy) to distribute your SDK in order to handle transitive dependencies.
My AAR includes a picasso library, but in my java code can't find picasso.
Here is my build.gradle:
and here is my multi-image-selector AAR gradle:
Why you not using only
compile 'com.squareup.picasso:picasso:2.5.2'
The aar file doesn't contain the nested 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 you have to add in your app (not the library):
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.squareup.picasso:picasso:2.5.2'
If you use the gradle maven plugin to deploy the aar to a local repo, then you can get transitive dependencies to work. Here's how to do that:
How to use maven plugin to publish a library as an aar
How to enable transitive dependencies on the library dependency
Assume that you have one app and one library module. In your library module you use Picasso as dependency.
Let me explain step by step, with possible scenarios.
1- If you add your library module to your app module as the following :
implementation(project(":myLibrary"))
Your library works correctly.
2- If you add your library module to your app module as the following :
implementation files('../libs/mainLibrary-debug.aar')
You may get a crash if you don't put Picasso dependency to your app module. You have two options to get rid of this crash.
2.a.First option is to add Picasso library to your app module.
2.b.The second option is to compile you aar using any fat aar plugin. If you use a fat aar plugin, when you generate aar, it automatically downloads Picasso library and put it in aar. In this way, you don't need to add Picasso dependency into your app module. There are several fat aar plugins available, here is one of them : https://github.com/kezong/fat-aar-android
I have worked on an Android library (an API client) that uses Retrofit and Joda DateTime.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'joda-time:joda-time:2.8.1'
}
Now that the library is completed I compiled it into an AAR file and I want to use it in an application, so I added it to the libs folder of the application and included it in the build.gradle file like so :
dependencies {
compile(name:'s3papiandroidclient', ext:'aar')
//Some other things
}
However, when I try to initialize the API client from the library, the application crashes when it comes to calling objects from RetroFit or DateTime (For instance, retrofit.RestAdapter). It looks like Gradle does not read the dependencies from the AAR library, thus doesn't install Retrofit and DateTime in my application. I tried to use the transitive=true parameter on my AAR file, does not help.
Other point that might help, I tried to generate a POM file, and the dependencies don't appear in it either. It looks like there's really something going on with these and I am completely stuck on that.
The only workaround I could find is to add manually the dependencies from the AAR file to the app's build.gradle file but it doesn't make sense, I assume Gradle can import dependencies on its own !
Regards,
Gyoo.
It looks like Gradle does not read the dependencies from the AAR library
That is because there are no dependencies in an AAR file.
I tried to generate a POM file, and the dependencies don't appear in it either
Then there is a problem in how you are generating the POM file. Plus, AFAIK, you would need to put the AAR and its POM file in a repository, in order for Gradle to recognize the POM and use the dependency information inside of it.
Let's pretend that I have a github repository at github.com/myprojects/myrepo
Let's also pretend that I have a project in IntelliJ with a build.gradle file that contains the following:
dependencies {
// android
compile 'com.android.support:appcompat-v7:21.0.0'
// google play services
compile 'com.google.android.gms:play-services:6.1.71'
}
I want to add my github project via Gradle so I imagine that the dependencies need to add something like:
compile 'github.com:myprojects:myrepo'
This obviously isn't how it works though since I get "Failed to resolve com.github:myprojects:myrepo
Actual examples which work:
compile 'com.github.satyan:sugar:1.3'
compile 'com.github.castorflex.smoothprogressbar:library:x.x.x'
Since I don't want to clone the repository into a libs folder, how can I add a compile command in the dependencies portion of my build.gradle file to compile from github so that I can simply add something like the following:
compile 'com.github.myprojects:myrepo'
compile 'com.github.castorflex.smoothprogressbar:library:x.x.x'
It works because this library is published in Central Maven.
compile 'github.com:myprojects:myrepo'
This obviously isn't how it works though since I get "Failed to resolve >com.github:myprojects:myrepo
It is not enough to push on github.
You have to publish you artifact on Central Maven or JCenter or a local maven. You can find some guides to publish on jcenter, for example this.