In an Android gradle project, I see the use of a library called Koin. Normally, as in all Android projects, you include the library in build.gradle like this:
implementation "org.koin:koin-core:$koin_version"
However in this project, there is nothing in any of the gradle files that contain this. I even did a file search to see where it is defined. The only place where I have seen it defined is when you select:
File > Project Structure > Dependencies
But when I build the project, it builds without any problems. How does gradle reference this dependency since it's not in the build.gradle file? Even though it's defined under:
File > Project Structure > Dependencies
there is no clear indication how gradle knows about this.
The dependencies can be located on your machine or in a remote repository, and any transitive dependencies they declare are automatically included as well.
dependencies {
// Dependency on a local library module
implementation project(":mylibrary")
// Dependency on local binaries
implementation fileTree(dir: 'libs', include: ['*.jar'])
// Dependency on a remote binary
implementation 'com.example.android:app-magic:12.3'
}
for more details Android build dependencies and kotlin Gradle
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 trying to test to reference without copying a library Project. So I created two projects one is ProjectA and one is LibraryA. Both projects are located inside the \StudioProjects folder. I am trying to reference LibraryA from ProjectA and I get the error at the title.
Here is settings.gradle from ProjectA
include ':app'
include ':LibraryA'
project(':LibraryA').projectDir = new File('../LibraryA')
Here is dependincies from app build.gradle of ProjectA
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile project(':LibraryA')
}
I am using Android Studio 1.5.1
If I remove compile project(':LibraryA') from dependencies it builds normally, however then I can't referance classes from LibraryA inside ProjectA.
In your settings.gradle (ProjectA) you are referring the wrong folder.
include ':LibraryA'
project(':LibraryA').projectDir = new File('../LibraryA')
Checking your image, the LibraryA folder is a root folder.
Gradle is searching for a "module" build.gradle while in this folder there is a top-level file configuration.
You have to refer to the module inside the LibraryA
project(':LibraryA').projectDir = new File('../LibraryA/app')
Of course you have to pay attention if some tasks or variable are defined in the top-level file (LibraryA). In this case you have to clone them inside your top-level file (ProjectA)
One of your projects does not have a valid gradle file. If your library is not a gradle project convert it to gradle project.
If you add your library as a module and select "gradle" when you are importing i think you'll solve your problem.
I just do not understand what you want to do. But try changing the following line:
project(':LibraryA').projectDir = new File('../ProjectA')
I've created an Android library project found here: https://github.com/dbotha/Android-Photo-Picker
The photo-picker library project itself has several dependencies of it's own:
// library build.gradle
dependencies {
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.squareup.picasso:picasso:2.5.2'
}
I've made this library project available on Maven Central so that it can be easily added to applications as a dependency:
// application build.gradle
dependencies {
compile 'ly.kite:photo-picker:1.1.2#aar'
}
The problem though is that when I come to add it as a dependency to a new Android application project it crashes because it can't find the Picasso dependency from the library project:
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.squareup.picasso.Picasso" on path
It's only when I explicitly add this dependency to the applications build.gradle do things work.
My libraries POM file dependencies looks correct: https://repo1.maven.org/maven2/ly/kite/photo-picker/1.1.2/photo-picker-1.1.2.pom
So I'm curious if applications that include my photo-picker library as a dependency always need to explicitly add all the photo-picker library dependencies too?
You are using the #aar notation.
It means that you want to download only the aar artifact, and no dependencies.
You can check this part of documentation:
Check the 1.4.1.2. Artifact only notation section:
An artifact only notation creates a module dependency which downloads only the artifact file with the specified extension. Existing module descriptors are ignored.
Using the #aar notation if you want to download the dependencies, you should add transitive=true.
I'd expect that omitting #aar it should work.
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.
I'm using Square's Wire library for my Android app, using Android Studio with Gradle.
I originally added the wire-runtime-1.2.0.jar into a libs folder in my module, and added the dependency to Gradle like this in my build.gradle file:
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
That worked fine.
I'm new to Gradle and Android Studio, but based on the way I'm depending on the Google Support and Play Services libraries, I thought I might be able to remove the wire-runtime-1.2.0.jar library from my repository and just declare a dependency like this (the line is from the Maven repository):
dependencies {
compile 'com.squareup.wire:wire:1.0.0'
}
But if I do that then I hit this error:
Gradle: package com.squareup.wire does not exist
Is there a way to set up this dependency without importing the JAR file directly? Or does that only work for libraries that you can install through the SDK Manager?
Some packages, like com.squareup.wire, have multiple artifacts in Maven Central. You need to choose the right one for your needs. In this case, the equivalent of wire-runtime-1.2.0.jar is the wire-runtime artifact, not the wire artifact.
Here's what your dependencies section should look like:
dependencies {
compile 'com.squareup.wire:wire-runtime:1.2.0'
}