2016-12-06 95 views
4

我學習如何在我的android項目中使用dagger2和Butterknife,一切工作正常,直到我注入Butterknife BindView註釋的視圖,它顯示這樣的錯誤;Android的Dagger2和Butterknife衝突

錯誤:(5·57)錯誤:無法找到符號類DaggerNetComponent

這是我DaggerNetComponent代碼:

public class App extends Application { 

private NetComponent mNetComponent; 

@Override 
public void onCreate() { 
    super.onCreate(); 

    mNetComponent = DaggerNetComponent.builder().appModule(new AppModule(this)).netModule(new NetModule("http://example.com/")) 
      .build(); 
} 

public NetComponent getNetComponent() 
{ 
    return mNetComponent; 
} 

}

,這是我注入我的看法與butterknife :

public class MainActivity extends AppCompatActivity implements MainScreenContact.View { 

@BindView(R.id.listContact) 
ListView listView; 

ArrayList<CharSequence> list; 
ArrayAdapter<CharSequence> adapter; 

@Inject 
MainScreenPresenter mainPresenter; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ButterKnife.bind(this); 

如果我刪除BindView註釋但是terKnife綁定在這個類上,然後它可以正常工作,但是如果我使用它,錯誤將會出現。

這是我Gradle.app

apply plugin: 'com.android.application' 
apply plugin: 'com.neenbedankt.android-apt' 
apply plugin: 'android-apt' 


android { 
compileSdkVersion 25 
buildToolsVersion "24.0.3" 
defaultConfig { 
    applicationId "com.project.echo.contactmanagement" 
    minSdkVersion 14 
    targetSdkVersion 25 
    versionCode 1 
    versionName "1.0" 
    testInstrumentationRunner  "android.support.test.runner.AndroidJUnitRunner" 
} 
buildTypes { 
    release { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 
} 

dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar']) 

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
    exclude group: 'com.android.support', module: 'support-annotations' 
}) 
compile 'com.android.support:appcompat-v7:25.0.0' 

//Retrofit 
compile 'com.squareup.retrofit2:retrofit:2.0.2' 
//OkHttp 
compile 'com.squareup.okhttp3:okhttp:3.2.0' 
compile 'com.squareup.okio:okio:1.7.0' 

//Gson 
compile 'com.google.code.gson:gson:2.6.2' 
compile 'com.squareup.retrofit2:converter-gson:2.0.1' 

//RxJava 
compile 'io.reactivex:rxjava:1.1.2' 
compile 'io.reactivex:rxandroid:1.1.0' 
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1' 

testCompile 'junit:junit:4.12' 

//Dagger 
apt 'com.google.dagger:dagger-compiler:2.2' 
compile 'com.google.dagger:dagger:2.2' 
provided 'javax.annotation:jsr250-api:1.0' 

//butterknife 
compile 'com.jakewharton:butterknife:8.1.0' 
apt 'com.jakewharton:butterknife-compiler:8.1.0' 
} 

請幫助我,任何回答將欣賞...

謝謝。

+0

如果您的ButterKnife註解處理失敗,您的Dagger也會失敗。檢查R.id.listContact指向ListView的一個實例並嘗試清理和重建。另外,檢查是否有其他編譯時錯誤。 –

+0

My ButterKnife註釋不會失敗,並且R.id.listContact指向ListView。我試圖清理並重建代碼,但錯誤仍然顯示,如果我刪除了ButterKnife註釋,錯誤將消失 – user1290932

+0

您是否擁有最新版本的Dagger 2和最新版本的ButterKnife?編輯你的文章至少包含'build.gradle'。更好的是,創建複製問題的最小項目 - 我們一直在一個項目中一起使用這兩個庫,而沒有像這樣的問題。 –

回答

14

我能夠通過使用兩個依賴同一註解插件來解決這個問題:閱讀下面的問題後

// Butter Knife 
compile 'com.jakewharton:butterknife:8.4.0' 
apt 'com.jakewharton:butterknife-compiler:8.4.0' 
// Dagger 2 
apt 'com.google.dagger:dagger-compiler:2.8' 
compile 'com.google.dagger:dagger:2.8' 
provided 'javax.annotation:jsr250-api:1.0' 

想出了這個修復:

https://github.com/JakeWharton/butterknife/issues/803

+0

是的,這有效。謝謝.. – user1290932

+0

兩個庫的更改版本都適合我 –

0

當使用apt範圍,則需要添加以下到android {在您的build.gradle中:

android { 
    // ... 
    packagingOptions { 
     // Exclude file to avoid 
     // Error: Duplicate files during packaging of APK 
     exclude 'META-INF/DEPENDENCIES' 
     exclude 'META-INF/LICENSE' 
     exclude 'META-INF/LICENSE.txt' 
     exclude 'META-INF/license.txt' 
     exclude 'META-INF/NOTICE' 
     exclude 'META-INF/NOTICE.txt' 
     exclude 'META-INF/notice.txt' 
     exclude 'META-INF/ASL2.0' 
     exclude 'META-INF/services/javax.annotation.processing.Processor' // <-- that one 
    } 
} 

而且我也認爲你只應用android-apt一次。

apply plugin: 'com.android.application' 
apply plugin: 'com.neenbedankt.android-apt' 
//apply plugin: 'android-apt' 

但另一種可能性是,你試圖綁定不會在佈局存在,所以ButterKnife在編譯時失敗,現在一個視圖。

+0

我試過這個,但沒有成功,仍然得到錯誤。 – user1290932

+0

然後檢查你的編譯錯誤信息超出「匕首組件不存在」,它應該在那裏 – EpicPandaForce

2

我找到了解決方案,但我不認爲這是最完美的解決方案。我將butterknife版本從8.1.0更改爲8.0.1,然後一切正常。我改變它在我的gradle.app

//butterknife 
compile 'com.jakewharton:butterknife:8.0.1' 
apt 'com.jakewharton:butterknife-compiler:8.0.1' 
+0

如何將匕首版本更改爲2.8? –