I'm using Dagger 2 in my Android project and I'm having trouble debugging it. I know that the compilation fails because of an error in my dagger 2 setup (had it before) but it's almost impossible to track it down because I don't get a proper error message telling me where the problem lies. All I get are messages that show that the annotation processing failed. Along the lines of:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Error:(14, 28) error: cannot find symbol class BR
Error:(17, 40) error: package com.some.package.databinding does not exist
Error:(17, 51) error: cannot find symbol class DaggerSomeComponent
...
Maybe it's somehow related to the fact that I'm also using databinding!?
I'm using Dagger 2.5, Gradle plugin 2.1.2 and android-apt 1.8.
Thanks for your help!
Java
javac by default will only show up to 100 errors. You are probably over this limit because of databinding reporting an error for each binding class it generates.
Add this to your apps's build.gradle:
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xmaxerrs" << "500"
}
}
Kotlin
You can enable the same javac option when using kapt by adding the following to your build.gradle.
kapt {
javacOptions {
option("-Xmaxerrs", 500)
}
}
This is currently ignored, but will be fixed in Kotlin v1.2.20.
Related
Let's start with an issue:
> Task :app:kaptAppDebugUnitTestKotlin FAILED
/app/build/tmp/kapt3/stubs/appDebugUnitTest/com/pckg/TestAppComponent.java:77: error: [ComponentProcessor:MiscError] dagger.internal.codegen.ComponentProcessor was unable to process this class because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public abstract class TestAppComponent extends com.pckg.AppComponent {
^warning: The following options were not recognized by any processor: '[room.schemaLocation, kapt.kotlin.generated, room.incremental]'[WARN] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: android.databinding.annotationprocessor.ProcessDataBinding (DYNAMIC).
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:kaptAppDebugUnitTestKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
> java.lang.reflect.InvocationTargetException (no error message)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
I'm trying to update our huge project to run with incremental kapt. One of the essentials was to update dagger. I tried a lot version, but the last one working is 2.20, everything above gives the mentioned 'error'.
To be honest, I don't see any error. The build works fine, when I assemble only app, but when I try to run the UnitTest task, it shows me that error. But I'm unable to find any issue, nor the AS code inspections in AppComponent. The TestAppComponent is not even generated.
I believe, we use completely regular setup of local unit tests for android.
Setup:
/core/src/main/com/pckg/core/Provisions.kt
interface Provisions {
....
}
/app/src/main/com/pckg/AppComponent.kt
#Component(modules=[....])
#Singleton
interface AppComponent : Provisions {
....
}
/app/src/test/com/pckg/TestAppComponent.kt
#Component(modules=[....])
#Singleton
interface TestAppComponent : AppComponent {
....
}
I also tried to make the Components to be abstract classes instead of interface (because the error says that class extends the interface, but without luck - same issue, just with abstract classes).
Of course I did try to run with --stacktrace, but it's just longer pointless exception.
Questions:
Do you know what needs to be changed when updating to dagger 2.21 and
above?
Do you know how to force dagger/kapt/gradle to say more than
(no error message)?
PS: Whatever library version you might think of is the latest. AGP 3.5.0, Gradle 5.5.1, Kotlin 1.2.50,...
I remember seeing something like this when I was trying to add the test component to the android tests. After adding the following to build.gradle the component was generated (originally I only had dagger in the kapt directive):
kaptAndroidTest "com.google.dagger:dagger-android-processor:$dagger_version"
kaptAndroidTest "com.google.dagger:dagger-compiler:$dagger_version"
If you're doing that for unit tests, adjust accordingly.
Note: If you're only seeing it when using incremental processing, then maybe it's some other configuration option you need to enable specifically for the unit test?
Turns out, the problem was in missing dependencies in tests.
In our case it was due to conflict with FindBugs.
We have defined FB as:
compileOnly "com.google.code.findbugs:annotations:3.0.1"
compileOnly "com.google.code.findbugs:jsr305:3.0.2"
So if we have a class with suppress:
public class JavaClass {
#Inject
#SuppressFBWarnings(value = "THIS_CRASHES_DAGGER",
justification = "Since this annotation is not available in test classpath, dagger will fail.")
Context mInjectedContext;
}
The #SuppressFBWarnings is not available in tests. This was ok up to dagger 2.20. But every version after is failing on that, because the annotation cannot be resolved. And dagger is trying to read other annotations to report you badly used annotations, etc.
The fix is easy:
testCompileOnly "com.google.code.findbugs:annotations:3.0.1"
testCompileOnly "com.google.code.findbugs:jsr305:3.0.2"
or making it implementation might ensure propagation to the tests also.
You can find more about this here: https://github.com/google/dagger/issues/1599
Is there any configuration to turn this warnings in errors when you're running gradlew lint check?
The example message is like this:
w: warning: viewModel.progressVisibility.getValue() is a boxed field
but needs to be un-boxed to execute android:visibility.
Based on the answer https://stackoverflow.com/a/54321834/6100078, you need to add this line on the application gradle:
android {
...
kapt {
javacOptions {
option("-Xmaxerrs", 1000)
option("-Werror")
}
}
This will make any build warning be reported as error.
I get this error when I type ./gradlew test in the command line:
e: java.util.ServiceConfigurationError: javax.annotation.processing.Processor: android.databinding.annotationprocessor.ProcessDataBinding Unable to get public no-arg constructor
at java.base/java.util.ServiceLoader.fail(ServiceLoader.java:581)
at ...
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:466)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:566)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
... 59 more
> Task :app:kaptDebugKotlin FAILED
FAILURE: Build failed with an exception.
It occurs also when I have kapt "androidx.databinding:databinding-compiler:$gradlePluginVersion" (both for 3.2.0 and 3.3.0-alpha13 versions) in build.gradle. I don't have other kapt dependencies. I have data binding enabled, it works and I can run tests via Android Studio (gradle task testDebugUnitTest works). I use embedded JDK.
I fixed this problem by using Java 8 instead of Java 11. The same problem may appear with Java 9 and 10. I set my JAVA_HOME via export JAVA_HOME=/path/to/java/8 and then ran ./gradlew build. And voila, no error.
I had the exception e: java.util.ServiceConfigurationError: javax.annotation.processing.Processor: android.databinding.annotationprocessor.ProcessDataBinding Unable to get public no-arg constructor and java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
when I ran the build from the command line using gradle. But these didn't appear when I run the build from Android Studio. I suspect this is because Android Studio is somehow using its own version of Java.
If you need to use databinding on Java 9+, you'll need to add JAX-B dependencies. Add this to your build.gradle's dependencies {} block:
// Add missing dependencies for JDK 9+
if (JavaVersion.current().ordinal() >= JavaVersion.VERSION_1_9.ordinal()) {
// Add both if you're using both Kotlin and Java
// If you're using Kotlin
kapt "com.sun.xml.bind:jaxb-core:2.3.0.1"
kapt "javax.xml.bind:jaxb-api:2.3.1"
kapt "com.sun.xml.bind:jaxb-impl:2.3.2"
// If you're using Java
annotationProcessor "com.sun.xml.bind:jaxb-core:2.3.0.1"
annotationProcessor "javax.xml.bind:jaxb-api:2.3.1"
}
Its because of Java version > 8
Point your JAVA_HOME to Java 8 version, it will work smoothly
Java 8 Download link
Details solution is here
java.util.ServiceConfigurationError: javax.annotation.processing.Processor: android.databinding.annotationprocessor.ProcessDataBinding Unable to get public no-arg constructor
I was getting this error because my 'build.gradle' , 'kotlin_version' and 'gradle wrapper' were not upto date.
ext.kotlin_version = '1.3.10' >> '1.3.20'
build:gradle:3.5.2 >> build:gradle:4.2.1
distributions/gradle-5.4.1-all.zip >> distributions/gradle-6.7.1-all.zip
I have downloaded google iosched repository and followed the build instructions but when I run
./gradlew clean assembleDebug
I get this error :
Information:Gradle tasks [clean, :apk:generateMapEditorDebugSources, :apk:generateMapEditorDebugAndroidTestSources, :apk:mockableAndroidJar, :apk:prepareMapEditorDebugUnitTestDependencies, :lib:generateMapEditorDebugSources, :lib:generateMapEditorDebugAndroidTestSources, :lib:mockableAndroidJar, :lib:prepareMapEditorDebugUnitTestDependencies]
Warning:[options] bootstrap class path not set in conjunction with -source 1.7
/home/mhmd/android_work/iosched/server/src/main/java/com/google/samples/apps/iosched/server/gcm/FcmRegistrationEndpoint.java
Error:(21, 63) error: package com.google.api.client.repackaged.com.google.common.base does not exist
/home/mhmd/android_work/iosched/server/src/main/java/com/google/samples/apps/iosched/server/registration/RegistrationEndpoint.java
Warning:(41, 52) Bool is internal proprietary API and may be removed in a future release
Warning:(41, 52) Bool is internal proprietary API and may be removed in a future release
Error:Execution failed for task ':server:compileJava'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 7.497 secs
Information:2 errors
Information:3 warnings
Information:See complete output in console
the line of error :
Error:(21, 63) error: package com.google.api.client.repackaged.com.google.common.base does not exist
note : I haven't done any changes to the project yet. I simply followed these instructions.
Forget about the build instructions from their github page. When you import the code, don't upgrade the gradle version when android studio gives you the popup. This project's gradle version is 2.3.1. Keep it that way. Don't upgrade to 3 or 4.
Make the following changes in the build.gradle file for the server module:
add this dependency:
compile group: 'com.google.api.client', name: 'google-api-client-repackaged-com-google-common-base', version: '1.2.3-alpha'
update this dependency to version 1.23.0:
compile 'com.google.api-client:google-api-client-appengine:1.23.0'
add googleClientVersion in endpoints:
appengine {
// ...
endpoints {
googleClientVersion = '1.23.0'
// ...
}
}
No more changes to the build.gradle file. Now replace the following import
import com.google.api.client.repackaged.com.google.common.base.Strings;
with
import com.google.appengine.repackaged.com.google.common.base.Strings;
in the file com.google.samples.apps.iosched.server.gcm.FcmRegistrationEndpoint.java in server module.
Update the api_keys from your Google Developer project.
Now build and run!
Edit
Related github issues:
#246
#241
Error log:
warning: [options] bootstrap class path not set in conjunction with -source 1.7
/Users/samuelyang/AndroidStudioProjects/iosched-master/server/build/generated-source/endpoints/java/com/google/samples/apps/iosched/rpc/cms/Cms.java:346:
error: method does not override or implement a method from a supertype
#override
^
How do i build it?
The Error is occurred a file named Cms.java, and the code snipe of Cms.java as below:
#Override
public Builder setBatchPath(String batchPath) {
return (Builder) super.setBatchPath(batchPath);
}
Every time i comment all these error code snipe and build the project, all these code will be auto-gen ,i don't know how to handle this...
Could anyone help me? thanks...
Download Google I/O 2017 Android App here
I had the same problem with other project.
Try to change google-api-client libraries from 1.22.0 to 1.23.0 in build.gradle
compile 'com.google.api-client:google-api-client:1.23.0'
compile 'com.google.http-client:google-http-client-android:1.23.0'