【发布时间】:2016-07-29 04:20:55
【问题描述】:
我有一个 Android 应用,我一直在转换为 Kotlin,但在涉及特定模块时,我遇到了 Dagger 的障碍。
在我的项目build.gradle 中,我有:
buildscript {
ext.kotlin_version = "1.0.1-2"
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0-alpha5'
classpath 'com.google.gms:google-services:2.0.0-rc3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
在app/build.gradle,我有以下相关设置:
apply plugin: "nebula.dependency-lock"
apply plugin: "com.android.application"
apply plugin: 'kotlin-android'
apply plugin: "kotlin-android-extensions"
apply plugin: "com.getkeepsafe.dexcount"
// ...
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
// ...
defaultConfig {
applicationId "com.myapp"
minSdkVersion 16
targetSdkVersion 23
// ...
}
buildTypes {
debug {
applicationIdSuffix ".debug"
// ...
}
release {
// ...
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
debug.java.srcDirs += 'src/debug/kotlin'
release.java.srcDirs += 'src/release/kotlin'
androidTest.java.srcDirs += 'src/androidTest/kotlin'
test.java.srcDirs += 'src/test/kotlin'
}
}
kapt {
generateStubs = true
}
dependencies {
compile fileTree(include: ["*.jar"], dir: "libs")
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "com.google.android.gms:play-services-gcm:$playServicesVersion"
compile "com.google.android.gms:play-services-analytics:$playServicesVersion"
compile "com.google.dagger:dagger:$daggerVersion"
kapt "com.google.dagger:dagger-compiler:$daggerVersion"
}
buildscript {
ext.daggerVersion = "2.2"
ext.playServicesVersion = "8.4.0"
dependencies {
classpath 'com.netflix.nebula:gradle-dependency-lock-plugin:4.2.0'
classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.4.4'
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}
repositories {
mavenCentral()
}
}
repositories {
mavenCentral()
jcenter()
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}
apply plugin: "com.google.gms.google-services"
我有一个应用程序组件:
@Singleton
@Component(
modules = arrayOf(
DebugApplicationModule::class,
DataModule::class,
DebugApiModule::class,
GoogleApiModule::class,
DebugSystemServicesModule::class
)
)
interface ApplicationComponent : DebugApplicationGraph
还有GoogleApiModule:
@Module
@Singleton
class GoogleApiModule {
@Provides
@Singleton
fun provideGoogleApiAvailability(): GoogleApiAvailability {
return GoogleApiAvailability.getInstance()
}
@Provides
@Singleton
fun provideGcmRegistrationManager(
context: Context,
preferences: AppPreferences,
googleApiAvailability: GoogleApiAvailability,
apiService: ApiService): GCMRegistrationManager {
return GCMRegistrationManager(context, preferences, googleApiAvailability, apiService)
}
@Provides
@Singleton
fun provideGoogleAnalytics(context: Context): GoogleAnalytics {
val analytics = GoogleAnalytics.getInstance(context)
analytics.setDryRun(BuildConfig.DEBUG)
return analytics
}
@Provides
@Singleton
fun provideAnalyticsTracker(analytics: GoogleAnalytics): Tracker {
val tracker = analytics.newTracker(BuildConfig.GOOGLE_ANALYTICS_TRACKING_ID)
tracker.enableExceptionReporting(true)
tracker.enableAutoActivityTracking(true)
return tracker
}
}
当我构建应用程序时,我从生成的 `DaggerApplicationComponent 中得到以下错误,说它找不到这些类的符号:
在我的任何其他模块中似乎都没有任何其他错误(有很多),main 中针对该组件的所有其他模块似乎也可以正常工作。
我不明白的是什么会导致这个模块无法为这个模块构建工厂,而不是其他模块,以及为什么它会如此悄无声息地失败。相同的结构在应用程序的 Java 版本中构建良好。
我尝试使用provided 而不是kapt 来代替dagger-compiler,但这似乎完全跳过了匕首编译。
我已经尝试了许多配置更改以使其正常工作,但我总是得到相同的结果。
有时,但并非总是如此,我也会在错误之前打印此消息:
Note: Generating a MembersInjector for com.myapp.services.GCMMessageListenerService.
Prefer to run the dagger processor over that class instead.
我不确定这意味着什么,我不确定它是否与缺少的编译有关,如果是,我也不知道如何修复它。
【问题讨论】:
-
您的
kapt配置设置正确。您正在正确使用最新的 dagger 2 和 kotlin 版本。除非您使用 Jack 编译器或 retrolambda,否则我认为您不应该使用 JDK8 进行编译。此外,您似乎缺少provided 'org.glassfish:javax.annotation:10.0-b28',请参阅此处github.com/damianpetla/kotlin-dagger-example/blob/master/app/…。
标签: android android-gradle-plugin google-play-services kotlin dagger-2