Cannot find symbol class "Generated" for Dagger 2 - java

I just started doing dependency injection using Dagger 2. When I spun up my modules, components and tried to build my application, gradle threw the error
Error:(4, 24) error: cannot find symbol class Generated
I dug into it and found that the error is in one of the classes Dagger generates to do DI. The particular class that's missing was javax.annotation.Generated and the line throwing the error is the line that anntotates a Dagger generated class as #Generated("dagger.internal.codegen.ComponentProcessor")
This question helped in finding the solution which is to add the javax package as a dependency by adding the line compile 'org.glassfish:javax.annotation:10.0-b28' to my gradle build file. This led to a successful build.
My question is, why is that not added as a transitive dependency for Dagger or why hasn't anyone else faced this particular issue (I assume so, since I couldn't find any question here regarding this?

TL;DR use Dagger >= 2.1
Alex is right, but it's better to add JSR250 dependency instead of GlassFish
provided 'javax.annotation:jsr250-api:1.0'
or for the latest gradle plugin:
compileOnly 'javax.annotation:jsr250-api:1.0'

Read this for more info: https://github.com/google/dagger/issues/95
Basically, the solution is to do what you've already done which is include the glassfish javax annotation library.

This happens if your JAVA_HOME points to JAVA version 9 or 10. Switching JAVA_HOME to Java 8 fixes the problem and you will not need that extra dependency.

I downgraded my JVM to Java 8 and running gradle build was successful in my Android application using Dagger 2.

The right answer today is to use a version of dagger which is greater than 2.1 (because of the fix mentioned by #tomrozb is integrated in 2.1)

Related

How to add com.fasterxml.jackson.core.exc.InputCoercionException as a dependency in gradle?

I am getting java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/PropertyNamingStrategies which is used in another project. I have included jackson jar in current gradle project as well. But while starting the project I am getting the above mentioned error. Seems like we need to add com.fasterxml.jackson.core.exc.InputCoercionException as an dependency but I am not able to understand where to add this as a dependency ? Can someone please help ?
java.lang.NoClassDefFoundError Either means - missing dependency with class com/fasterxml/jackson/databind/PropertyNamingStrategies or class was removed meaning jackson libs versions used in your project dependencies won't work together.
How to start solving problems like those.
1, Via IDE try to find missing class if is present. If is not present then try to find jar with missing class on internet and add as dependency. In case your IDE show class is present then problem may be with import scope. Scope management differ per used technology so provide detail which one you use or paste dependencies from build.kts . Make sure you use implementation in case you import this class in project and not runtimeOnly.
2, You found class then try to print project dependency tree command differ per used technology. For gradle ./gradlew dependencies or for submodule ./gradlew submoduleName:dependencies and look at versions of jackson in your project.
3, Check jackson lib with version listed via dependency tree contains missing class.
How to avoid problem like those with spring boot.
I would recoment to use BOM provided by spring boot project, versions in there should work together.
For gradle with kotlin DSL we import it like this
import org.springframework.boot.gradle.plugin.SpringBootPlugin
plugins {
id("org.springframework.boot") version "2.6.2"
}
dependencies {
val springBootPlatform = platform(SpringBootPlugin.BOM_COORDINATES)
annotationProcessor(springBootPlatform)
implementation(springBootPlatform)
//this version has to be searched for spring boot version
implementation(platform("org.springframework.cloud:spring-cloud-dependencies:2021.0.0"))
//put desired jackson dependencies
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
}

Android compilation error after upgrading to 'com.google.android.gms:play-services-ads:18.1.0'

I am upgrading my play-services-ads library from version 12 to version 18.1:
dependencies {
api 'com.google.android.gms:play-services-ads:18.1.0'
}
The problem is that the compilation fails with this error:
.gradle/caches/transforms-1/files-1.1/play-services-ads-identifier-17.0.0.aar/75b3c9fbdc51199269673bd2fa8b6cfe/jars/classes.jar(com/google/android/gms/ads/identifier/AdvertisingIdClient.class): warning: Cannot find annotation method 'value()' in type 'GuardedBy': class file for javax.annotation.concurrent.GuardedBy not found
I took away all the usages for AdvertisingIdClient and left only the import, but the problem persists:
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
Is there anything I am doing wrong?
com.google.android.gms:play-services-ads:18.1.0 depends upon com.google.android.gms:play-services-ads-identifier:17.0.0, among other libraries.
Your error indicates that com.google.android.gms:play-services-ads-identifier:17.0.0 references javax.annotation.concurrent.GuardedBy. However, that class is not in the Android SDK. The POM file for com.google.android.gms:play-services-ads-identifier:17.0.0 should be referencing a library that has an implementation of that class, but it does not seem to.
One library that has an implementation of that class is com.google.code.findbugs:jsr305. Adding a dependency on com.google.code.findbugs:jsr305 for a recent version (e.g., 3.0.2) gave you that class, satisfying the compiler.
So, there appears to be a bug in the Play Services SDK packaging, which my workaround resolves. You might want to add a comment in your module's build.gradle file to consider removing the com.google.code.findbugs:jsr305 if a future update to com.google.android.gms:play-services-ads fixes this bug.

How to fix undefined MqttChannelInitializer constructor in HiveMQ Client?

I was using HiveMQ Client version 1.0.1 but I decided to update to the recently released version 1.1. I completely started from scratch and imported the project as a Gradle project and tried to build. The build work only after ignoring a few failed tests. I'm getting 3 errors in 3 different classes. I realize this is likely related to the Dagger dependency injection tool and I had already successfully built the project and added the directory of build/generated/source/apt/main/ to my build path as noted by my previous stack post where I had issues with a DaggerSingletonComponent not being found: How to fix DaggerSingletonComponent not resolved in HiveMQ (MQTT protocol) . This seems to be a new issue and I'm not sure what's wrong. I tried rebuilding by project but the errors still persist. I've left some screenshot below as well as the specific errors.
HiveMQ:
https://github.com/hivemq/hivemq-community-edition
https://github.com/hivemq/hivemq-mqtt-client
Errors:
The constructor MqttChannelInitializer(MqttClientConfig, MqttConnAckFlow, MqttEncoder, MqttConnectHandler, MqttDisconnectHandler, MqttAuthHandler, Lazy) is undefined
The constructor MqttSession(MqttClientConfig, MqttSubscriptionHandler, MqttIncomingQosHandler, MqttOutgoingQosHandler) is undefined
The method provideBootstrap(NettyEventLoopProvider, MqttChannelInitializer) in the type ConnectionModule is not applicable for the arguments (MqttClientConfig, NettyEventLoopProvider, MqttChannelInitializer)
Screenshots:
Executing ./gradlew clean build on the command line will fix your error.
But I also think that the real solution for your use case is to create a new empty project (gradle or maven) and add the client library as a dependency, like described here: https://hivemq.github.io/hivemq-mqtt-client/docs/installation.html
The issue turned out to be caused by an issue with the source folder in the directory build/generated/source/apt/main/ not having the option “Update exclusion filters in other source folders to solve nesting” selected. Selecting that option solved all of the errors.

Why does Gradle resolve a package with wrong version number?

I am building a web application and I am using Dropwizard 1.3.0, which has a dependency on jetty-io 9.4.8. This dependency has conflicts with another package (dropwizard-websocket-jee7-bundle 2.0.0), because it seem to fetch the wrong version number.
I looked into tha package, and found the method that has been renamed in 9.4.x - AbstractWebSocketConnection.java from 9.3.x - AbstractWebSocketConnection.java. The issue is that even though in Gradle the dependency tree shows I fetched 9.4.8 (the new one which I need), I still get the older, 9.3.x java file which causes the conflicts. I tried to Invalidate Caches / Restart and rebuild the whole project, but I seem to get the outdated file all the time.
What are the possible solutions for this?
If your bad class are imported by a transitive dependency, try to exclude explicit the transitive dependency.
For example if your required library is 'my.group:requiredLibrary:2.0.0' and there are another version in 'my.group:someDependency:0.1.5' you can do like this:
dependencies{
compile 'my.group:requiredLibrary:2.0.0'
compile ('my.group:someDependency:0.1.5'){
exclude group: 'my.group' module:'requiredLibrary'
}
}
Try forcing a particular version in your build.gradle
Example here: https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html

Cannot migrate my java code to Elasticsearch 2.4 (Java API)

I recently migrated to Elasticsearch 2.4 in my Java code. I am using the following external libs in my gradle file:
'spring-data-mongodb': 'org.springframework.data:spring-data-mongodb:1.6.0.RELEASE',
'spring-data-rest': 'org.springframework.data:spring-data-rest-webmvc:2.2.0.RELEASE',
'spring-webmvc': 'org.springframework:spring-webmvc:4.0.7.RELEASE',
'elasticsearch-client': 'org.elasticsearch:elasticsearch:2.4.0',
'spring-context': 'org.springframework:spring-context:4.0.7.RELEASE',
'jackson': 'com.fasterxml.jackson.core:jackson-databind:2.8.1',
'commons-io': 'commons-io:commons-io:2.4',
'commons-codec': 'commons-codec:commons-codec:1.8',
'commons-httpclient': 'org.apache.httpcomponents:httpclient:4.3.6',
'commons-lang': 'org.apache.commons:commons-lang3:3.3.2',
'commons-collections': 'org.apache.commons:commons-collections4:4.0',
'commons-cli': 'commons-cli:commons-cli:1.3',
'commons-csv': 'org.apache.commons:commons-csv:1.1',
'joda-time': 'joda-time:joda-time:2.3',
'spring-reactor': 'org.projectreactor.spring:reactor-spring-context:1.1.3.RELEASE',
'json-smart': 'net.minidev:json-smart:1.3.1',
'mongeez': 'org.mongeez:mongeez:0.9.3',
'mongo-driver': 'org.mongodb:mongo-java-driver:2.12.3',
'akka-actor': 'com.typesafe.akka:akka-actor_2.10:2.3.13',
'scala-library': 'org.scala-lang:scala-library:2.10.4
I am using mockmvc to test my project. The error is occurring in MockMvc.java class and is the following:
Could not instantiate bean class:
[org.springframework.hateoas.hal.Jackson2HalModule$HalLinkListDeserializer]: Constructor threw exception
com.fasterxml.jackson.databind.deser.std.ContainerDeserializerBase: method (Ljava/lang/Class;)V not found
I think that this is is problem related to Jackson dependencies but I am not sure.
Yes, it does sound like an incompatibility between Jackson version that one of Spring modules was compiled against (and expected to use with), and 2.8.
It may be necessary to try a more recent version of Spring (MVC?).
Note that you also will want to use Jackson 2.8.3 instead of 2.8.1, due to various bug fixes, although it probably does not matter wrt this particular issue.
Thanks #StaxMan this issue was fixed by adding the following configuration in plugin-security.policy file location on C:\elasticsearch-2.4.1\modules\lang-groovy:
permission org.elasticsearch.script.ClassPermission "java.lang.*";
permission org.elasticsearch.script.ClassPermission "java.text.*";

Categories

Resources