问题描述
我正在用Kotlin在一个Android项目中测试Dagger 2.我的灵感来自 Android Clean Architecture 存储库.我的gradle构建中有两个模块,一个是"app",一个是"module".模块包含一个类调用模型.在我的应用gradle模块中,我使用模型提供程序创建了一个名为"DaggerModule"的Dagger模块.当我尝试构建项目时,出现编译错误:
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
当我尝试将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 { }
免责声明:这是我第一次驾驶Dagger 2,并且是摇篮新人.示例项目是我发现该问题的最低代码.我不期望它会做很多事情:D.
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.
推荐答案
您的 module/build.gradle 设置为java而不是kotlin.我已经将其修改为以下内容,并且可以正常编译.
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"
}
这篇关于未解决的参考匕首2 + Kotlin + Android Gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!