未解决的参考匕首2 + kotlin + android gradle

时间:2015-07-28 05:12:09

标签: android android-gradle kotlin dagger-2

我正在Android项目中使用Kotlin测试Dagger 2。我受Android Clean Architecture回购的启发。我的gradle构建中有两个模块,一个是" app"一个是"模块"。模块包含一个类调用模型。在我的app gradle模块中,我创建了一个名为" DaggerModule"的Dagger模块。与模型提供者。当我尝试构建项目时,出现编译错误:

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

当我尝试从"模块"移动Model类时模块到" app"模块一切都编译没有错误。试着弄清楚我是不是做了些蠢事,或者我是否应该在某个地方提交错误。

此处参考有问题文件的片段:

----
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 { }

免责声明:这是我第一次尝试驾驶Dagger 2并且是一个gradle newb。示例项目是我可以找到的显示问题的最小代码。我不希望它做得太多:D。

1 个答案:

答案 0 :(得分:10)

您的 module / build.gradle 设置为java而不是kotlin。我已将其修改为以下内容并且编译正常。

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"
}