我正在尝试将 Hilt 集成到我的项目中,但我无法通过初始配置。到目前为止,我按照官方文档一步一步添加了插件和依赖项。一旦我用 标记Application
类@HiltAndroidApp
,构建就会失败并出现以下错误:
/.../app/build/generated/source/kapt/stagingDebug/..../DaggerMyApp_HiltComponents_ApplicationC.java:110: error: cannot find symbol
return ImmutableSet.<ViewModelProvider.Factory>of();}
^
symbol: method <Factory>of()
location: class ImmutableSet
/.../app/build/generated/source/kapt/stagingDebug/..../DaggerMyApp_HiltComponents_ApplicationC.java:143: error: cannot find symbol
return ImmutableSet.<ViewModelProvider.Factory>of();}
^
symbol: method <Factory>of()
location: class ImmutableSet
2 errors
我检查了生成的文件,发现了一些麻烦的方法:
@Override
public Set<ViewModelProvider.Factory> getActivityViewModelFactory() {
return ImmutableSet.<ViewModelProvider.Factory>of();}
@Override
public Set<ViewModelProvider.Factory> getFragmentViewModelFactory() {
return ImmutableSet.<ViewModelProvider.Factory>of();}
我对 Dagger 和 Hilt 还很陌生,所以我不知道这可能来自哪里。我认为这可能是一个依赖问题,但我没有得到任何运气。我在项目中导入了一些外部模块,这是我的 gradle:
项目/build.gradle
buildscript {
apply from: 'dependencies.gradle'
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:${versions.gradle_version}"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin_version}"
classpath "io.realm:realm-gradle-plugin:${versions.realm_version}"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:${versions.navigation_version}"
classpath "com.google.dagger:hilt-android-gradle-plugin:${versions.hilt_version}"
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
// maven { url "https://jitpack.io" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
应用程序/build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
apply plugin: 'androidx.navigation.safeargs.kotlin'
apply plugin: 'dagger.hilt.android.plugin'
android {
compileSdkVersion 29
defaultConfig {
applicationId "..."
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
dataBinding {
enabled = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
flavorDimensions "version"
productFlavors {
staging{
...
}
}
}
configurations {
cleanedAnnotations
compile.exclude group: 'com.intellij' , module:'annotations'
}
realm {
syncEnabled = true
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation jetbrains.kotlin
implementation jetbrains.coroutines
implementation androidx.appcompat
implementation androidx.core_ktx
implementation google.material
implementation androidx.constraint_layout
// Navigation
implementation androidx.navigation_fragment
implementation androidx.navigation_ui
// Lifecycle runtime scope
implementation androidx.runtime_ktx
// Lifecycle viewmodel scope
implementation androidx.viewmodel_ktx
// Lifecycle livedata scope
implementation androidx.livedata_ktx
implementation androidx.legacy_support
implementation androidx.common_java8
// Amplify
implementation amplifyframework.core
implementation amplifyframework.auth_cognito
// Multidex
implementation androidx.multidex
kapt kapt_ext.databinding_compiler
implementation spongycastle.core
implementation spongycastle.prov
implementation zetetic.database_sqlcipher
// Timber
implementation logging_ext.timber
// Retrofit
implementation remotesource.retrofit2
implementation remotesource.gson
implementation remotesource.converter_gson
implementation remotesource.okhttp3_interceptor
// Room
implementation androidx.room
implementation androidx.room_ktx
// Hilt
implementation hilt_ext.hilt
kapt hilt_ext.hilt_compiler
// Test
testImplementation test_ext.junit
androidTestImplementation test_ext.arch_core_testing
androidTestImplementation google.truth
androidTestImplementation test_ext.coroutines_test
androidTestImplementation test_ext.test_ext_junit
androidTestImplementation test_ext.espresso_core
implementation project(path: ':...')
implementation project(path: ':...')
implementation project(path: ':...')
}
依赖项.gradle
ext {
versions = [
gradle_version : '4.0.1',
kotlin_version : '1.3.72',
coroutines_version : '1.3.4',
appcompat_version : '1.1.0',
core_ktx_version : '1.3.1',
material_version : '1.1.0',
constraint_layout_version : '1.1.3',
navigation_version : '2.3.0',
lifecycle_ktx_version : '2.2.0',
legacy_support_version : '1.0.0',
amplifyframework_version : '1.1.1',
databinding_compiler_version: '3.2.0-alpha10',
multidex_version : '2.0.1',
realm_version : '6.1.0',
room_version : '2.3.0-alpha02',
retrofit2_version : '2.9.0',
gson_version : '2.8.6',
converter_gson_version : '2.9.0',
okhttp3_interceptor_version : '4.8.0',
timber_version : '4.7.1',
spongycastle_version : '1.58.0.0',
database_sqlcipher_version : '4.4.0',
junit_version : '4.13',
arch_core_version : '2.1.0',
test_ext_junit_version : '1.1.1',
espresso_core_version : '3.2.0',
truth_version : '1.0.1',
hilt_version : '2.28-alpha',
hilt_extensions_version : '1.0.0-alpha01'
]
jetbrains = [
kotlin : "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${versions.kotlin_version}",
coroutines: "org.jetbrains.kotlinx:kotlinx-coroutines-core:${versions.coroutines_version}"
]
androidx = [
appcompat : "androidx.appcompat:appcompat:${versions.appcompat_version}",
core_ktx : "androidx.core:core-ktx:${versions.core_ktx_version}",
constraint_layout : "androidx.constraintlayout:constraintlayout:${versions.constraint_layout_version}",
navigation_fragment: "androidx.navigation:navigation-fragment-ktx:${versions.navigation_version}",
navigation_ui : "androidx.navigation:navigation-ui-ktx:${versions.navigation_version}",
runtime_ktx : "androidx.lifecycle:lifecycle-runtime-ktx:${versions.lifecycle_ktx_version}",
viewmodel_ktx : "androidx.lifecycle:lifecycle-viewmodel-ktx:${versions.lifecycle_ktx_version}",
livedata_ktx : "androidx.lifecycle:lifecycle-livedata-ktx:${versions.lifecycle_ktx_version}",
common_java8 : "androidx.lifecycle:lifecycle-common-java8:${versions.lifecycle_ktx_version}",
legacy_support : "androidx.legacy:legacy-support-v4:${versions.legacy_support_version}",
multidex : "androidx.multidex:multidex:${versions.multidex_version}",
room : "androidx.room:room-runtime:${versions.room_version}",
room_ktx : "androidx.room:room-ktx:${versions.room_version}"
]
kapt_ext = [
databinding_compiler: "com.android.databinding:compiler:${versions.databinding_compiler_version}",
room_compiler : "androidx.room:room-compiler:${versions.room_version}"
]
google = [
material: "com.google.android.material:material:${versions.material_version}",
truth : "com.google.truth:truth:${versions.truth_version}"
]
hilt_ext = [
hilt : "com.google.dagger:hilt-android:${versions.hilt_version}",
hilt_compiler : "com.google.dagger:hilt-android-compiler:${versions.hilt_version}",
hilt_extensions : "androidx.hilt:hilt-lifecycle-viewmodel:${versions.hilt_extensions_version}",
hilt_extensions_compiler: "androidx.hilt:hilt-compiler:${versions.hilt_extensions_version}"
]
remotesource = [
retrofit2 : "com.squareup.retrofit2:retrofit:${versions.retrofit2_version}",
gson : "com.google.code.gson:gson:${versions.gson_version}",
converter_gson : "com.squareup.retrofit2:converter-gson:${versions.converter_gson_version}",
okhttp3_interceptor: "com.squareup.okhttp3:logging-interceptor:${versions.okhttp3_interceptor_version}"
]
logging_ext = [
timber: "com.jakewharton.timber:timber:${versions.timber_version}"
]
amplifyframework = [
core : "com.amplifyframework:core:${versions.amplifyframework_version}",
auth_cognito: "com.amplifyframework:aws-auth-cognito:${versions.amplifyframework_version}"
]
spongycastle = [
core: "com.madgag.spongycastle:core:${versions.spongycastle_version}",
prov: "com.madgag.spongycastle:prov:${versions.spongycastle_version}"
]
zetetic = [
database_sqlcipher: "net.zetetic:android-database-sqlcipher:${versions.database_sqlcipher_version}@aar"
]
test_ext = [
junit : "junit:junit:${versions.junit_version}",
arch_core_testing: "androidx.arch.core:core-testing:${versions.arch_core_version}",
coroutines_test : "org.jetbrains.kotlinx:kotlinx-coroutines-test:${versions.coroutines_version}",
test_ext_junit : "androidx.test.ext:junit:${versions.test_ext_junit_version}",
espresso_core : "androidx.test.espresso:espresso-core:${versions.espresso_core_version}"
]
}
我希望有人至少能指出我正确的方向,现在我盲目地试图猜测问题出在哪里。谢谢。