我如何让 Dagger 和 Butterknife 与 Gradle 一起工作? [英] How do I get Dagger and Butterknife working with Gradle?

查看:22
本文介绍了我如何让 Dagger 和 Butterknife 与 Gradle 一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目使用 Butterknife 进行了视图注入,效果很好.但是我需要添加 Dagger 以注入依赖项.

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

我添加了 Annotation Processor Tool Gradle 插件和适当的 Dagger 要求(为了简洁,只显示修改的部分);

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

    ...
}

此时,当我构建和运行应用程序时,用 @InjectView 注释标记的属性不会被注入 Butterknife 发出的以下调试消息;

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

推荐答案

原来我需要做的就是添加另一个 apt 依赖项,以便让 Annotation Processor Tool 拾取包含在 Butterknife 中的处理器.这是作为编译时依赖项的补充;

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

这篇关于我如何让 Dagger 和 Butterknife 与 Gradle 一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆