Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger 2 inject error in AppCompatActivity

I'm newbie for Dagger. Current I create sample project some snip code:

MyComponent.java

@PerActivity
@Component(modules = MyModule.class)
public interface MyComponent {
    void inject(TutorialActivity activity);
}

MyModule.java

@Module
public class MyModule {
    @Provides
    Position providePosition() {
        return new Position();
    }
}

PerActivity.java

@Scope
@Retention(RUNTIME)
public @interface PerActivity {}

TutorialActivity.java

public class TutorialActivity extends AppCompatActivity{}

When compile project I get error:

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> java.lang.IllegalArgumentException: expected one element but was: <android.support.v4.app.FragmentActivity, android.support.v4.app.TaskStackBuilder.SupportParentable>

So if I change TutorialActivity as:

public class TutorialActivity extends Activity{}

or even

public class TutorialActivity{} // Without extends

Then it will working normally.(I can see class generated by Dagger2).

Please help !

Thanks.

UPDATE

My project structure:

  • common module.
  • app module. (app module will use common module as depended in gradle).

In both build.gradle (common and app module) I added:

apt "com.google.dagger:dagger-compiler:${daggerVersion}"
compile "com.google.dagger:dagger:${daggerVersion}"

In build.gradle at common module:

provide "org.glassfish:javax.annotation:${javaxAnnotationVersion}" 

An error only occurs if I have 2 module. (module app depended on common). If I move my Component/Module to module common -> It work. But when I move that to app module -> Error when compile.

like image 519
quangson91 Avatar asked Feb 16 '16 05:02

quangson91


1 Answers

I'm not sure that your issue is a problem with Dagger because I don't see you requesting any dependencies in your Android components.

Nonetheless you need this in your build.gradle to use the depdendency injection annotations.

 provided 'javax.annotation:jsr250-api:1.0'
like image 183
plast Avatar answered Oct 03 '22 20:10

plast