Unresolved reference dagger 2 + kotlin + android g

2019-06-15 04:52发布

I'm testing out Dagger 2 with Kotlin in an Android project. I was inspired by the Android Clean Architecture repo. I have two modules in my gradle build, one is "app" and one is "module". Module contains one class call Model. In my app gradle module I created a dagger module called "DaggerModule" with a Model provider. When I try to build the project, I get compilation errors:

DaggerModule.kt: (3, 57): Unresolved reference: Model 
DaggerModule.kt: (9, 34): Unresolved reference: Model
DaggerModule.kt: (9, 42): Unresolved reference: Model

When I try moving the Model class from "module" module to inside of the "app" module everything compiles w/o error. Trying to figure out if I'm doing something stupid, or if I should file a bug somewhere.

For reference here the snippets of the problematic files:

----
app/build.gradle

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.12.1218'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

repositories {
    jcenter()
    mavenCentral()
}

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.github.app.kotlin_unresolved_reference.app"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile project(':module')
    compile 'com.android.support:appcompat-v7:22.2.1'

    kapt "com.google.dagger:dagger-compiler:2.0.1"
    compile "com.google.dagger:dagger:2.0.1"
    compile "javax.annotation:javax.annotation-api:1.2"
    compile "org.jetbrains.kotlin:kotlin-stdlib:0.12.1218"
}


---
module/build.gradle

apply plugin: 'java'

//noinspection GroovyUnusedAssignment
sourceCompatibility = 1.7
//noinspection GroovyUnusedAssignment
targetCompatibility = 1.7

configurations {
    provided
}

sourceSets {
    main {
        compileClasspath += configurations.provided
    }
}

dependencies {
    provided "com.google.dagger:dagger-compiler:2.0.1"
    compile "com.google.dagger:dagger:2.0.1"
    compile "javax.annotation:javax.annotation-api:1.2"
    compile "org.jetbrains.kotlin:kotlin-stdlib:0.12.1218"
}

---
DaggerModule.kt

Module
public class DaggerModule {
    Provides fun provideModel(): Model = Model()
}

----
Model.kt

public class Model { }

DISCLAIMER: This is my first time test driving Dagger 2 and am a gradle newb. The sample project is the minimum code I could find to display the problem. I'm not expecting it to do much :D.

1条回答
啃猪蹄的小仙女
2楼-- · 2019-06-15 05:23

Your module/build.gradle is set to java instead of kotlin. I have modified it to the following and it compiles ok.

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.12.1218'
    }
}

apply plugin: 'kotlin'

dependencies {
    kapt "com.google.dagger:dagger-compiler:2.0.1"
    compile "com.google.dagger:dagger:2.0.1"
    compile "javax.annotation:javax.annotation-api:1.2"
    compile "org.jetbrains.kotlin:kotlin-stdlib:0.12.1218"
}
查看更多
登录 后发表回答