2013-12-23 21 views
7

我有我的項目與Butterknife偉大的工作,以做注射。但是我需要添加Dagger以注入依賴關係。我如何獲得使用Gradle的Dagger和Butterknife?

我添加Annotation Processor Tool搖籃插件與適當匕首要求(僅示出了出於簡潔的修改的部分);

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發出以下的調試消息注入的應用;

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 

回答

7

結果我需要做的就是添加另一個apt依賴項,以使註釋處理器工具獲取Butterknife中包含的處理器。除了將它作爲編譯時間依賴包含在內之外;

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

    compile "com.jakewharton:butterknife:${butterknifeVersion}" 
} 
+2

沒有理由列出它的兩倍。只要編譯聲明就足夠用於黃油刀。 –

+0

那是當我發現處理器停止工作。讓我再試一次,我想知道是否得到了AS的緩存構建問題。 –

+0

@ jake-wharton在使用apt插件時,如果我刪除了apt定義,黃油刀在運行時抱怨無法找到視圖注入器,它看起來像某些事情會出錯。我需要更多地看看我使用的插件。 –