Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger2 do not generate Dagger* classes

Dagger2 does not create classes Dagger*. I created the interface IDaggerTestModule, but the class DaggerIDaggerTestModule not created! All libraries have. Maybe "apt" is not triggered???

my test project: https://github.com/gc986/Dagger2Test

MyConfig:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0-beta2'
        classpath 'me.tatarka:gradle-retrolambda:3.3.0-beta4'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

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


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

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"
    defaultConfig {
        applicationId "com.netelement.gc986.mpos_android"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    /*compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_8
    }*/

}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    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:24.2.0'
    testCompile 'junit:junit:4.12'

    compile 'io.reactivex:rxjava:1.1.9'

    // Retrofit
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'me.tatarka:gradle-retrolambda:3.3.0-beta4'
    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'

    // Dagger
    compile 'com.google.dagger:dagger:2.0'
    apt 'com.google.dagger:dagger-compiler:2.0'
    provided 'org.glassfish:javax.annotation:10.0-b28'

    // Butterknife
    compile 'com.jakewharton:butterknife:8.3.0'
    apt 'com.jakewharton:butterknife-compiler:8.3.0'

}

User.java

public class User {

    private String firstName;
    private String lastName;


    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

DaggerUser.java

@Module
public class DaggerUser {

    @Provides
    public User provideUser(){
        User user = new User();
        user.setFirstName("name1");
        user.setLastName("name2");

        return user;
    }

}

IDaggerFactory.java

@Singleton
@Component (
        modules = {DaggerUser.class}
)
public interface IDaggerFactory {

    void inject();
    User user();

}

This not create...

public class DaggerApplication extends Application {

IDaggerFactory component;

public static IDaggerFactory component(Context context){
    return ((DaggerApplication) context.getApplicationContext()).component;
}

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

    component = -->DaggerIDaggerFactory<--
                .builder()
                .daggerUser(new DaggerUser())
                .build();
}

}

like image 793
gc986 Avatar asked Mar 12 '23 07:03

gc986


1 Answers

That's because this method in your component interface doesn't actually do anything. Remove it.

void inject();

EDIT:

Okay, just got home from vacation. So first thing, removing void inject() fixes the following error:

Error:Execution failed for task ':app:compileDebugJavaWithJavac'. java.lang.IllegalArgumentException: not a valid component method: inject()

After that, it says "Cannot find symbol" in DaggerApplication because it hasn't been imported yet, so I added the import

import com.gc986.dagger2test.di.DaggerIDaggerFactory;

And then it compiled successfully

So in reality, you forgot to import the newly generated DaggerIDaggerFactory class in your Java file

like image 190
EpicPandaForce Avatar answered Mar 27 '23 00:03

EpicPandaForce