7

I had my project working great with Butterknife to do view injection. But I then needed to add Dagger in order to inject dependencies.

I added the Annotation Processor Tool Gradle plugin with the appropriate Dagger requirement (Only showing the modified parts for brevity);

buildScript {
    repositories {
        maven {
            url "https://oss.sonatype.org/content/repositories/snapshots/"
        }
    }
    dependencies {
        ...
        classpath 'com.jimdo.gradle:gradle-apt-plugin:0.2-SNAPSHOT'
    }
}

apply plugin: 'apt'

dependencies {
    apt "com.squareup.dagger:dagger-compiler:${daggerVersion}"

    ...
}

At this point now when I build and run the application the properties marked with the @InjectView annotation are not getting injected with the following debug messages emitted by Butterknife;

D/ButterKnife﹕ Looking up view injector for com.example.MainActivity
D/ButterKnife﹕ Not found. Trying superclass com.example.FactListAbstractActivity
D/ButterKnife﹕ Not found. Trying superclass android.app.Activity
marcus.ramsden
  • 2,633
  • 1
  • 22
  • 33

1 Answers1

7

Turns out all I needed to do was add another apt dependency in order to have the Annotation Processor Tool pickup the processor included in Butterknife. This is in addition to including it as a compile time dependency;

dependency {
    apt "com.jakewharton:butterknife:${butterknifeVersion}"

    compile "com.jakewharton:butterknife:${butterknifeVersion}"
}
marcus.ramsden
  • 2,633
  • 1
  • 22
  • 33
  • 2
    No reason to list it twice. Just the `compile` declaration is enough for Butter Knife. – Jake Wharton Dec 24 '13 at 02:39
  • That was when I found the processor stopped working. Let me just try it again, I wonder if I was getting a cached build issue with AS. – marcus.ramsden Dec 24 '13 at 10:29
  • @jake-wharton it looks like something is going wrong specifically when using the apt plugin if I remove the apt definition butter knife complains at runtime about being unable to find a view injector. I'll need to take a look a bit more of a look at the plugin I'm using. – marcus.ramsden Dec 24 '13 at 10:41
  • I haven't used that plugin specifically. It may be disabling the annotation processor from running in the normal `javac` task. Use what works, I guess. – Jake Wharton Dec 24 '13 at 22:20
  • @JakeWharton the plugin is setting javac's `-processorpath` so it disables searching the classpath for annotation processors. Others, such as Spring's `propdeps-plugin` simply tweak the classpath. – Thomas Broyer Jan 06 '14 at 01:16
  • How do we know what the current ${butterknifeVersion} is? – IgorGanapolsky Jun 06 '14 at 21:35
  • This was just a variable that I defined elsewhere in my build.gradle before the dependencies section. Take a look at Gradle's [ExtraPropertiesExtension](http://www.gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html). – marcus.ramsden Jun 07 '14 at 10:09