简体   繁体   中英

Dagger not generating Component classes

I have tried a great deal debugging this issue but unable to find the cause. Dagger simply doesn't create the DaggerComponent classes. I've checked SO for duplicates but none of the solutions provided worked.

project's build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        classpath 'me.tatarka:gradle-retrolambda:3.2.3'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath 'me.tatarka:gradle-retrolambda:3.0.1'

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

allprojects {
    repositories {
        jcenter()
        jcenter {
            url "http://jcenter.bintray.com"
        }
        mavenCentral()
        maven {
            url "https://oss.sonatype.org/content/repositories/snapshots"
        }

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app's build.gradle

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'me.tatarka.retrolambda'


android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    defaultConfig {
        applicationId "com.hr.crux"
        minSdkVersion 18
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {

        }
        release {
            minifyEnabled false

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

retrolambda {
    jvmArgs '-noverify'
}

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

    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    provided 'javax.annotation:jsr250-api:1.0'
    apt 'com.google.dagger:dagger-compiler:2.2'
    compile 'com.google.dagger:dagger:2.2'
    provided 'javax.annotation:jsr250-api:1.0'
    testCompile 'junit:junit:4.12'

}

HttpModule.java

@Module
public class HttpModule {
    @Provides
    @Singleton
    Retrofit getRetrofit() {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://maps.googleapis.com/maps/api/place/")
                .build();
        return retrofit;

    }
}

HttpComponent.java

@Singleton
@Component(modules = {HttpModule.class})
public interface HttpComponent {

    void inject(MainActivity activity);

}

Application.java

public class Application extends android.app.Application {

private static Application application;

private HttpComponent appComponent;

@Override
public void onCreate() {
    super.onCreate();
    application = this;
    appComponent = //Cannot find DaggerHttpComponent
}


public static Application getInstance() {
    return application;
}
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Inject
Retrofit retrofit;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
  }
}

Dagger is failing to generate the component class in my Application class. I've tried clean building, I've tried invalidating cache but nothing works.

You can start a fresh project by yourself instead of following the tutorial project. If you do so, here is the solution.

These two lines are responsible to generate the compile-time framework in Dagger 2.

compile 'com.google.dagger:dagger:2.14.1' //generates framework in compile time annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1' //generates framework in compile time based on the annotations you provided.

Full setup Dagger

        //dagger 2
        compile 'com.google.dagger:dagger:2.14.1'
        annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1'

        //to enable DaggerActivity, DaggerBroadcastReceiver, DaggerFragment etc classes
        compile 'com.google.dagger:dagger-android:2.14.1'
        annotationProcessor 'com.google.dagger:dagger-android-processor:2.14.1'

        //support libraries with dagger 2
        compile 'com.google.dagger:dagger-android-support:2.14.1'

Note : You need to configure Annotation Process as provided in the screenshot below. You can do this File>Other Settings>Default Settings> search "Annotation processor" 在此处输入图片说明

As Other threads' Answers does NOT work for me:

I've answered here

Pref -> Editor -> File Types -> Ignore Files And Folders -> Remove "Dagger*.java;"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM