Dagger2不会生成Dagger *类

时间:2016-08-26 10:22:55

标签: android dagger-2

Dagger2不会创建Dagger *类。我创建了接口IDaggerTestModule,但没有创建类DaggerIDaggerTestModule!所有图书馆都有。也许“apt”不会被触发???

我的测试项目: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();

}

这不会创造......

公共类DaggerApplication扩展了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();
}

}

1 个答案:

答案 0 :(得分:6)

那是因为组件界面中的这个方法实际上并没有做任何事情。去掉它。

void inject();

修改

好的,刚回家度假。首先,删除void inject()修复了以下错误:

  

错误:任务':app:compileDebugJavaWithJavac'的执行失败。   java.lang.IllegalArgumentException:不是有效的组件方法:inject()

之后,它在DaggerApplication中显示“无法找到符号”,因为它尚未导入,所以我添加了导入

  

import com.gc986.dagger2test.di.DaggerIDaggerFactory;

然后它成功编译

所以实际上,您忘记在Java文件中导入新生成的DaggerIDaggerFactory