Use a Library in the processor's generated classes - java

I'm developing a library to generate classes using annotations and processors. The generated classes should use Gson library from google.
My question is : Where should I add the Gson dependency ? I'm currently adding it into the processor build.gradle but when the classes are generated, Gson is not found and Android Studio is suggesting to add it to the app module.
build.gradle of the processor :
implementation project(':lib-annotation')
implementation 'com.squareup:javapoet:1.9.0'
implementation 'com.google.code.gson:gson:2.8.1'
implementation 'com.google.auto.service:auto-service:1.0-rc3'
build.gradle of the app :
implementation project(':lib-annotation')
annotationProcessor project(':lib-processor')
Any help would be greatly appreciated, thanks!
P.S. The project is meant to be a library. I expect the users to only include my library in their gradle file, not the "sub dependency".

Simply using implementation prevent you from using transitive dependency.
The main project can't use or invoke the dependencies you added to your library.
As reminded to docs the implementation configuration should be used to declare dependencies which are internal to the component.
So you have to use api OR compile instead of implementation or add a condition to your transitive library e.g :-
implementation ('com.google.code.gson:gson:2.8.1'){
transitive = true;
}

Besides the fact implementation hides the dependency and therefore keeping it out of the app's path (so if you need to expose Gson, avoid using implementation), the problem is due to being compiling/implementing the library module instead of using a regular aar/jar/apklib package. Try this:
-Add the Android Maven Gradle plugin to the library project and configurate it.
-Using the plugin, compile and install the library into your local maven repository.
-instead of using compile/api/implementation for adding the library to the app, include mavenLocal() into your build.gradle, and then add the library as a regular dependency. All the necessary dependencies should be there.
here's an example of the plugin, in case you need it: https://github.com/fcopardo/EnhancedMapView

follow this link, I have imported without any problems.
http://blog.madadipouya.com/2015/09/21/how-to-add-gson-library-to-android-studio-project/

Okay, I finally fixed the problem!
I had to add the Gson dependency into the processor build.gradle using implementation or compile :
implementation 'com.google.code.gson:gson:2.8.1'
And add the dependency again into the annotation build.gradle using compile :
compile 'com.google.code.gson:gson:2.8.1'
I think it wasn't working because the Gson dependency was in the processor build.gradle which is included using annotationProcessor 'lib-processor' (Somehow the "transitivity" wasn't applying). Therefore I put the Gson dependency in the annotation build.gradle since I include it using implementation 'lib-annotation' and it worked.
Hope it will help

Related

How to import 3rd party library using mavin into Java project using gradle?

I want to use the sxcml-java library in my son's school's robotics code (currently a private repo).
The library uses Maven. I was able to successfully include the library in a test project using Maven.
However, I've just discovered that the existing robotics project code uses Gradle. I don't know either Maven or Gradle, and I haven't programmed in Java in almost 30 years.
How can I most easily use scxml-java - which itself has external 3rd party dependencies — in the robotics project?
This question is similar to this one, but the solution there was easy because both projects were using Gradle.
Provided the package is published in an artifactory, which is the case (See here), you can just include it as any other Gradle dependency (using groupId, artifactId and version), regardless of what build system was used to build it in the first place.
dependencies {
implementation 'com.nosolojava.fsm:scxml-java-implementation:1.0.1'
}
If you use IntelliJ IDEA, pasting the Maven dependency block into the build.gradle file will automatically convert it into the Gradle dependency format like the one above.
Please note however this does not apply to plugins, only to regular dependencies.
If You install your jar or third party jar into maven local repo like ~/.m2
you can add mavenLocal()
repositories {
mavenCentral()
// * Require by Use JAR install to Maven Local Repo your .m2
mavenLocal()
}
then add implementation to dependencies
dependencies {
implementation 'com.google.guava:guava:31.1-jre'
implementation 'yourGroupId:yourArtifactId:yourVersion'
}
Please mapping yourGroupId , yourArtifactId, yourVersion from your pom.xml
If You only download third party jar into foler like /home/yourName/your-libs
you can add configurations
configurations {
sxcml-java-lib
}
then add dependencies
dependencies {
implementation 'com.google.guava:guava:31.1-jre'
//sxcml-java-lib fileTree(dir: "${System.getProperty("user.home")}/libs", include: "*.jar")
sxcml-java-lib fileTree(dir: "/home/yourName/your-libs", include: "*.jar")
}

How do I add libraries to my Gradle file?

It's probably very simple, but only to people who know what they are doing.
I have a Java program that imports these two:
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
As an aside, I don't want to use the lang3 package but the lang package.
I do not have anything in my Gradle file about these. When I try to build the file, it gives me errors for these two, saying the packages do not exist.
My questions are:
Do I need to add them as "compile" or as "api"?
What is the exact syntax? I have lines that look like this:
api group: 'commons-httpclient', name: 'commons-httpclient', version: '3.1'
How do I find the right name (or should I just invent one)? and the version?
Anything your code needs (besides basic JRE classes) is a dependency for your code. Gradle manages these dependencies, usually downloading them from a repository.
First you need to find such a repository. You probably have repositories already configured in your build.gradle, like so:
buildscript {
repositories {
mavenCentral()
// maybe more repositories
}
}
That means Gradle will try to download dependencies from Maven Central. You can either do a web search for "gradle" and your dependency, or go to repository and search, or check the dependency's homepage.
You'll end up with a dependency name and version like 'org.apache.commons:commons-lang3:3.12.0'. This needs to go in your build.gradle.
Gradle has different dependencies:
buildscript dependencies provide code that Gradle needs to execute to build your project, e.g., a tool to pull in version control system information or generate code
implementation dependencies are dependencies your code needs to run, like a logging framework or JSON parser or PDF generator
test dependencies are dependencies needed to run your automated tests, like JUnit
Depending on where you need the dependency, you put it in the buildscript or the dependencies block.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.tmatesoft.svnkit:svnkit:1.9.+'
}
}
dependencies {
implementation 'pdfbox:pdfbox:0.7.3'
testImplementation 'junit:junit:4.+'
}
You don't need to repeat the implementation dependencies for the testImplementation btw, as it inherits them automatically.
You can define your own configurations as need; see the Gradle manual on dependencies, for example if you have different test suites (unit, integration, performance, ...) that need different dependencies.
you'll have to go to their official website and get the implementations then add those to the dependencies(you'll find it at the bottom of the file) in the build.gradle(module) it would look something like -
the $lifecycle_version might be somethiing like 1.2.3 or some version number.
this is what I got (not exactly sure if this is right)-
implementation 'org.apache.commons:commons-lang3:3.12.0'
got from library website in the gradle short tab
in the build.gradle(project), look for maven repo.
Once done, you'll be able to import the respective libraries.

Gradle: app does not see code imported from second level of submodule

I have a problem with transitive dependencies in my project.
There is an app (main) module which includes gradle library module A, which includes module B.
The problem is that app does not see classes inside module B.
Module A of course see classes from B, but app does not.
Dependencies should be transitive by default, but nothing happens.
So app gradle file has:
implementation project(path: ':A')
Module A has:
implementation project(path: ':B')
If I add:
implementation project(path: ':B')
to the app gradle file, it works, but i must exclude this, since I will use product flavors and this does not works for me anymore.
Is there any workaround for this issue?
If you use implementation, the classes won't be accessible from the modules which use it.
In order to expose the classes from sub-modules, you should replace implementation with api
In module 'A', use:
api project(':B')
In module 'app', use:
implementation project(':A')

Exclude Package From Gradle Dependency

I'm experiencing an issue where multiple versions of the same class are showing up in my classpath. The class in question is javax.ws.rs.core.UriBuilder. The version I want to use is brought in by javax.ws.rs:javax.ws.rs-api:2.0.1. However, we also use the Jira rest client library which has a dependency on the older version of jersey (com.sun.jersey:jersey-core) which has included the java.ws packages bundled in it's jar.
Here is an example snippet from the build file:
dependencies {
compile 'com.atlassian.jira:jira-rest-java-client-core:2.0.0-m31'
compile 'javax.ws.rs:javax.ws.rs-api:2.0.1'
compile 'org.glassfish.jersey.core:jersey-client:2.17'
}
I can't remove com.sun.jersey:jersey-core as it uses different package name from the new version and would cause class def not found exceptions in the Jira client.
As far as I can tell, my options at this point are:
Revert to using Jersey 1.x and it's implementation of jsr311
Somehow have gradle exclude the javax.ws package from the old jersey client.
I'd like to keep using the newer version of jersey so #2 would be my ideal solution but I'm not sure if it's even possible. Does anyone know how to go about this? If that's not possible, I'm open to other suggestions.
You can exclude an transitive dependency module like this:
compile ('org.glassfish.jersey.core:jersey-client:2.17') {
exclude group: 'javax.ws.rs'
exclude module: 'javax.ws.rs-api'
}
ref: 50.4.7 here
I found out that com.sun.jersey:jersey-core:1.19 doesn't bundle the javax.ws.rs class files and instead lists them as a compile-time dependency. Adding this snippet to my build.gradle fixed the issue.
configurations.all {
resolutionStrategy {
// For a version that doesn't package javax.ws
force 'com.sun.jersey:jersey-core:1.19'
}
}
exclude the group and module as below.
Ex :
implementation('org.apache.httpcomponents:httpclient:4.5.13') {
exclude group: 'commons-codec', module: 'commons-codec'
}

Configuring Gradle project to depend on lwjgl

How do I configure build.gradle to depend on LWJGL?
I'm new to Gradle, and how to configure library dependencies is clear as mud to me.
It's my understanding is that one can specify library dependencies for Gradle to download rather than checking them in to source control, but any sort of help with configuring things would be appreciated.
(I don't know any Ivy or Maven.)
I think what you want is to have lwjgl in your build classpath and resolve it automatically right?
try this snippet:
plugins {
id "java"
}
repositories{
maven {
url = "http://adterrasperaspera.com/lwjgl"
}
}
dependencies{
implementation "org.lwjgl:lwjgl:2.6"
implementation "org.lwjgl:lwjgl-util:2.6"
}
This snippet above defines a maven repository which contains the lwjgl libs and defines two compile dependencies to your project.
regards,
René

Categories

Resources