代码之家  ›  专栏  ›  技术社区  ›  user1290932

Android Dagger2和Butterknife冲突

  •  4
  • user1290932  · 技术社区  · 8 年前

    我学习了如何在我的android项目中使用dagger2和Butterknife,在我注入Butterknife BindView注释的视图之前,一切都很好,它会显示这样的错误;

    错误:(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注释和ButterKnife绑定,那么它工作得很好,但是如果我使用它,就会出现错误。

    这是我的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'
    }
    

    请帮助我,任何答案都将不胜感激。。。

    非常感谢。

    3 回复  |  直到 8 年前
        1
  •  15
  •   dazza5000    8 年前

    我能够通过对两个依赖项使用相同的注释插件来修复此问题:

    // 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

        2
  •  2
  •   user1290932    8 年前

    //butterknife
    compile 'com.jakewharton:butterknife:8.0.1'
    apt 'com.jakewharton:butterknife-compiler:8.0.1'
    
        3
  •  0
  •   EpicPandaForce    8 年前

    使用时 apt 范围,您需要添加以下内容 packagingOptions 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在编译时失败。