New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dagger 2 swallows errors #306

Closed
charbgr opened this Issue Feb 3, 2016 · 25 comments

Comments

Projects
None yet
@charbgr
Copy link

charbgr commented Feb 3, 2016

Hi, i have an unexpected problem with my setup. Also to inform you, i heavily use the official android databinding in my project (in 10+ activities and 20+ fragments).

my app build.gradle is something like this:

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.2'

    dataBinding {
        enabled = true
    }

    dexOptions {
        incremental true
        javaMaxHeapSize "4g"
    }

    defaultConfig {
        applicationId "com.charbgr"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1"

        //Espresso
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        //RenderScript
        renderscriptTargetApi 19
        renderscriptSupportModeEnabled true

        multiDexEnabled true
    }

    packagingOptions {

        //Espresso excludes
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
        exclude 'LICENSE.txt'
        //Espresso excludes

    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

}



dependencies {

    ...

    compile 'com.google.dagger:dagger:2.0.2'
    apt 'com.google.dagger:dagger-compiler:2.0.2'
    provided 'org.glassfish:javax.annotation:10.0-b28'
}

the root build.gradle is this one:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0-alpha5'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

In my code, i make an enforced error(i know that i cannot create an object with the same method name in a module). The enforced error looks like this:

@Module
public class AndroidModule {

    @Provides
    public Foo providesFoo(String bar) {
        return new Foo(bar);
    }

    @Provides
    public Foo providesFoo(String bar, String unusedArgument) {
        return new Foo(bar);
    }
}

For some reason, when i build my project i get 100+ errors from databinding with this output:
cannot find symbol class BR

and dagger doesn't prints this error
Cannot have more than one @Provides method with the same name in a single module

Seems that Dagger "swallows" this error or the databinding overrides some behaviour of the annotation processor tool(apt).

Do you know what's going on?

@ronshapiro

This comment has been minimized.

Copy link

ronshapiro commented Feb 3, 2016

Can you post a small sample app that exhibits this? I'm not sure when the data binding "compilation" occurs, but my guess is that when javac fails due to Dagger, the entire gradle build halts and therefore you get these errors. Similar errors occur when you have just Dagger errors and the DaggerFooComponet cannot be generated; javac doesn't know how to handle this and throws up its hands.

@charbgr

This comment has been minimized.

Copy link

charbgr commented Feb 3, 2016

Sadly, I can't reproduce it in a separate project. :(

Do you want a copy of the output of ./gradlew assembleDebug --debug ?

@charbgr

This comment has been minimized.

Copy link

charbgr commented Feb 5, 2016

After couple of days of debugging, i found that javac outputs by default max 100 errors.
So if anyone, come with this error just add to gradle to force javac to output over 100 errors.

gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xmaxerrs" << "500"
    }
}

so @ronshapiro you can close this issue.

@ronshapiro ronshapiro closed this Feb 5, 2016

@jmfayard

This comment has been minimized.

Copy link

jmfayard commented May 3, 2016

@ronshapiro @charbgr
my co-worker just ran into this issue. The fix above solved the problem, but in the meantime, it made her hate dagger and code-generation in general. 100 errors displayed on screen, and none having anything to do with something she did. So I would suggest to document it beter than in a comment of a closed issue

@jimmy0251

This comment has been minimized.

Copy link

jimmy0251 commented Jul 27, 2016

@charbgr You saved my life, can we document this somewhere?

@cgruber

This comment has been minimized.

Copy link

cgruber commented Jul 27, 2016

What errors were being displayed such that you had over 100
non-dagger-caused errors?

On Tue, 26 Jul 2016 at 22:38 Jignesh Sanghani notifications@github.com
wrote:

@charbgr https://github.com/charbgr You saved my life, can we document
this somewhere?


You are receiving this because you are subscribed to this thread.

Reply to this email directly, view it on GitHub
#306 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAUN4hfvIeHYjW_ky3KOpudSXiGZueGYks5qZu62gaJpZM4HSXDD
.

@jmfayard

This comment has been minimized.

Copy link

jmfayard commented Jul 27, 2016

@cgruber
it's the bad interaction with the android data-binding library generates a MyLayoutBinding class for each resource/layout/my_layout.xml, and myLayoutBinding.subViewXX for each android:id="@+id/subViewXX". If you have 20 layouts and 5 ids in each layout, you have very soon 120 generated symbols in your code that each cause a compilation error if not generated correctly.

@jimmy0251

This comment has been minimized.

Copy link

jimmy0251 commented Jul 27, 2016

If you're using data binding, android studio generates error for every layout file even though there's no error in them, which causes dagger errors to be swallowed.

@ronshapiro

This comment has been minimized.

Copy link

ronshapiro commented Dec 16, 2016

This seems to be a periodic question, I'm going to add some docs to it so that it's more clear.

@tasomaniac

This comment has been minimized.

Copy link

tasomaniac commented Jul 4, 2017

The mentioned fix actually didn't work for me with Gradle 4.0. It does not find the JavaCompile tasks. Here what I did instead (in my main build.gradle)

allprojects {
  afterEvaluate {
     tasks.withType(JavaCompile.class) {
       options.compilerArgs << "-Xmaxerrs" << "500"
     }
   }
 }
@sangeetsuresh1

This comment has been minimized.

Copy link

sangeetsuresh1 commented May 15, 2018

But for koltin it's showing databinding error in case of dagger related errors. Dagger related error is not showing in logs. Can anybody help me with this error

@tasomaniac

This comment has been minimized.

Copy link

tasomaniac commented May 15, 2018

@sangeetsuresh1 when annotation processor errors, the error output is mixed with errors coming from all annotation processors (including databinding). Increase the number of errors as mentioned above and search for possible dagger problems in that big list of errors.

There is not really a better way at the moment.

@sangeetsuresh1

This comment has been minimized.

Copy link

sangeetsuresh1 commented May 15, 2018

@tasomaniac I have found that new data binding library is causing the issue android.databinding.enableV2=true
If databinding v2 is disabled it's showing proper dagger error.

@breakline87

This comment has been minimized.

Copy link

breakline87 commented May 18, 2018

Can you guys tell us an update on this? Any time something is wrong I just give up and go with whats working. But it's hard to modularize when Dagger does not tell me whats wrong.

@liminal

This comment has been minimized.

Copy link

liminal commented May 23, 2018

@breakline87 it's not dagger swallowing the errors. It's the java compiler that by default cuts out after 100 errors. 100 errors is a lot normally but when you have a bunch of annotation processors like dagger and android databindings you easily pass that limit. It's not something that can (or probably even should) be fixed in dagger, the only thing you can do is increase the maximum number of displayed compilation errors as shown in multiple comments above (eg #306 (comment) or #306 (comment) )

@breakline87

This comment has been minimized.

Copy link

breakline87 commented May 25, 2018

Unfortunately, there is not always an appropriate error message. Several bugs in databinding can cause this (one example would be to use wrong package names in layout files).

Dagger fails when I try to inject into a private field, however either I dont see the error message (at least not anymore) or its just not there. Sometimes it takes up hours to find a bug because all I see is "class not found" and similar error messages everywhere. Even if I see the whole output in a big project that means thousands of lines I dont care about at all.

@tasomaniac

This comment has been minimized.

Copy link

tasomaniac commented May 25, 2018

An advice that I can give you is to go one step at a time when it comes to dagger. Don't change bunch of things and try to run some time later.

If you build each time after you do a dagger related change, then you will know what exactly broke it when it is broken. I know building also takes time but it will not take hours.

@sangeetsuresh1

This comment has been minimized.

Copy link

sangeetsuresh1 commented May 25, 2018

Why it is not showing errors for new android data binding compiler i.e v2. Have somebody found the root cause of errors not showing?

@vfarafonov

This comment has been minimized.

Copy link

vfarafonov commented Jul 17, 2018

Kotlin docs helped me:

kapt {
    javacOptions {
        // Increase the max count of errors from annotation processors.
        // Default is 100.
        option("-Xmaxerrs", 500)
    }
}

Of course this should be used if you use kapt

@benwicks

This comment has been minimized.

Copy link

benwicks commented Aug 2, 2018

@vfarafonov This isn't fixing the problem for us. Even after putting that configuration in our offending module and the app module, the build still fails with kapt 100 errors.

error: package javax.annotation.processing does not exist
import javax.annotation.processing.Generated;
@ronshapiro

This comment has been minimized.

Copy link

ronshapiro commented Aug 2, 2018

FWIW, @netdpb is working on limiting the amount of actual errors that Dagger prints out, and this should help greatly.

Specifically, for many types of errors, we used to print the error N times, where N was the number of entry points from a (sub)component where the error was presented. Initially that was due to an implementation detail, but now it's not necessary and it proves to not be so helpful. So we're likely going to pick 1 of the entry points, print information for that.

Hopefully that will make these types of problems less likely to come up.

@tbroyer

This comment has been minimized.

Copy link

tbroyer commented Aug 2, 2018

@benwicks With javac that would mean that you're using Java 10 (or 9) and didn't pass -source 8 or --release 8. There must be an equivalent with kapt.

@carlonzo

This comment has been minimized.

Copy link

carlonzo commented Sep 4, 2018

I've applied it to all my submodules with this in my root build.gradle:

subprojects {

    afterEvaluate {
        if (project.plugins.hasPlugin("kotlin-kapt")) {
            kapt {
                javacOptions {
                    option("-Xmaxerrs", 500)
                }
            }
        }
    }
}
@zoltish

This comment has been minimized.

Copy link

zoltish commented Sep 17, 2018

@carlonzo Only solution that actually works for me. Thanks for sharing!

@GorkemKarayel

This comment has been minimized.

Copy link

GorkemKarayel commented Oct 23, 2018

I've applied it to all my submodules with this in my root build.gradle:

subprojects {

    afterEvaluate {
        if (project.plugins.hasPlugin("kotlin-kapt")) {
            kapt {
                javacOptions {
                    option("-Xmaxerrs", 500)
                }
            }
        }
    }
}

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment