2016-04-11 7 views
4

Ich versuche Dagger2 mit Kotlin zu verwenden, aber heute versucht, diesen Fehler zu kompilieren bekam:Kotlin Dagger2 nicht finden können, Symbol ApplicationModule_ProvideApplicationFactory

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

Error:(5, 43) error: cannot find symbol class ApplicationModule_ProvideApplicationFactory

(App) Build.gradle

apply plugin: 'com.android.application' 
apply plugin: 'kotlin-android' 
apply plugin: 'kotlin-android-extensions' 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.3" 

    defaultConfig { 
     applicationId "com.ilyarb.geotags" 
     minSdkVersion 15 
     targetSdkVersion 23 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    sourceSets { 
     main.java.srcDirs += 'src/main/kotlin' 
    } 
    dexOptions { 
     javaMaxHeapSize "2048M" 
    } 
} 

kapt { 
    generateStubs = true 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 

    final SUPPORT_LIBRARY_VERSION = "23.3.0" 
    final PLAY_SERVICES_VERSION = "8.4.0" 

    compile "com.android.support:appcompat-v7:$SUPPORT_LIBRARY_VERSION" 
    compile "com.android.support:design:$SUPPORT_LIBRARY_VERSION" 

    // Dagger 2 
    compile 'com.google.dagger:dagger:2.0.2' 
    kapt 'com.google.dagger:dagger-compiler:2.0.2' 
    provided 'org.glassfish:javax.annotation:10.0-b28' 

    compile "com.google.android.gms:play-services-maps:$PLAY_SERVICES_VERSION" 
    compile "com.google.android.gms:play-services-location:$PLAY_SERVICES_VERSION" 

    compile 'com.jakewharton.timber:timber:4.1.0' 
    compile 'com.jakewharton.byteunits:byteunits:0.9.1' 

    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1' 
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1' 

    testCompile 'junit:junit:4.12' 
} 

repositories { 
    mavenCentral() 
} 

buildscript { 
    ext.kotlin_version = '1.0.1-2' 
repositories { 
    mavenCentral() 
} 

dependencies { 
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 
} 
} 

root build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules. 

buildscript { 
    repositories { 
     jcenter() 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:1.5.0' 

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

allprojects { 
    repositories { 
     jcenter() 
    } 
} 

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

Anwendungsklasse

package com.ilyarb.geotags 

import android.app.Application 
import com.google.android.gms.common.api.GoogleApiClient 
import com.ilyarb.geotags.injection.component.ApplicationComponent 
import com.ilyarb.geotags.injection.component.DaggerApplicationComponent 
import com.ilyarb.geotags.injection.module.ApplicationModule 
import com.squareup.leakcanary.LeakCanary 
import timber.log.Timber 
import javax.inject.Inject 

class GeotagApp : Application() { 

    @Inject lateinit var mGoogleApiClient: GoogleApiClient 

    companion object { 
     @JvmStatic lateinit var mApplicationComponent: ApplicationComponent 
    } 

    override fun onCreate() { 
     super.onCreate() 
     LeakCanary.install(this) 

     if (BuildConfig.DEBUG) { 
      Timber.plant(Timber.DebugTree()) 
     } 

     mApplicationComponent = DaggerApplicationComponent.builder() 
      .applicationModule(ApplicationModule(this)) 
      .build() 

     mApplicationComponent.inject(this) 
    } 

} 

Anwendungskomponente

package com.ilyarb.geotags.injection.component 

import android.app.Application 
import com.ilyarb.geotags.injection.module.ApplicationModule 
import dagger.Component 
import javax.inject.Singleton 

@Singleton 
@Component(modules = arrayOf(ApplicationModule::class)) 
interface ApplicationComponent { 

    fun inject(application: Application) 

} 

Applikationsmodul

package com.ilyarb.geotags.injection.module 

import android.app.Application 
import com.google.android.gms.common.api.GoogleApiClient 
import com.google.android.gms.location.LocationServices 
import dagger.Module 
import dagger.Provides 
import javax.inject.Singleton 

@Module 
class ApplicationModule(private val mApplication: Application) { 

    @Provides 
    @Singleton 
    fun provideGoogleApiClient() : GoogleApiClient { 
     return GoogleApiClient.Builder(mApplication) 
      .addApi(LocationServices.API) 
      .build() 
    } 

} 

Es erzeugt DaggerApplicationComponent aber wenn ich versuche zu laufen applicatio n es scheitert mit diesem Fehler.

Ich habe bereits versucht, das Projekt zu säubern und neu zu erstellen, aber es hat nicht funktioniert.

Jede Hilfe wird sehr geschätzt.

+1

Ilia, vielleicht können Sie Beispielprojekt auf Github erstellen, es wird wirklich Menschen helfen, auf Ihre Frage zu antworten. – IRus

+0

@IRus Ich habe ein Projekt, das einen gleichen Fehler [Link] (https://github.com/ilya-rb/Photo-Map) hat. Ich habe bereits versucht, ein neues Projekt zu erstellen und es funktioniert, aber immer noch gleichen Fehler –

Antwort

1

Durch Ihr Projekt sah ich einige Ihrer Dolche Module s bieten Methoden mit ähnlichen Namen wie providesContext(). Dagger 2 (oder kapt) hat möglicherweise ein Problem damit, was zu deinem Fehler führt. Bitte versuchen Sie, sie umzubenennen, so dass alle @Provides Methoden eindeutige Namen haben.

Auch Annotationen haben standardmäßig die Laufzeitbeibehaltung in Kotlin, so dass Sie @Retention(AnnotationRetention.RUNTIME) nicht verwenden müssen.

+0

Es funktioniert, vielen Dank. Du hast den Tag gerettet –

Verwandte Themen