I created an android library that acts as a wrapper. I use some dependencies in the library notably Volley and Google Play Services. When I try to paste the .aar file in the project and compile it these libraries are not being imported and they throw an error. When I manually add these dependencies in the build.gradle of the new project then they work fine.
Is there way so that whenever my library is added it will auto import all the required dependencies?
just a friendly warning: including dependencies is considered a very bad practice at SHOULD BE AVOIDED.
However, if you do still want to do that, I suggest using the "download library from Maven" feature in Android Studio as described here.
This will make sure the library and all of its dependencies are downloaded.
Related
I am developing my own SDK, which in turn depends on some 3rd party SDKS. For example - OkHttp.
Should I add OkHttp to my build.gradle, or let the users of my SDK include that? In this scenario, they will probably "anyway" use it, so its safe to say they already have it.
Another point to add - not all paths of my SDK needs "OkHttp", so, in theory, some user of my SDK could use those parts only, and have not OkHttp on his APK.
Another thing I am contemplating:
If I do embed OkHttp on by build.gradle - how can users of my SDK use that OkHttp library, instead of consuming another replica?
Should I add OkHttp to my build.gradle, or let the users of my SDK include that?
Adding the dependencies in build.gradle doesn't mean packaging the dependencies inside the aar file.
The aar file doesn't contain the transitive dependencies and doesn't have a pom file which describes the dependencies used by the library.
Uploading the artifact in a maven repository you will have your aar and a pom file which will contains the dependencies list.
In this way gradle will automatically download all the dependencies tree and you can configure gradle to exclude same libraries.
Use implementation and package it - the consumer can still exclude it.
One cannot depend on something and then not package it; this won't build.
In the application package, it can/must only exists once ...so what's the point?
I will answer your questions in a reverse order
Another thing I am contemplating: If I do embed OkHttp on by build.gradle - how can users of my SDK use that OkHttp library, instead of consuming another replica?
How Gradle build system works is suppose, In my project I use your library and I'm using v2 of OkHttp and your library is using V1 of OkHttp, then the gradle will automatically use the latest version. You can read about it here
Another point to add - not all paths of my SDK needs "OkHttp", so, in theory, some user of my SDK could use those parts only, and have not OkHttp on his APK.
In my project I use your library and it uses OkHttp, whereas I don't use it in my project also, I'm not using the part of your library where you are using OkHttp but still my APK will include OKHttp in it. This can be avoided either by splitting your library into two separate libraries or me using proguard in my Project.
Should I add OkHttp to my build.gradle, or let the users of my SDK include that? In this scenario, they will probably "anyway" use it, so its safe to say they already have it.
You should not bundle it in your library you just use implementation and let the user of your library decide if he wants to exclude it or not.
You need knows about api and implementation in the gradle
The link will be helpful
Api:
Role: Declaring ,API, dependencies
Consumable? no
Resolveable? no
Description: This is where you should declare dependencies which are transitively exported to consumers, for compile.
Implemetation:
Role: Declaring, implementation, dependencies
Consumable? no
Resolveable? no
Description: This is where you should declare dependencies which are purely internal and not meant to be exposed to consumers.
Upgrade to Android Studio 3.0.0 mentions this, and doesn't elaborate on how to handle it:
Library modules no longer process local JARs. This is to speed up incremental builds that are caused by changes to a library module's code.
So I have a project with a library project in it. In my library project's build.gradle file I have this:
compile files('libs/com.somelib.somepackage.jar')
I changed compile to implementation and when I tried to run my app, all my classes that tried to access the import com.somelib.somepackage.SomeClass import statement threw an error that this package didnt exist.
I changed back to compile and I was able to build and run my app.
I want to comply to the new rules since compile is deprecated and will be removed with the next Gradle release, so how do I go about doing that?
If you are trying to access classes from the .jar that is included in the library project from the app project, you will have to use api instead of implementation otherwise the classes will only be accessible in the library project:
implementation files('libs/com.somelib.somepackage.jar')
should be
api files('libs/com.somelib/somepackage.jar')
As said by 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 ...
Reference:
https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#new_configurations
The title is a little misleading but I honestly don't know how to word it in any other way. This is my project structure:
I want to use the highlighted (fasteranimationscontainer-master) in my current app that I'm working on. I imported it by putting the jar file in my library folder then adding it to my library through Android Studio. But I try to create an object of that imported library, it doesn't show up/import.
I'm still learning how to use android studio so any help would be much appreciated!
1. Library with only the Source Code
To include a library with only the source code (like FasterAnimationsContainer) to your project, you only need to import it as a module from File -> New-> Import Module in Android Studio:
There should be a dialog for the project folder, enter the path where your library reside.
Then you need to add the module to your app build.gradle as a dependency:
android {
...
dependencies {
...
compile project(':fasteranimationscontainer')
...
}
}
Now you can use it in your project.
2. Library from Maven or JCenter
To add dependencies to your project where the library has uploaded to maven or jcenter, you need to modify your app build.gradle file and add extra lines configuring the packages you require. For example, for certain Google or Android, dependencies look like:
android {
...
dependencies {
// Google Play Services
compile 'com.google.android.gms:play-services:6.5.+'
// Support Libraries
compile 'com.android.support:support-v4:22.2.1'
}
}
Try to always read the library README first.
Suggestion:
If you still learning using Android Studio, you can read Using Android Studio. Then read Getting Started with Gradle, because Android Studio is tightly related with Gradle.
I have a library which itself has a few dependencies, namely Realm, Retrofit, as well as a native library. The native library is on github and I can successfully pull it into my project via jitpack.
I have a sample app which im using to test this library. In my sample apps 'app' module build.gradle i my dependencies block looks somewhat like the following:
dependencies {
compile project(':sdk')
...
//compile realm,rx,retrofit, etc..
}
When doing this, my sample app works correctly.
Now lets say I either:
Grab the sdk's generated .jar file and put it in my sample apps /libs
Get the .aar and do the same as above
Put the repo on jitpack and try to download it via compile 'xxxx'
Trying to include the sdk in my sample app any of these other ways does not seem to work and spits out unhelpful errors.
What could possibly be the issue? I got a hint that it may be an issue with 'transitive dependencies' but am not sure where to start.
Ideas?
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.