Android Dagger 2在java / kotlin项目

时间:2018-03-10 18:05:04

标签: java android kotlin dagger-2

我真的需要你的帮助,因为我无法继续我的项目。我遇到的问题是当我尝试生成匕首组件时,它似乎无法使用。我将Dagger组件/模块的Java类和其他应用程序的Kotlin类使用。我尝试了仅使用java的类,它也不起作用。

Gradle:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

 android {
compileSdkVersion 26
  defaultConfig {
    applicationId "com.example.gebruiker.beertime1"
    minSdkVersion 15
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner 
   "android.support.test.runner.AndroidJUnitRunner"
     }
    buildTypes {
       release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 
'proguard-rules.pro'
    }
}



     }

   dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'

implementation 'com.google.dagger:dagger-android:2.14'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.14'


implementation 'com.google.dagger:dagger:2.14'
annotationProcessor 'com.google.dagger:dagger-compiler:2.14'
kapt 'com.google.dagger:dagger-compiler:2.14'

implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'

implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'io.reactivex.rxjava2:rxjava:2.1.9'

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-
core:3.0.1'
implementation 'com.android.support:recyclerview-v7:26.1.0'
 }

我的存储库组件:

package com.example.gebruiker.beertime1.di;

 import com.example.gebruiker.beertime1.MainScreen.MainActivity;

  import javax.inject.Singleton;

  import dagger.Component;

  @Component(modules = NetworkModule.class)
  public interface RepositoryComponent {
   void inject(MainActivity mainActivity);
 }

我的主要活动成分:

@Component(dependencies = RepositoryComponent.class ,modules = 
{AppModule.class,RepositoryModule.class,NetworkModule.class})
 interface MainActivityComponent{

void inject(MainActivity mainActivity);

 }

1 个答案:

答案 0 :(得分:3)

对于某些依赖项,您使用的是annotationProcessor而不是kapt。将您的build.gradle更改为以下内容,它应该有效:

def final dagger_version = '2.14.1'

implementation "com.google.dagger:dagger:${dagger_version}"
implementation "com.google.dagger:dagger-android:${dagger_version}"
implementation "com.google.dagger:dagger-android-support:${dagger_version}"
kapt "com.google.dagger:dagger-compiler:${dagger_version}"
kapt "com.google.dagger:dagger-android-processor:${dagger_version}"