【问题标题】:Android Dagger2 and Butterknife conflictAndroid Dagger2 和 Butterknife 冲突
【发布时间】:2016-12-06 04:29:17
【问题描述】:

我学习了如何在我的 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;
}

}

这就是我用黄油刀注入我的观点的方式:

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

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

谢谢你..

【问题讨论】:

  • 如果您的 ButterKnife 注释处理失败,您的 Dagger 也会失败。检查R.id.listContact 是否指向`ListView 的实例并尝试清理和重建。此外,检查任何其他编译时错误。
  • 我的 ButterKnife 注释没有失败,并且 R.id.listContact 指向 ListView。我尝试清理和重建代码,但错误仍然显示,如果我删除 ButterKnife 注释,错误将消失
  • 你有最新版的匕首2和最新版的黄油刀吗?编辑您的帖子以至少包含build.gradle。更好的是,创建复制问题的最小项目 - 我们在一个项目中一起使用这两个库已经有一段时间了,没有出现这样的问题。
  • 你好@DavidRawson,我已经用我的 gradle.app 编辑了我的问题。请查看并感谢您的帮助。

标签: java android dagger-2 butterknife


【解决方案1】:

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

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

    我已经找到了解决方案,但我认为这不是完美的解决方案。我将黄油刀版本从 8.1.0 更改为 8.0.1,然后一切正常。我在我的 gradle.app 中更改它

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

    【讨论】:

    • 将匕首版本改为 2.8 怎么样?
    【解决方案3】:

    使用apt作用域时,需要在build.gradle中将packagingOptions添加到android {

    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 现在在编译时失败。

    【讨论】:

    • 这个我试过了,还是不行,还是报错。
    • 然后检查你的编译错误信息,除了“匕首组件不存在”,它应该在某个地方
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    相关资源
    最近更新 更多