Gradle transitive dependency to JAR inside of a library module - java

I'm migrating an Android multi-module project from Ant to Gradle.
We have some jars in the repo in the libs directory of a library module.
The dependency is as follows:
AppModule depends on LibModule.
The code in AppModule cannot access contents of the library jar that is in LibModule.
Although it can access from-source classes of LibModule (proving that the dependency in general is established).
Gradle documentation says, that all dependencies are transitive by default, but this experience seems to invalidate such claim. Is this a bug or is there some legitimate reason?
I've managed to hack it by adding a workaround dependency in AppModule:
compile fileTree(dir: '../LibModule/libs', include: ['*.jar']) // HACK!
But there should be a more DRY way to do this, right?
Gradle version: 2.1.
Interestingly, Android Studio appears to respect the transitiveness of the jar and does not signal an error.
The error occurs when I'm building using
./gradlew assembleDebug
Thy typical java error is signalled:
error: package net.jcip.annotations does not exist
import net.jcip.annotations.NotThreadSafe;
^
I know I can also specify deps in a maven-ish style, but we would like to be able to work with jars-in-the-repo as well, for our purposes.
TIA, Karol

Related

Do the dependencies in build.gradle generated automatically?

Does gradle work like go mod ? First importing some dependencies in java file such as import org.apache.hadoop.mapreduce.Mapper, then calling gradle build and gradle would generate dependencies in build.gradle automatically, also download all the jars needed to gradle cache.
Or, all the denpendencies we need must add to build.gradle manully? Which version should append to the denpendency, search in the maven repo one by one?
Thank~
Gradle uses the dependencies declared in the build file to create the compile classpath. Similarly it will use test dependencies for the test compile classpath and the test runtime classpath.
So you have to declare the dependencies first, including figuring out which version you want to use in your project, and then you will be able to compile and run code that leverages these libraries.

Gradle Build Error - Program type already present

I'm trying to build an android project which has two modules, one is an application and the other is a library. Both modules have gson-2.8.5.jar files included in their respective libs folder. When I deploy the application, it fails during build with the following error message.
Program type already present: com.google.gson.FieldNamingPolicy$6
Message{kind=ERROR, text=Program type already present: com.google.gson.FieldNamingPolicy$6, sources=[Unknown source file], tool name=Optional.of(D8)}
However, I tested my setup by replacing hard provided .jar files dependencies with gradle's (implementation 'com.google.code.gson:gson:2.8.5') and it worked fine. But I have to use jar files in libs folder since I'm going to use Android.mk file to build the entire project later on, therefore can't depend on gradle injected dependency.
I searched about this error but unfortunately didn't find any useful results. Hope someone knows how to fix this.
Got it fixed. Seems like I had to mark transitive property to false for my included module.
Before:
implementation project(path: ':myLib')
After (fix):
implementation project(path: ':myLib', transitive: false)
Had the same issue with library:
implementation project(':library')
debugImplementation project(':library')
releaseImplementation 'com.library:1.0.0'
With
./gradlew project:assembleRelease
Gives the error.
Solution:
implementation project(':library') should be removed.

How to fix warning about "dependency is ignored for debug..."

I have java module, that used not only for android projects, but also for java projects. Java module used json, so it has dependency:
compile 'org.json:json:20160810'
Of couse, on build android project I has warning:
WARNING: Dependency org.json:json:20160810 is ignored for debug as it
may be conflicting with the internal version provided by Android.
In case of problem, please repackage with jarjar to change the class packages
I'm use module with sources, not as jar and I do not want install module to local repository.
How I can fix issue with warning?
I know, that for jar I can use exclude group/module, but it do not work for module.

Gradle (Android) - Multiproject dependency missing in testCompile

I have a Library project A and another Library Project B. The project B imports as a compile dependency the project A and I wanted also to add this same dependency for the unit tests, so I can mock some classes from project A (not importing the test folder, but the actual project).
Even with gradle sync working properly, and ./gradlew projectB:dependencies showing the tree with the correct dependencies, in my test classes in project B I cannot access the classes from the A project.
dependencies {
//Project B dependencies
compile project(":projectA")
testCompile project(":projectA")
}
Which is even more fun is that if instead of using the project(":projectA") aka DefaultProjectDependency I use the "group:artifact:version" way aka DefaultExternalModuleDependency by installing Project A locally, it works properly.
For me it sounds like a bug, not sure if it is an Android one or a gradle one.
Versions:
gradle - 2.11
android gradle plugin - 1.5.0
Also opened an issue in b.android: https://code.google.com/p/android/issues/detail?id=201820&thanks=201820&ts=1456399375
Finally this was an error in the Android Plugin and according to the comments in this bug report it will be fixed in 2.0.0 beta 7.

How to include dependencies in android library gradle project?

I'm building an android library project with Gradle using Android Studio. It has some local dependencies:
compile project(':androidlibrary')
with nested, additional external dependencies:
compile group: 'com.google.guava', name: 'guava', version: '14.0.1'
I managed to build the aar file but no external or local dependencies are included. I expect external dependencies to be mentioned in the POM file when published to maven, but what about the local ones?
What is the right way to build such project?
I ended up using "download library from maven" feature of Intellj IDEA as described here. It downloads the library and all it's dependencies to the local directory.
Just a friendly warning: including dependencies is considered a very bad practice at SHOULD BE AVOIDED. It may cause conflicts.
There is no automated way to package the dependencies of the library inside the aar. This is contrary to proper dependency management and can lead to problems down the line.
If you really want to do this, you'd have to gather the dependencies manually (using code in build.gradle) and manually package the files in the aar.

Categories