Android include all dependencies in a output library - android

I've created an android library which use itself another library (aar). When i use my generated aar file in a new project i can not execute functions which use the other library code. How can generate an aar with all library dependencies included?

You need to use api for adding your dependencies in your library instead of using implementation. Usingapi` will expose your dependencies to the world. Something like this:
dependencies {
api 'com.android.support:appcompat-v7:27.1.1'
api 'com.your.library:libraryName:1.0'
}

Related

Understanding "repositories" tags

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).

Create a Kotlin library in Android Studio

I am very new to both the Android and JVM platforms. Using Android Studio, I would like to create an Android app and put most of my business logic in a library. I would also like to just use Kotlin, no Java.
When I go to File > New Module, the options listed are
Phone & Tablet module
Android Library
Instant App
Feature Module
Android Wear Module
Android TV Module
Android Things Module
Import Gradle Project
Import Eclipse ADT Project
Import .JAR/.AAR Package
Java Library
I can create a Kotlin-based library with the Android Library option, but this also includes a lot of files for dealing with resources and controls.
I just want a pure Kotlin library. What is the easiest way to create one?
Can I delete a portion of an Android Library?
Can I change some settings in a Java Library?
Can I download a plugin that will just give me the option to create a Kotlin library?
I am still a bit confused the file organization in Java/Kotlin/Android projects.
You need a module with no Android dependencies and resources - this is what a Java library module does, so you start by creating one of those. From there, you just need to enable Kotlin inside this module.
The build.gradle file you get when you create your module is something like this:
apply plugin: 'java-library'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
You need to add the Kotlin plugin and standard library. If you don't have the mavenCentral repository added for all modules in your project yet, you need to add that as well:
apply plugin: 'java-library'
apply plugin: 'kotlin'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
repositories {
mavenCentral()
}
This is assuming that you have a kotlin_version declared in your project level build.gradle file, which is the usual way for Android projects, and I believe that's how Android Studio configures a project if you tick the checkbox to use Kotlin.
With those changes to your library's build.gradle, you can remove the automatically generated MyClass.java file from your library's source folder (/src/main/java/your/package/name), and start adding Kotlin files instead. If you'd like, you can also rename the /src/main/java folder to /src/main/kotlin. Or you can use both folders, this is entirely up to you.
Create Android studio project
After that create as many as module which are basically Android library
You can select the language of module (java/kotlin)
Extra
Upload your module to github and use jitpcak tool to make your module a library like this
Implementation 'com. My. Module:1.0'
And use your module any future project

Why dependency defind in lib module is not visible in app module?

I have an Android application. It contains two modules of app and pax-lib. app module depends on pax-lib module.
I have libs folder under pax-lib that contains some jar files. I have linked them in to gradle file of this module and use it across this module without any issue. This is how I have defined them:
dependencies {
...
// Local libs not in Maven Central
implementation files('libs/commons-io-1.3.2.jar')
implementation files('libs/commons-lang3-3.2.1.jar')
implementation files('libs/httpclientandroidlib-4.3.0.jar')
implementation files('libs/Kahuna_442.jar')
implementation files('libs/mapquest-android-sdk-1.0.5.jar')
...
}
This is how I defined this dependency in gradle file of app module.
dependencies {
implementation project(':pax-lib')
...
}
I am able to use all classes I have defined in pax-lib without any issue, however, I am not able to use .jar files that have defined in Gradle file of pax-lib module. My expectation is to be able to use them as I was in Gradle version below 3.0.
I must be able to copy/paste these jar files under app module but I want to make sure I am not doing something wrong first.
Use api rather than implementation

How to include a dependency inside an Android library?

Currently i'm writing a library for android that needs Volley to function. Currently, the Volley dependency is declared in both the dependencies block for the library and whatever app uses the library. What do I need to do so that my Library can pull in its needed dependencies itself, instead of having the implementing app also declaring the dependency?
Gradle supports transitive dependencies.
For a local library, this works like this:
compile(project(:LIBRARY_NAME)) {
transitive=true
}
For remote libraries:
compile ('com.somepackage:LIBRARY_NAME:1.0.0'){
transitive=true //default, normally no need to specify explicitly
}

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