Packaging Android AAR that has dependencies - android

I want to package my library as an aar.
The library has several dependencies (universal image loader, ORMLite, guava...)
It also has a dependency for another library that I wrote (call it 'library B').
I have 2 questions:
Will everyone who will use my library need to add dependencies according to the library's dependencies (universal image loader, ORMLite, guava...)
Do I need to create a separate aar for 'library B', and have users of my lib have a separate dependency for it?

Will everyone who will use my library need to add dependencies according to the library's dependencies (universal image loader, ORMLite, guava...)
Not if you are distributing your AAR as an artifact in a repository with appropriate metadata (e.g., Maven-style POM file). The metadata will point to your dependencies, and build systems (e.g., Gradle) will pull in the dependencies.
Do I need to create a separate aar for 'library B'
Yes, otherwise nobody will have access to it, unless you eliminate it and fold its code into your first library.
and have users of my lib have a separate dependency for it?
See above for setting up dependencies.

You don not need do that,and you only do the next:
if you aar libray have thirty dependencies:
you should make others use your aar library in the way:
for example:
compile('com.android:com.android.download:1.3')
or
compile('com.android:com.android.download:1.3#aar'){
transitive = true
}
if your aar libray have not thirty dependencies:
compile('com.android:com.android.download:1.3#aar')
In a word:#aar have conflict with the default vault of transitive

Related

Achieve modularization in android, I want SDK to work with optional dependencies. Whether dependency is provided or not code should compile in both

I am creating SDK and want to modularize it.
And prevent internal APIs from being exposed on your public interface.
Want optional dependency based on features required. If features is not required we can remove dependency and code and imports should not work.
Tried Ways
Submodule dependencies
Fat AAR
Submodule dependencies
when a library module gets built, the .aar artifact will only include code and resources that are in the library module itself. It won’t include:
any code or resources from database and ui-components
links to its transitive dependencies (these go into the build.gradle)
So when the app module directly includes the library as a gradle dependency, it would crash due to missing classes from database and ui-components on its classpath.
Fat AAR
In the fat .aar solution, code and resources of the submodules are bundled into the main SDK module, hence creating a fat .aar
the fat .aar plugin breaks on almost every minor Android Gradle plugin update! This is because it hooks itself into particular tasks of the Android Gradle plugin and these very often get renamed/moved. However, the project maintainer need to do job at fixing those within a few weeks after the breaking change.
Also, because of the way fat .aar references dependencies from submodules, it can significantly increase the binary size of your SDK.
Is there any other possible solution?

Android Studio: How to read classes from a jar file from a library?

I have two projects that use the same library. This library uses a jar file as a dependency, however only the library is able to read the classes from the jar file. The two projects don't have access to the classes from the jar file.
I tried adding the jar file to the two projects separately, but I get a build error, probably because now it's included in the project level and library level resulting in conflicts.
The jar file is YouTubeAndroidPlayerApi.jar
/------------------- Project1 (can't read classes from jar)
Library(w/ jar)
\------------------- Project2 (can't read classes from jar)
Any ideas on how to approach? Thanks!
If you are using gradle 4.1, make sure to use the api keyword when importing the .jar into your library. Then remove it from your other projects.
for example: api fileTree(dir: 'libs', include: ['*.jar'])
from the documentation:
When a module includes an api dependency, it's letting Gradle know that the module wants to transitively export that dependency to other modules, so that it's available to them at both runtime and compile time. This configuration behaves just like compile (which is now deprecated), and you should typically use this only in library modules. That's because, if an api dependency changes its external API, Gradle recompiles all modules that have access to that dependency at compile time. So, having a large number of api dependencies can significantly increase build times. Unless you want to expose a dependency's API to a separate test module, app modules should instead use implementation dependencies.

What are dependencies in android?

What are dependencies ?
Why do we add dependencies ?
I searched a lot but could not find the answers to above questions.
In Android Studio, dependencies allows us to include external library or local jar files or other library modules in our Android project.
For example: Suppose I want to show some images in ImageView. But I'm using Glide Library to enhance the smoothness of application. So I have to add a dependency in the build.gradle(Module App) as:
compile 'com.github.bumptech.glide:glide:3.7.0'
So Now I can use Glide library :) and show my images.
Note: Glide library is the bumptech's library but still I can use it in my project from 1 line of code of dependency.
Whenever you add a dependency to your gradle file, it will download the added libraries, and add them to your project so that is available in your project. It makes it easy to manage external libraries in your project.
To study more , visit : https://developer.android.com/studio/build/dependencies.html
if you want to use external libraries or modules in your android
project you have to add the dependancies so that you may be given the
authority to use that particular library otherwise that will not be accessible to you inside the project.
so, its concluded that:
The Gradle build system in Android Studio makes it easy to include external binaries or other library modules to your build as dependencies. The dependencies can be located on your machine or in a remote repository, and any transitive dependencies they declare are automatically included as well
for further you can visit this link:
https://developer.android.com/studio/build/dependencies

Create a library with dependencies in android

I created a library 'LibA' which has dependencies on many 3rd party libraries like RecyclerView, EventBus etc. When i tried to include it in another project as an aar, Library was included successfully but dependencies did not came in aar.
Q1 How can I include dependencies in LibA, so that when some other project includes this library, it should not worry about internal dependencies of my library.
Q2 How does gradle manages dependencies of libraries, does it downloads all depenedencies at once, or first checks which are already available in the main project?
Q3 When someone includes a library from jcenter, does that brings all the dependencies with it?
Any help will very much appreciated. :)
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.
Q1 How can I include dependencies in LibA, so that when some other project includes this library, it should not worry about internal dependencies of my library.
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.
Q2 How does gradle manages dependencies of libraries, does it downloads all depenedencies at once, or first checks which are already available in the main project?
Gradle handles the dependencies for you. It doesn't add the same dependency twice or more.
Q3 When someone includes a library from jcenter, does that brings all the dependencies with it?
As said before, in this case the dependency has a pom file which describes all the nested dependencies. Gradle downloads it automatically.
Q1 How can I include dependencies in LibA, so that when some other project includes this library, it should not worry about internal dependencies of my library.
You should use some Dependency manager. For example Maven Central which has .pom file which defines all additional dependencies which should be used
Q2 How does gradle manages dependencies of libraries, does it downloads all depenedencies at once, or first checks which are already available in the main project?
gradle downloads all necessary dependencies. It creates a graph for dependencies, try to solve conflicts and download them
Q3 When someone includes a library from jcenter, does that brings all the dependencies with it?
If additional dependencies were not defined in pom file they will not be downloaded, only library

Android can't find the third library on AAR

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

Categories

Resources