繁体   English   中英

匕首不生成组件类

[英]Dagger not generating Component classes

我已经尝试了很多调试这个问题,但无法找到原因。 Dagger 根本不创建 DaggerComponent 类。 我已经检查了 SO 是否有重复项,但所提供的解决方案都没有奏效。

项目的 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
}

应用程序的 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);

}

应用程序.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 无法在我的 Application 类中生成组件类。 我尝试过干净的构建,我尝试过使缓存无效,但没有任何效果。

您可以自己启动一个新项目,而不必遵循教程项目。 如果您这样做,这是解决方案。

这两行负责在Dagger 2中生成编译时框架。

compile 'com.google.dagger:dagger:2.14.1' //在编译时annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1'生成框架compile 'com.google.dagger:dagger:2.14.1' annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1' //在您提供的注释的基础上在编译时生成框架。

完整设定匕首

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

注意 :您需要按照下面的屏幕快照中的说明配置注释过程。 您可以执行以下操作:“ File>Other Settings>Default Settings>搜索"Annotation processor" 在此处输入图片说明

由于其他线程的答案对我不起作用:

我已经在这里回答了

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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