How do I get Dagger and Butterknife working with G

2019-04-25 06:16发布

问题:

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

回答1:

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}"
}