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
Related
I know the way using 'flatDir'.
But android studio warn do not use flatDir.
My project is android library.
I add jniLib in sourcet and use implementation files.
Then, I could add direct aar dependency to my lib and app which use my lib.
But I can't add dependency to androidTest in my android library project.
I want to use androidTestImplementation but my project is multi flavor build and there is no API flavor[Debug|Release]AndroidTestImplementation with files arguments.
I tried to search the way, but failed.
Now, I use flatDir and ignore warning.
How can I solve this problem?
I am confused why same repositories tags are required in multiple places in an Android project. Why don't we have all repositories under one tag, and the compiler searches everything from there.
My particular case is:
I created a library module and added it in a project. In my library gradle file, I have:
implementation 'com.github.jkwiecien:EasyImage:1.3.1'
for which I added maven { url "https://jitpack.io" } in repositories tag in the library's gradle file. I want EasyImage in the library only, not in the project. But it wont compile until I added this same jetpack.io in Project's app/build.gradle file. Why do we need to do this?
I want to distrubute my library and I don't want the users to add things that are already added in my library and are not required by their project.
Edit:
If you put jitpack repository url in your Project level build.gradle instead of app level build.gradle it will work for both. When you upload your library to JitPack, it automatically 'builds' your library for you but in your case your library is being built locally so it needs the repository url in each build file to build them separately. However you should keep the repository url in your library because eventually you will be distributing it and JitPack won't be able to build your library module if you have the url in your project build.gradle file instead of repository.
Previous Answer:
From what I understand:
You are using EasyImage in your library, and when you add the ibrary to your project, you want to use the same EasyImage library that you loaded in your library instead of adding it to your project.
if you load your library using 'implementation' like this:
implementation 'com.github.you:yourlibrary'
you will not be able to access dependencies that 'yourlibrary' uses. But if you load it using 'api'
api 'com.github.you:yourlibrary'
Now you can access EasyImage from this library instead of adding it again.
This was added in Gradle 3.0 and it works the same way as 'compile' keyword used to work(which is deprecated now). You should checkout this article for detailed explanation.
Why this behavior?:
By using imepentation,
if any implementation in EasyImage is changed, Gradle just needs to recompile EasyImage and Your library as any other class which does not import your library directly cannot use any implementation of it.
But if you use api to load library, If any change is implemented inside EasyImage, gradle needs to recompile EasyImage, Your library and all other modules which import your library as any other module might use implementation of EasyImage (like your app).
I am using Android Studio 1.2
I create a private library I want to use that one in another application.
To use it I create an AAR files, but this AAR don't work. I have in my library a dependency to an AAR file.
The AAR files do not the dependencies?
If I use the jar and I includ ans create all the dependencies the project works fine.
NOTE :
I know how to importe the AAR file. The problem is to use an AAR in the AAR..
Thanks.
If I'm understanding your question correctly, there are 3 projects involved:
Library Project 2 --> Library Project 1 --> Application Project
You are editing "Library Project 1" and have added to it's app/build.grade a dependency on the Library Project 2's aar. Something like this: compile 'com.arasthel:gnavdrawer-library:1.1.5'
I am not sure where you are running into an issue, but I'll attempt an answer anyway. If I'm completely off-base, can you please elaborate on how the AAR dependency is not working? Any error messages?, a class/resource not found, etc.
I think it's unlikely you are unable to use a class from Library Project 2 inside Library Project 1, because I just tried this myself and it seems to be working just fine. It's worth noting that the Library Project 1 aar file will NOT include classes or resources from Library Project 2. Library Project 2 will be noted as a dependency in Library Project 1's pom if published using gradle's maven plugin to publish Library Project 1.
My guess is that you are having a problem in the Application Project? Perhaps the class from Library Project 2 is not found in the Application Project?
If that is correct, then there are two possible solutions:
Enable transitive dependencies on the aar dependency in the Application project's app/build.gradle: Instead of compile 'com.example:myLibrary:versionX', make it compile('com.example:myLibrary:versionX'){transitive=true}. I just verified this causes gradle to read Library Project 1's pom and automatically add dependencies found there into the Application Project.
If you would like to use transitive dependencies, your Library Project will need to be generating a pom and publishing it along with the aar. See https://stackoverflow.com/a/30085677/431296 for some additional information on how I have this working.
Manually add the dependency on Library Project 2 to the Application Project - so that your Application has a dependency line for both Libraries. Depending on your specific situation this may or may not be a workable solution.
Add following code to you project build.gradle file, and you should put you AAR file to the libs folder.
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
And finally add compile info to your dependencies:
dependencies {
compile(name:'AARFileName', ext:'aar')
}
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
I'm using the new Android build system that is based on Gradle, together with the early access preview Android Studio. Now, I have two projects: an Android library project, and an Android app project (basically a demo for the library).
In the library project I have added a dependency to the gson library, so my build.gradle file looks like this:
dependencies {
compile 'com.android.support:support-v4:13.0.+'
compile 'com.google.code.gson:gson:2.2.+'
}
Still, everything works fine and dandy and I'm able to use gson in my library and then my app. But I want to understand where this library is embedded. I've opened both the .aar that is built by the library project and the .apk of the demo app. I was expecting to find the jars for the two dependencies in at least one of these, but I didn't.
So where are they?
From Android Tools website:
These items, plus the output of the compilation of the project’s own source code, are sent to dex for bytecode conversion and inclusion in the final APK.
In other words, they are in your *.dex file inside the APK.
As #SharkyXTS said, the code from any external dependencies is compiled into the final .dex file inside your APK. The reason why you can't find any references to these dependencies in the .aar is because there aren't any.
The .aar format is only supported through Maven for now, so dependencies are found through there. I believe there are plans to eventually support local .aar dependencies (without Maven), but the Android plugin isn't quite there yet. You can see this issue for more information.