google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.45k stars 2.02k forks source link

NoSuchMethodError in Util.getAnnotation() #135

Closed sockeqwe closed 9 years ago

sockeqwe commented 9 years ago

Hi, I'm trying to use Dagger2 in an android application. Im using android-apt plugin for annotataion processing. Unfortunately I get the following error while building my app with gradle:

Caused by: java.lang.NoSuchMethodError: dagger.internal.codegen.Util.getAnnotation(Ljava/lang/Class;Ljavax/lang/model/element/Element;)Ljava/util/Map;
    at dagger.internal.codegen.ValidationProcessor.validateProvides(ValidationProcessor.java:80)
    at dagger.internal.codegen.ValidationProcessor.process(ValidationProcessor.java:69)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:793)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:722)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$1700(JavacProcessingEnvironment.java:97)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1029)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1163)
    at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1108)
    at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:824)
    at com.sun.tools.javac.main.Main.compile(Main.java:439)
    ... 65 more

I only have one module and one component so far:

@Module(
    library = true,
    complete = false)
public class HippoModule {

  @Provides @Singleton public OkHttpClient providesOkHttpClient() {
    OkHttpClient client = new OkHttpClient();
    client.networkInterceptors().add(new FoursquareInterceptor());
    return client;
  }

  @Provides @Singleton public RestAdapter providesRestAdapter(OkHttpClient client) {
    return new RestAdapter.Builder().setEndpoint("https://api.foursquare.com/v2")
        .setClient(new OkClient(client))
        .build();
  }

  @Provides @Singleton public EventBus providesEventBus() {
    return EventBus.getDefault();
  }

  @Provides @Singleton public FoursquareApi providesFoursquareApi(RestAdapter restAdapter) {
    return restAdapter.create(FoursquareApi.class);
  }
}
@Component(modules = HippoModule.class)
public interface ShakeComponent {

    public ShakeFragment injectFragment(ShakeFragment fragment);
    public ShakePresenter presenter();
}
public class ShakePresenter{

  @Inject FoursquareApi foursquare;
  ...
}
public class ShakeFragment extends Fragment 
      @Inject EventBus eventBus;

      public void onCreate(Bundle b){
            super.onCreate(b);
            // Here I want to inject
            // Dagger_ShakeComponent.create().injectFragment(this);
            // and create a ShakePresenter instance
            // but Dagger_ShakeComponent can not be generated
       }
      ...
}

I'm using java 7 on my mac book pro:

java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

and my gradle android config looks like this:

android {
  compileSdkVersion 22
  buildToolsVersion "22.0.0"

  defaultConfig {
    applicationId "com.hannesdorfmann.hippohappa"
    minSdkVersion 14
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
  }
  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }

  packagingOptions {
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE'
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
  }
}
cgruber commented 9 years ago

This seems like version skew. ValidationProcessor is part of Dagger 1, not Dagger 2. ComponentProcessor is the only processor that should be running on your paths. You seem (in this example) to be missing the actual project gradle file which would show us what your deps are. Can you please show that, and while you'er there, check and make sure you're pulling in the right dependencies?

sockeqwe commented 9 years ago

You are right! Sorry for that! I was migrating my app from dagger1 to dagger2. In fact the dependencies were declared correctly, but somehow the dagger1 compiler was still used. Even cleaning the project didn't helped. After deleting cache directories by hand it works. I think it was a problem on my machine, because I can't reproduce that on another pc (using the same project). Thanks for your help.

PS: if you have time, do you have any recommendation for unit testing on android with dagger2? http://stackoverflow.com/questions/29259688/injecting-test-module-with-dagger2