Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Gradle only include classes from my library with project dependency

I'm building an Android app that has a dependency on a custom library, and Gradle is only willing to include my custom library when I use a project dependency, not when I use a files dependency to include the library's jar file. I'm building both my app and the library with the API levee 19 SDK.

failing dependencies section from build.gradle:

dependencies {
  compile 'com.android.support:appcompat-v7:+'
  compile files('libs/MyLibrary.jar')
}

If I use the above dependencies section, none of the class in MyLibrary.jar are included in the build apk file, as verified by extracting its classes.dex and running dexdump. I have also verified that all of the classes are present in the jar file I'm using.

If I use the following dependencies section, then all of the classes in MyLibrary are included in the apk file:

dependencies {
  compile 'com.android.support:appcompat-v7:+'
  compile project(':MyLibrary')
}

I'm using Android Studio 0.4.0, Gradle 1.9, and I think the Gradle plugin 0.7.1.

What is going on here? I'd really like to build my app with the API level 18 sdk to test compatibility, but I can't get that working unless I'm able to just use the jar file for my library.

like image 998
Greg Avatar asked Dec 23 '13 17:12

Greg


People also ask

How do I add a dependency project in Gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build. gradle file. This declares a dependency on an Android library module named "mylibrary" (this name must match the library name defined with an include: in your settings.

Where does Gradle get its dependencies?

Gradle can consume dependencies available in the local Maven repository. Declaring this repository is beneficial for teams that publish to the local Maven repository with one project and consume the artifacts by Gradle in another project. Gradle stores resolved dependencies in its own cache.

How do I resolve Gradle dependencies?

Given a required dependency, with a version, Gradle attempts to resolve the dependency by searching for the module the dependency points at. Each repository is inspected in order. Depending on the type of repository, Gradle looks for metadata files describing the module ( .


1 Answers

Okay, this was my fault. My library's build.gradle was configured to only include the source files in the jar output file. The following is incorrect Gradle code and will give you the same problems as I've had.

task jar(type: Jar) {
  from android.sourceSets.main.java
}

This answer shows how to fix the jar file creation. It's ugly, but it seems to work.

like image 85
Greg Avatar answered Nov 02 '22 23:11

Greg