2016-12-24 14 views
6

Próbuję dodać Kotlin do mojego projektu, ale po włączeniu Kotlin nie mogę budować, ponieważ klasy Dagger2 nie są już generowane. Próbowałem drugiego projektu i mam ten sam problem (jest gorzej, narzeka zarówno na Dagger2, jak i DataBinding).Kotlin i Dagger2

Są to zmiany mam zrobić aby umożliwić Kotlin: build.gradle

projektu:

build.gradle
diff --git a/build.gradle b/build.gradle 
index 486700c..91e4cda 100644 
--- a/build.gradle 
+++ b/build.gradle 
@@ -1,13 +1,15 @@ 
// Top-level build file where you can add configuration options common to all sub-projects/modules. 

buildscript { 
+ ext.kotlin_version = '1.0.5-3' 
    repositories { 
     jcenter() 
    } 
    dependencies { 
-  classpath 'com.android.tools.build:gradle:2.3.0-alpha2' 
+  classpath 'com.android.tools.build:gradle:2.3.0-beta1' 
     classpath 'com.google.gms:google-services:3.0.0' 
     classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' 
+  classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 

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

App:

diff --git a/app/build.gradle b/app/build.gradle 
index 345dab0..e59f91c 100644 
--- a/app/build.gradle 
+++ b/app/build.gradle 
@@ -1,5 +1,6 @@ 
apply plugin: 'com.android.application' 
-apply plugin: 'com.neenbedankt.android-apt' 
+apply plugin: 'kotlin-android' 
+apply plugin: 'kotlin-kapt' 

android { 
    compileSdkVersion 25 
@@ -39,6 +40,13 @@ android { 
     incremental true 
     javaMaxHeapSize "5g" 
    } 
+ sourceSets { 
+  main.java.srcDirs += 'src/main/kotlin' 
+ } 
+} 
+ 
+kapt { 
+ generateStubs = true 
} 

dependencies { 
@@ -71,11 +79,15 @@ dependencies { 
    compile 'com.artemzin.rxjava:proguard-rules:1.2.1.0' 

    // Dagger 2 
- apt 'com.google.dagger:dagger-compiler:2.7' 
- testApt 'com.google.dagger:dagger-compiler:2.7' 
+ kapt 'com.google.dagger:dagger-compiler:2.7' 
+ //testApt 'com.google.dagger:dagger-compiler:2.7' 
    compile 'com.google.dagger:dagger:2.7' 
    provided 'javax.annotation:jsr250-api:1.0' 
+ compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 
} 

apply plugin: 'com.google.gms.google-services' 
+repositories { 
+ mavenCentral() 
+} 

Błąd dzieje tutaj:

sObjectGraph = DaggerObjectGraph 
    .builder() 
    .appModule(new AppModule(context.getApplicationContext())) 
    .build(); 

gdzie DaggerObjectGraph nie jest już zdefiniowany.

Każda pomoc zostanie bardzo doceniona.

+0

Myślę, że nie należy jawnie używać 'kotlin-kapt'. Jest to jednak dzikie domysły. – EpicPandaForce

+0

Nie wiem, co przez to rozumiesz, ale informacje, które znalazłem online dla Dagger2 z Kotlin wyraźnie wskazują, że procesor kapt musi być używany (https://blog.jetbrains.com/kotlin/2015/06/better -annotation-processing-supports-stubs-in-kapt /). – Francesc

+0

Spróbuj bez niego. – EpicPandaForce

Odpowiedz

2

Wystarczy usunąć

kapt { 
    generateStubs = true 
} 
0

Problemem jest twój używasz

DaggerObjectGraph

który nie jest już dostępny w dagger2.

Here is the documentation for dagger 2.

To jest wersja robocza

w poziomie build.gradle app dodać następujące pod zamknięciem Zależności

compile "com.google.dagger:dagger:2.9" 
    kapt "com.google.dagger:dagger-compiler:2.9" 
    provided 'javax.annotation:jsr250-api:1.0' 

dodaj to również do tego samego pliku

kapt { 
    generateStubs = true 
} 

Następnie należy utworzyć składniki i klasy modułów i wstrzyknąć dowolną aktywność lub fragment, który chcesz.

Oto Klasa Próbka Składnik

@Singleton 
@Component(modules = arrayOf(AppModule::class, NetModule::class)) 
interface AppComponent { 

    fun gson() : Gson 
    fun retrofit(): Retrofit 
    fun okHttpClient(): OkHttpClient 
} 

Tutaj jest klasa Module próbka

@Module 
class AppModule(internal var mApplication: Application) { 

    @Provides 
    @Singleton 
    fun providesApplication(): Application { 
     return mApplication 
    } 

} 

W mojej klasie aplikacji

class App : Application() { 

companion object { 
    @JvmStatic lateinit var component: AppComponent 
} 

override fun onCreate() { 
    super.onCreate() 

    appComponent = DaggerAppComponent.builder() 
      .appModule(AppModule(this)) 
      .netModule(NetModule(Constants.BASE_URL)) 
      .build() 

} 


} 

ten sposób składniki sztylet są inicjowane w sztyletem 2. Możesz teraz tworzyć podkomponenty dla różnych działań lub fragmentów i wstrzykiwać je.

Powiązane problemy