簡體   English   中英

無法在Android中使用較新的Dagger 2.11 API

[英]Cannot use newer Dagger 2.11 API in Android

我試圖將Dagger 2導入Android Studio上的全新項目中,並查看各種指南和文檔,所以我無法使用DaggerAppComponent

我的Gradle設置如下:

建立(項目)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

構建(模塊:應用)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"

    defaultConfig {
        applicationId "com.raywenderlich.todolist"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'

    //Dagger 2
    compile 'com.google.dagger:dagger:2.11'
    compile 'com.google.dagger:dagger-android:2.11'
    annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
}

在我的TodolistApplication.java類中:

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

    DaggerAppComponent
            .builder()
            .application(this)
            .build()
            .inject(this);
  }

但是,Android Studio顯示以下錯誤:

錯誤:(21,5)錯誤:找不到符號變量DaggerAppComponent

我嘗試重建項目並導入各種dagger文件,但是似乎沒有任何效果。

錯誤:(21,5)錯誤:找不到符號變量DaggerAppComponent

上面的錯誤不是依賴性錯誤。 必須先創建AppComponent,然后再使用,如下所示

@Module public class ApplicationModule {
   //all dependency provides here
}

@Component(modules = ApplicationModule.class) 
public interface ApplicationComponent {
}

查看此參考資料以全面了解https://medium.com/@isoron/a-friendly-introduction-to-dagger-2-part-1-dbdf2f3fb17b

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM