2016-12-06 93 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? –