简体   繁体   English

Dagger 2 Build IllegalArgumentException compileDebugJavaWithJavac

[英]Dagger 2 Build IllegalArgumentException compileDebugJavaWithJavac

I have been testing out Dagger 2, and everything had been working, until I did a bit of refactoring. 我一直在测试Dagger 2,一切都在工作,直到我做了一些重构。 Now gradle is throwing an IllegalArgumentException , and I cannot figure out what I changed that is now causing the error. 现在gradle抛出一个IllegalArgumentException ,我无法弄清楚我改变了什么现在导致错误。 I haven't made any changes to the gradle file, and this seems to be the brunt of the stack trace: 我没有对gradle文件进行任何更改,这似乎是堆栈跟踪的主要内容:

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. Dagger也没有生成任何文件,而且之前也是如此。 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. 我一直在尝试解决这个问题的每个方法,我可以找到,主要涉及修复gradle文件或清除build文件夹,但到目前为止还没有任何工作。


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. 如果有人知道可能导致这种情况的原因,我相信它会帮助那些已经或将来会遇到这个问题的人。

I ran into this issue while bringing Firebase into the project. 我在将Firebase引入项目时遇到了这个问题。 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. 无论出于何种原因,尝试直接注入Firebase派生服务会导致构建以您描述的方式失败。 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. 不可否认,让这个中间人类很烦人,而firebase派生的服务必须以间接的方式与注入的组件进行交互。 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. 但是我不清楚为什么我不能注入Firebase衍生服务,或者Firebase的特殊之处让Dagger2不满意。

This is not yet solved in dependency dagger2.0 still throws IllegalArgumentException , I agree with @KATHYxx's approach to solve it in dagger2.0 But square has solved the inject in dagger2.7 version. 这还没有解决依赖dagger2.0仍然抛出IllegalArgumentException ,我同意@KATHYxx在dagger2.0解决它的方法但是square已经解决了dagger2.7版本中的inject问题。 So, updating the dependency fixed the issue 因此,更新依赖项修复了问题

implementation "com.google.dagger:dagger:2.7"
apt "com.google.dagger:dagger-compiler:2.7"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Dagger2构建AndroidStudio:java.lang.IllegalArgumentException:...不能表示为类<?> - AndroidStudio build with Dagger2: java.lang.IllegalArgumentException: … cannot be represented as a Class<?> 离子构建android:CordovaLib:compileDebugJavaWithJavac - ionic build android :CordovaLib:compileDebugJavaWithJavac 科尔多瓦建造|运行失败 - compileDebugJavaWithJavac - Cordova build | Run failed - compileDebugJavaWithJavac 匕首:IllegalArgumentException:没有注射器工厂绑定 Class - Dagger: IllegalArgumentException: No injector factory bound for Class Dagger 1.x @Inject引发IllegalArgumentException - Dagger 1.x @Inject throws an IllegalArgumentException Dagger构建错误 - Dagger build error 任务“:app:compileDebugJavaWithJavac”的应用程序未构建执行失败 - app not build for Execution failed for task ':app:compileDebugJavaWithJavac' 构建时出现匕首异常。 错误:任务&#39;:app:compileDebugJavaWithJavac&#39;的执行失败。 java.lang.IllegalStateException - Dagger exception when building. Error:Execution failed for task ':app:compileDebugJavaWithJavac'. java.lang.IllegalStateException 使用匕首刀柄 + 解析服务器 SDK: java.lang.IllegalArgumentException - Using dagger hilt + parse Server SDK: java.lang.IllegalArgumentException 构建项目时崩溃匕首 - Crash dagger when build project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM