gradle - Cannot find sub module for maven library - java

I am trying to make an android library public on maven, this artifact contains another three modules, and each modules is an aar file, which means it has string, layout resources. The structure of the library A is:
+ Library A
-- Module 1
-- Module 2
-- Module 3
And in build.gradle of library A, I include these three modules by using
compile project (:module1);
compile project (:module2);
compile project (:module3);
But when I use this library in new project's gradle, it cannot find all the submodules. The error message is:(assume all modules version is 1.0)
Could not find module1:1.0
Searched in the following locations:
file:/Users/chan/Documents/AndroidSDK/sdk/extras/m2repository/project/modules/widgets/module1/1.0/module1-1.0.pom
file:/Users/chan/Documents/AndroidSDK/sdk/extras/m2repository/project/modules/widgets/module1/1.0/module1-1.0.jar
file:/Users/chan/Documents/AndroidSDK/sdk/extras/google/m2repository/project/modules/widgets/module1/1.0/module1-1.0.pom
file:/Users/chan/Documents/AndroidSDK/sdk/extras/google/m2repository/project/modules/widgets/module1/1.0/module1-1.0.jar
file:/Users/chan/Documents/AndroidSDK/sdk/extras/android/m2repository/project/modules/widgets/module1/1.0/module1-1.0.pom
file:/Users/chan/Documents/AndroidSDK/sdk/extras/android/m2repository/project/modules/widgets/module1/1.0/module1-1.0.jar
https://maven.google.com/project/modules/widgets/module1/1.0/module1-1.0.pom
https://maven.google.com/project/modules/widgets/module1/1.0/module1-1.0.jar
https://maven.com/artifactory/libs-release-local/project/modules/widgets/module1/1.0/module1-1.0.pom
https://maven.com/artifactory/libs-release-local/project/modules/widgets/module1/1.0/module1-1.0.jar
https://repo1.maven.org/maven2/project/modules/widgets/module1/1.0/module1-1.0.pom
https://repo1.maven.org/maven2/project/modules/widgets/module1/1.0/widgets-unspecified.jar
Same error for module2 and module3.
I dont know why it cannot find the modules.
The Library A is configured in new project:
compile ('LibraryA:1.0#aar') {
transitive=true
}
The followings are what I have tried so far, with no luck:
I have added mavenCentral(), mavenLocal() or jcenter() in reporsitories() but still no luck.
Tried using fat-aar android https://github.com/adwiv/android-fat-aar on library. No luck.Same error
Added flatDir in reporsitories, no luck
Any suggestions would be much appreciated!

Related

Gradle dependencies: package does not exist

I know this has been asked multiple times but the questions have multiple answers.
I'm trying to use a Java package that's a dependency of my dependency. Let's say I've built this gradle project called "ee_tools". And that has a dependency called "my_models", which has a package called "com.mycompany.my_models.db". So in the ee_tools project build.gradle, I've got
dependencies {
// My stuff
implementation group: "com.mycompany", name: "my_models", version: "1.0.0"
}
Then in my current project, I've got
dependencies {
// My stuff
implementation group: "com.mycompany", name: "ee_tools", version: "1.0.0"
Shouldn't this mean that the public classes in my_models are accessible through ee_tools to my current project? Gradle was able to find both in my artifactory instance. And the gradle dependencies command shows ee_tools under the compileClasspath, implementation, and testCompileClasspath trees, but not with its children dependencies. It also shows up in the runtimeClasspath and testRuntimeClasspath trees with its children dependencies, including my_models.
I am also able to see that package inside the jar on the left side of IntelliJ, under the "External Libraries" tree, along with some classes.
But when I try to use the my_models package in my current project, IntelliJ can't find it and it fails a gradle build with the error
error: package com.company.my_models.db does not exist
It can't find any package in that dependency. What gives? Am I declaring the dependencies incorrectly? Or is this a gap between the gradle binary in my command line vs IntellJ and gradlew?
If ee_tools depends on my_models, then your gradle file in ee_tools should be like
implementation project(path: ":path:to:my_models", configuration: 'default')
:path:to:my_models is defined in settings.gradle in project root path like this:
include ':path:to:my_models'

Apply gradle custom module plugin

I created a plugin for gradle with java.I want to test it.
so, I created a another gradle project add I added my plugin as a module.
Now , How can I apply my module as a plugin in build.gradle file ?
My project structure
My plugin
version '1.0.0'
id = 'com.hello.first.plugin'
implementationClass = 'com.hello.first.plugin'
Here I saw for applying jar as a plugin in gradle : ANSWER.
How can I add module as a plugin in build.gradle ?
You need to make it a standalone project and publish it, at least to mavenLocal().
Then just add mavenLocal() into buildscript.dependencies and it should resolve.
A local flatDir repository should work, too.

How to put local dependencies first when calling gradle idea?

When calling gradle idea, external dependencies are ordered first in the class path relatively to local Jar inclusions. As such :
dependencies {
compile fileTree(dir: 'libs', include:['*.jar'])
compile group: 'foo', name:'bar', version:'1.0.0'
}
will include my local jars last. This is a problem in my project as these jars' purpose is to partially overwrite the external library.
The same behavior is observed when specifying the repository as a source of dependencies using flatDir and loading the jar without fileTree. It is put last in the classpath.
I have found several mentions of the problem when researching, such as https://discuss.gradle.org/t/gradle-messes-up-the-classpath-order-in-generated-projects-when-there-are-mixed-dependency-types/13130, but no workarounds.
I suppose these exist, gradle being very customisable, but being very new to it my attempts to make one fail. How to proceed?
I'm not using IntelliJ on a regular basis but tried it in the context of this question and my impression is that gradle's idea plugin and IntelliJ's gradle plugin don't go well together. That is you should either use the idea gradle plugin and import as plain Java project or import as gradle project using IntelliJ's gradle plugin. Main reason is that the idea plugin and the IntelliJ plugin are generating slightly different iml-files (those files are holding the project dependencies - amongst others) which leads to lot of confusion when using both plugins together. As you specifically asked for the gradle idea plugin, I used this plugin and imported into IntelliJ as plain java project.
But to answer your question I found no evidence that the order of libraries on the classpath differs from the order as declared in the dependencies section of the gradle file, when using a flatDir repo. When using compile fileTree(dir: 'libs', include:['*.jar']) the order was actually broken as described in your question. That is, you should stick to using a flatDir repo.
I'm using gradle 4.9 and IntelliJ 2018.2.
This is my gradle file
apply plugin: 'java'
apply plugin: 'idea'
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
dependencies {
compile 'zzz:zzz-0.0.0'
compile 'aaa:aaa-0.0.0'
compile 'com.google.guava:guava:24.0-jre'
compile group: 'javax.websocket', name: 'javax.websocket-api', version: '1.1'
}
task wrapper(type: Wrapper) {
gradleVersion = '4.9'
distributionUrl = "http://services.gradle.org/distributions/gradle-${gradleVersion}-bin.zip"
}
In my libs folder there are two jars aaa-0.0.0.jar and zzz-0.0.0.jar both are copies of guava-24.0-jre.jar. That is all guava classes are present in both jars as well. As zzz:zzz-0.0.0 is the first dependency in the gradle file, the expectation would be that guava classes are being loaded from zzz-0.0.0.jar instead of guava-24.0-jre.jar or aaa-0.0.0.jar. I used the following main class to test this:
package test;
import com.google.common.math.LongMath;
public class Test {
public static void main(String[] args) throws Exception {
System.out.println(LongMath.class.getProtectionDomain().getCodeSource().getLocation().toURI());
}
}
And the output when running it from IntelliJ is
file:/C:/ws/gradle-idea-test/libs/zzz-0.0.0.jar
That is the com.google.common.math.LongMath class is indeed being loaded from the local libs/zzz-0.0.0.jar instead of the guava-24.0-jre.jar.
I noticed that the list of external dependencies in IntelliJ doesn't show the local libraries. And even more confusing the libraries are ordered alphabetically and don't reflect the actual order on the classpath which might be quite confusing:
To get the actual order of elements on the classpath you will have to look in the module dependencies section in the module settings ("Open Module Settings" > "Project" > "Modules" > "Dependencies Tab") which looks like this:
As you can see the dependencies are listed in correct order and include the local libraries as well. The order of libs in this dialog is basically the same as in the generated iml-file.
When using the IntelliJ gradle plugin instead of gradle's idea plugin, IntelliJ basically behaved the same way but the generated iml-file looked different and the external libraries were displayed in a different format. But there was no difference regarding the classpath order.

Gradle dependencies of local dependency

So let's say I have project A, which requires certain dependencies. And I have project B, which has project A as one of its dependencies. So I have the following code in project B's build.gradle:
project B's build.gradle:
dependencies {
compile files("projectA_dir/build/libs/projectA.jar")
}
However, since Gradle does not include project A's dependencies in projectA.jar by default. I was wondering if there's a way to let project B successfully compile project A without creating a fatjar for project A.
The jar file doesn't contain the transitive dependencies and doesn't have a pom file which describes the dependencies used by the module.
It means that, if you are importing a jar file using compile files you have to specify the dependencies also in your project.
You should use a maven repository, private or public, to avoid the issue.
In this case, gradle downloads the dependencies using the pom file which will contains the dependencies list.

Java only project not adding dependecies on Android Studio

I am using Android Studio 0.8.2 and in my project I have one Android module and one Java only module.
The java only module depends on a external library. I have tried adding it in the libs dir of the project:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
And using maven repositories:
repositories {
mavenCentral()
}
dependencies {
compile 'net.sf.kxml:kxml2:2.3.0+'
}
Both systems allows compilation but the library is not included on final jar. I have unzipped the jar and the classes are not there!
Exception in thread "main" java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException
A Gradle sync shows this warning message:
"A top-level `dependencies` block should only appear in build files that correspond to a module"
Any idea? Thanks advanced.
Looks like you are not declaring dependencies in build.gradle in app folder, but in the main roof of project? Am I right? You should compare declaring dependencies with other projects like hmm on github. Example: https://github.com/loopj/android-async-http/blob/master/sample/build.gradle (this is build.gradle file in module project, if you go up to root, in build.gradle file there is not declaring not connected to gradle depenncies). Read carefuly the comments and warnings.

Categories