Dagger 2 Build IllegalArgumentException compileDebugJavaWithJavac [英] Dagger 2 Build IllegalArgumentException compileDebugJavaWithJavac

查看:56
本文介绍了Dagger 2 Build IllegalArgumentException compileDebugJavaWithJavac的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在测试Dagger 2,并且一切正常,直到我做了一些重构。现在gradle抛出了 IllegalArgumentException ,而且我无法弄清楚我更改了什么导致了错误。我没有对gradle文件进行任何更改,这似乎是堆栈跟踪的首当其冲:

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

Dagger也没有生成任何文件,而以前是。我一直在尝试找到所有可以解决的方法,主要涉及修复gradle文件或清除build文件夹,但到目前为止没有任何效果。

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.

推荐答案

我在将Firebase引入项目时遇到了这个问题。这是第一个添加到项目中的后台服务,因此我决定使用一项什么都不做的服务进行侦查。

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.

此构建项:

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);
    ...
}

但这会导致构建失败:

public class HopefullyBuildsService extends FirebaseMessagingService {
}

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

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

无论出于何种原因试图直接注入到Firebase派生的服务会导致构建以您描述的方式失败。但是,间接注入另一个类,然后实例化该类以使其在服务内部得以实现,从而使其得以再次构建。

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());
}

然后构建良好。诚然,拥有这种中间人类很烦人,并且Firebase派生的服务必须以间接方式与注入的组件进行交互。但是我不清楚为什么我不能注入Firebase派生的服务,或者Firebase的特殊之处使Dagger2不满意。

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.

这篇关于Dagger 2 Build IllegalArgumentException compileDebugJavaWithJavac的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆