I have a java library submodule holding interfaces which are used in another android library submodule. The problem is that the sources of the java library module are compiled but not included in the aar artifact of the android library project.
How can I include the sources of the java library in the android library aar file?
I ended up including the source files of the java library directly into the android library project using:
sourceSets {
main{
java {
srcDir("../java_library/src")
}
}
}
Only this way the sources are included in the aar file. Still will be great to know if it is possible using simple gradle dependency management.
Related
I have created a library (aar) in Android Studio that includes some utility methods that rely on Joda Time classes. I am using the dependency from jcenter with the following line of code in my gradle file
compile 'joda-time:joda-time:2.9.4'
I then added my library to a private maven repository and created a new Android project and added my library to it from this repo.
When I try to use my library's methods in the new project I get the following error:-
java.lang.NoClassDefFoundError: org.joda.time.DateTime
I tried going back to my library and instead of including joda time via jcenter I added the .jar file directly to my 'libs' folder and this time when I tried the methods in my new project they worked and I didn't get the above error.
Does this mean that dependencies are not actually compiled into the aar file when included from jcenter? Is there a way to do so?
The AAR file just contains the compiled dex code, manifest (partial) and resources for the library. When you copied the jar file for joda-time into your project, it gets built into the AAR. When using gradle dependencies, gradle (and therefore Android Studio) know how to resolve the joda-time library for building your AAR, but that is it. Since your AAR is being published on jcenter, you need to have a maven manifest for your AAR which also lists joda-time as a dependency. After that projects will be able to list just your library as a dependency and the dependency on joda-time will be resolved by gradle (via its maven support.)
I created a library project in Android Studio (currently 0.5.2) by choosing File > New Project... > "Mark this project as a library".
I have two other non-library projects that I would like to add a dependency to this library project.
-My Library
-Project 1 (depends on My Library)
-Project 2 (depends on My Library)
My goal is to keep each project independent and avoid duplicating modules/code. How can this be done without copying the library module into the other projects?
Update:
Android Studio 0.6.0 allows you to Import a module, though, this simply copies the module source into the Project.
You can also refer to a library outside of your project folder using the project().projectDir property. If your external library is relative to your project like so
- MyLibrary
- library
- MyProject
- app
in MyProject/settings.gradle
include ':library'
project(':library').projectDir = new File(settingsDir, '../MyLibrary/library')
in MyProject/app/build.gradle
dependencies {
compile project(':library')
}
This is very similar to this question:
Sharing an Android library between multiple Android apps using Gradle
Instead of pushing to maven central you can push to your local maven repository (mavenLocal() in build.gradle)
Another route (if you don't want to deploy the library somewhere) is to use your VCS and check out the library within your project. Git has submodules for that, Mercurial has subrepos and SVN has external to name a few examples.
Then add it to your Gradle build using a project dependency.
I would like to find out the best way in order to generate with Gradle an Android Library AAR archive containing all the dependencies resolved by maven, thus the application doesn't have to include those dependencies, just import the AAR and everything is configured by the android library.
The jars should be placed by the Gradle task in the AAR libs/ dir.
I was doing this, before the new AAR system, with a JAR containing multiple source sets, just because it was not possible to assemble a binary android library.
I have a library project(Project A) in Eclipse that itself depends on a library project(Project B). How to include the Project A and Project B(on which Project A depends) as a library project in my Android Studio main Project (Project M)? I know the answer of how to include a single library project. But my question is how to include a library project which itself depends on a library project.
You'll need to include both library projects (A and B) in your main project (M). Android Studio doesn't support a module with transitive dependencies on other modules that aren't specifically defined in the project's settings.gradle file.
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.
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.
Jar task does not include dependencies in the final jar artifact.
From Gradle documentation on jar task:
The jar task creates a JAR file containing the class files and
resources of the project.
It assumes that since you are building jar for your project, all dependencies will be provided during runtime. As opposed to war, where all dependencies are usually included in the final artifact.
If you need to create "fat jar", which will include the dependencies, then look into specific plugins, for example gradle-fatjar-plugin.
It's a little bit of a longshot, but if you're not using Android Studio 0.4.0 and you've just added the jar file, try cleaning your project and rebuilding from scratch. We've seen this bug: https://code.google.com/p/android/issues/detail?id=63366 where libraries don't get included without cleaning the project, though this bug refers to a dependency downloaded from Maven and not a local jar file (which may or may not be an important difference). This was fixed in Android Studio 0.4.0 (more specifically, in the Gradle plugin 0.7.0).