Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger 2 Build IllegalArgumentException compileDebugJavaWithJavac

I have been testing out Dagger 2, and everything had been working, until I did a bit of refactoring. Now gradle is throwing an IllegalArgumentException, and I cannot figure out what I changed that is now causing the error. I haven't made any changes to the gradle file, and this seems to be the brunt of the stack trace:

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':mobile:compileDebugJavaWithJavac'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
    ...

Caused by: java.lang.IllegalArgumentException
    at com.google.common.base.Preconditions.checkArgument(Preconditions.java:108)
    at dagger.internal.codegen.writer.ClassName.peerNamed(ClassName.java:130)
    at dagger.internal.codegen.SourceFiles.membersInjectorNameForMembersInjectionBinding(SourceFiles.java:266)
    at dagger.internal.codegen.InjectBindingRegistry.registerBinding(InjectBindingRegistry.java:194)
    at dagger.internal.codegen.InjectBindingRegistry.registerBinding(InjectBindingRegistry.java:171)
    at dagger.internal.codegen.InjectProcessingStep.process(InjectProcessingStep.java:129)
    at dagger.shaded.auto.common.BasicAnnotationProcessor.process(BasicAnnotationProcessor.java:228)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:794)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:705)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$1800(JavacProcessingEnvironment.java:91)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1035)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1176)
    at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1170)
    at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:856)
    at com.sun.tools.javac.main.Main.compile(Main.java:523)
    ... 89 more

No files are being generated by Dagger as well, and they were previously. I have been trying every method to fix this that I can find, mostly involving fixing the gradle files or clearing out the build folder, but so far nothing has worked.


Quick update (since I noticed a few up-votes); I never did find out what I did wrong, I ended up reverting to an old build. After the revert, I did the refactoring again and it worked fine. I must've done something different when I initially refactored the code, but I have no idea what it was.

If anyone has an idea of what could have caused this, I'm sure it will help out anyone else who has, or will in the future, run into this issue.

like image 571
Bryan Avatar asked Feb 25 '16 14:02

Bryan


1 Answers

I ran into this issue while bringing Firebase into the project. It was the first background service being added to the project so I decided to do some sleuthing with a service that did nothing.

This built:

public class HopefullyBuildsService extends IntentService {
    public HopefullyBuildsService(String name) {
         super(name);
    }

     @Override
     protected void onHandleIntent(Intent intent) {

     }
}

..............

@ApplicationScoped
@Component(modules = {ApplicationModule.class, RestModule.class})
public interface ApplicationComponent {
    ...
    void inject(HopefullyBuildsService service);
    ...
}

But this caused the build to fail:

public class HopefullyBuildsService extends FirebaseMessagingService {
}

..............

@ApplicationScoped
@Component(modules = {ApplicationModule.class, RestModule.class})
public interface ApplicationComponent {
    ...
    void inject(HopefullyBuildsService service);
    ...
}

For whatever reason trying to inject directly into a Firebase derived service causes the build to fail in the way you described. However indirectly injecting into another class and then instantiating it the old-fashioned way inside the service allowed it to build again.

public class FirebaseDaggerInjectHelper {

    @Inject
    PersistManager persistManager;

    @Inject
    RestClient restClient;

    @Inject
    OtherClasses stuffEtc;

    public FirebaseDaggerInjectHelper(MyApplication application){
        application.getApplicationComponent().inject(this);
   }

    //getters and setters n stuff
}

........

@ApplicationScoped
@Component(modules = {ApplicationModule.class, RestModule.class})
public interface ApplicationComponent {
    ...
    void inject(FirebaseDaggerInjectHelper helper);
    ...
}

........

public class HopefullyBuildsService extends FirebaseMessagingService {
    private FirebaseDaggerInjectHelper injectHelper;

    @Override
    public void onCreate() {
        super.onCreate();
        injectHelper = new FirebaseDaggerInjectHelper((MyApplication) getApplicationContext());
}

And then it built fine. Admittedly, having this middleman class is annoying and the firebase derived service has to interact with the injected components in an indirect fashion. But its not clear to me why I cannot inject into a Firebase derived service, Or what is special about Firebase that made Dagger2 unhappy.

like image 170
Kabliz Avatar answered Nov 02 '22 01:11

Kabliz