Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Dagger and Butterknife working with Gradle?

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
like image 675
marcus.ramsden Avatar asked Dec 23 '13 11:12

marcus.ramsden


1 Answers

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}"
}
like image 175
marcus.ramsden Avatar answered Jan 16 '23 16:01

marcus.ramsden